blob: a5a844b1d8e3691b733af4e407ecc03be850ccec [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files --------*- C++ -*-===//
John Criswell856ba762003-10-21 15:17:13 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This file implements the bison parser for LLVM assembly languages files.
11//
Chris Lattnercf3056d2003-10-13 03:32:08 +000012//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +000013
Chris Lattner00950542001-06-06 20:29:01 +000014%{
15#include "ParserInternals.h"
Chris Lattner00950542001-06-06 20:29:01 +000016#include "llvm/SymbolTable.h"
17#include "llvm/Module.h"
Chris Lattner00950542001-06-06 20:29:01 +000018#include "llvm/iTerminators.h"
19#include "llvm/iMemory.h"
Chris Lattner1cff96a2002-09-10 22:37:46 +000020#include "llvm/iOperators.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000021#include "llvm/iPHINode.h"
Chris Lattner830b6f92004-04-05 01:30:04 +000022#include "llvm/Support/GetElementPtrTypeIterator.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000023#include "Support/STLExtras.h"
Chris Lattner00950542001-06-06 20:29:01 +000024#include <list>
Chris Lattnerc188eeb2002-07-30 18:54:25 +000025#include <utility>
Chris Lattner30c89792001-09-07 16:35:17 +000026#include <algorithm>
Chris Lattner00950542001-06-06 20:29:01 +000027
Chris Lattner386a3b72001-10-16 19:54:17 +000028int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
Chris Lattner09083092001-07-08 04:57:15 +000029int yylex(); // declaration" of xxx warnings.
Chris Lattner00950542001-06-06 20:29:01 +000030int yyparse();
31
Brian Gaeked0fde302003-11-11 22:41:34 +000032namespace llvm {
33
Chris Lattner00950542001-06-06 20:29:01 +000034static Module *ParserResult;
Chris Lattner9232b992003-04-22 18:42:41 +000035std::string CurFilename;
Chris Lattner00950542001-06-06 20:29:01 +000036
Chris Lattner30c89792001-09-07 16:35:17 +000037// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
38// relating to upreferences in the input stream.
39//
40//#define DEBUG_UPREFS 1
41#ifdef DEBUG_UPREFS
Chris Lattner699f1eb2002-08-14 17:12:33 +000042#define UR_OUT(X) std::cerr << X
Chris Lattner30c89792001-09-07 16:35:17 +000043#else
44#define UR_OUT(X)
45#endif
46
Vikram S. Adved3f7eb02002-07-14 22:59:28 +000047#define YYERROR_VERBOSE 1
48
Chris Lattner99e7ab72003-10-18 05:53:13 +000049// HACK ALERT: This variable is used to implement the automatic conversion of
50// variable argument instructions from their old to new forms. When this
51// compatiblity "Feature" is removed, this should be too.
52//
53static BasicBlock *CurBB;
54static bool ObsoleteVarArgs;
55
56
Chris Lattner7e708292002-06-25 16:13:24 +000057// This contains info used when building the body of a function. It is
58// destroyed when the function is completed.
Chris Lattner00950542001-06-06 20:29:01 +000059//
Chris Lattner9232b992003-04-22 18:42:41 +000060typedef std::vector<Value *> ValueList; // Numbered defs
Chris Lattner16af11d2004-02-09 21:03:38 +000061static void ResolveDefinitions(std::map<unsigned,ValueList> &LateResolvers,
62 std::map<unsigned,ValueList> *FutureLateResolvers = 0);
Chris Lattner00950542001-06-06 20:29:01 +000063
64static struct PerModuleInfo {
65 Module *CurrentModule;
Chris Lattner16af11d2004-02-09 21:03:38 +000066 std::map<unsigned,ValueList> Values; // Module level numbered definitions
67 std::map<unsigned,ValueList> LateResolveValues;
68 std::vector<PATypeHolder> Types;
Chris Lattner9232b992003-04-22 18:42:41 +000069 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000070
Chris Lattner2079fde2001-10-13 06:41:08 +000071 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
72 // references to global values. Global values may be referenced before they
73 // are defined, and if so, the temporary object that they represent is held
Chris Lattnere9bb2df2001-12-03 22:26:30 +000074 // here. This is used for forward references of ConstantPointerRefs.
Chris Lattner2079fde2001-10-13 06:41:08 +000075 //
Chris Lattner9232b992003-04-22 18:42:41 +000076 typedef std::map<std::pair<const PointerType *,
Chris Lattnerbc207592004-03-08 06:09:57 +000077 ValID>, GlobalValue*> GlobalRefsType;
Chris Lattner2079fde2001-10-13 06:41:08 +000078 GlobalRefsType GlobalRefs;
79
Chris Lattner00950542001-06-06 20:29:01 +000080 void ModuleDone() {
Chris Lattner7e708292002-06-25 16:13:24 +000081 // If we could not resolve some functions at function compilation time
82 // (calls to functions before they are defined), resolve them now... Types
83 // are resolved when the constant pool has been completely parsed.
Chris Lattner30c89792001-09-07 16:35:17 +000084 //
Chris Lattner00950542001-06-06 20:29:01 +000085 ResolveDefinitions(LateResolveValues);
86
Chris Lattner2079fde2001-10-13 06:41:08 +000087 // Check to make sure that all global value forward references have been
88 // resolved!
89 //
90 if (!GlobalRefs.empty()) {
Chris Lattner9232b992003-04-22 18:42:41 +000091 std::string UndefinedReferences = "Unresolved global references exist:\n";
Chris Lattner749ce032002-03-11 22:12:39 +000092
93 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
94 I != E; ++I) {
95 UndefinedReferences += " " + I->first.first->getDescription() + " " +
96 I->first.second.getName() + "\n";
97 }
98 ThrowException(UndefinedReferences);
Chris Lattner2079fde2001-10-13 06:41:08 +000099 }
100
Chris Lattner7e708292002-06-25 16:13:24 +0000101 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000102 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +0000103 CurrentModule = 0;
104 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000105
106
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000107 // DeclareNewGlobalValue - Called every time a new GV has been defined. This
Chris Lattner2079fde2001-10-13 06:41:08 +0000108 // is used to remove things from the forward declaration map, resolving them
109 // to the correct thing as needed.
110 //
111 void DeclareNewGlobalValue(GlobalValue *GV, ValID D) {
112 // Check to see if there is a forward reference to this global variable...
113 // if there is, eliminate it and patch the reference to use the new def'n.
Chris Lattner9232b992003-04-22 18:42:41 +0000114 GlobalRefsType::iterator I =
115 GlobalRefs.find(std::make_pair(GV->getType(), D));
Chris Lattner2079fde2001-10-13 06:41:08 +0000116
117 if (I != GlobalRefs.end()) {
Chris Lattnerbc207592004-03-08 06:09:57 +0000118 GlobalValue *OldGV = I->second; // Get the placeholder...
Misha Brukman5560c9d2003-08-18 14:43:39 +0000119 I->first.second.destroy(); // Free string memory if necessary
Chris Lattner2079fde2001-10-13 06:41:08 +0000120
121 // Loop over all of the uses of the GlobalValue. The only thing they are
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000122 // allowed to be is ConstantPointerRef's.
Chris Lattnerfd059242003-10-15 16:48:29 +0000123 assert(OldGV->hasOneUse() && "Only one reference should exist!");
Chris Lattner9e932bd2002-10-14 03:28:42 +0000124 User *U = OldGV->use_back(); // Must be a ConstantPointerRef...
125 ConstantPointerRef *CPR = cast<ConstantPointerRef>(U);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000126
Chris Lattner9e932bd2002-10-14 03:28:42 +0000127 // Change the const pool reference to point to the real global variable
128 // now. This should drop a use from the OldGV.
Chris Lattnerbc207592004-03-08 06:09:57 +0000129 CPR->replaceUsesOfWithOnConstant(OldGV, GV);
Chris Lattner9e932bd2002-10-14 03:28:42 +0000130 assert(OldGV->use_empty() && "All uses should be gone now!");
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000131
132 // Remove OldGV from the module...
Chris Lattnerbc207592004-03-08 06:09:57 +0000133 if (GlobalVariable *GVar = dyn_cast<GlobalVariable>(OldGV))
134 CurrentModule->getGlobalList().erase(GVar);
135 else
136 CurrentModule->getFunctionList().erase(cast<Function>(OldGV));
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000137
Chris Lattner2079fde2001-10-13 06:41:08 +0000138 // Remove the map entry for the global now that it has been created...
139 GlobalRefs.erase(I);
140 }
141 }
142
Chris Lattner00950542001-06-06 20:29:01 +0000143} CurModule;
144
Chris Lattner79df7c02002-03-26 18:01:55 +0000145static struct PerFunctionInfo {
Chris Lattner7e708292002-06-25 16:13:24 +0000146 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner00950542001-06-06 20:29:01 +0000147
Chris Lattner16af11d2004-02-09 21:03:38 +0000148 std::map<unsigned,ValueList> Values; // Keep track of numbered definitions
149 std::map<unsigned,ValueList> LateResolveValues;
Chris Lattner9232b992003-04-22 18:42:41 +0000150 std::vector<PATypeHolder> Types;
151 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner15b69722003-11-12 04:40:30 +0000152 SymbolTable LocalSymtab;
Chris Lattner7e708292002-06-25 16:13:24 +0000153 bool isDeclare; // Is this function a forward declararation?
Chris Lattner00950542001-06-06 20:29:01 +0000154
Chris Lattner79df7c02002-03-26 18:01:55 +0000155 inline PerFunctionInfo() {
156 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000157 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000158 }
159
Chris Lattner79df7c02002-03-26 18:01:55 +0000160 inline void FunctionStart(Function *M) {
161 CurrentFunction = M;
Chris Lattner00950542001-06-06 20:29:01 +0000162 }
163
Chris Lattner79df7c02002-03-26 18:01:55 +0000164 void FunctionDone() {
Chris Lattner00950542001-06-06 20:29:01 +0000165 // If we could not resolve some blocks at parsing time (forward branches)
166 // resolve the branches now...
Chris Lattner386a3b72001-10-16 19:54:17 +0000167 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000168
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000169 // Make sure to resolve any constant expr references that might exist within
170 // the function we just declared itself.
171 ValID FID;
172 if (CurrentFunction->hasName()) {
173 FID = ValID::create((char*)CurrentFunction->getName().c_str());
174 } else {
175 unsigned Slot = CurrentFunction->getType()->getUniqueID();
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000176 // Figure out which slot number if is...
Chris Lattner16af11d2004-02-09 21:03:38 +0000177 ValueList &List = CurModule.Values[Slot];
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000178 for (unsigned i = 0; ; ++i) {
Chris Lattner16af11d2004-02-09 21:03:38 +0000179 assert(i < List.size() && "Function not found!");
180 if (List[i] == CurrentFunction) {
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000181 FID = ValID::create((int)i);
182 break;
183 }
184 }
185 }
186 CurModule.DeclareNewGlobalValue(CurrentFunction, FID);
187
Chris Lattner7e708292002-06-25 16:13:24 +0000188 Values.clear(); // Clear out function local definitions
Chris Lattner15b69722003-11-12 04:40:30 +0000189 Types.clear(); // Clear out function local types
190 LocalSymtab.clear(); // Clear out function local symbol table
Chris Lattner79df7c02002-03-26 18:01:55 +0000191 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000192 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000193 }
Chris Lattner394f2eb2003-10-16 20:12:13 +0000194} CurFun; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000195
Chris Lattner394f2eb2003-10-16 20:12:13 +0000196static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000197
Chris Lattner00950542001-06-06 20:29:01 +0000198
199//===----------------------------------------------------------------------===//
200// Code to handle definitions of all the types
201//===----------------------------------------------------------------------===//
202
Chris Lattner9232b992003-04-22 18:42:41 +0000203static int InsertValue(Value *D,
Chris Lattner16af11d2004-02-09 21:03:38 +0000204 std::map<unsigned,ValueList> &ValueTab = CurFun.Values) {
Chris Lattner2079fde2001-10-13 06:41:08 +0000205 if (D->hasName()) return -1; // Is this a numbered definition?
206
207 // Yes, insert the value into the value table...
208 unsigned type = D->getType()->getUniqueID();
Chris Lattner2079fde2001-10-13 06:41:08 +0000209 //printf("Values[%d][%d] = %d\n", type, ValueTab[type].size(), D);
Chris Lattner16af11d2004-02-09 21:03:38 +0000210 ValueList &List = ValueTab[type];
211 List.push_back(D);
212 return List.size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000213}
214
Chris Lattner30c89792001-09-07 16:35:17 +0000215// TODO: FIXME when Type are not const
Chris Lattner9232b992003-04-22 18:42:41 +0000216static void InsertType(const Type *Ty, std::vector<PATypeHolder> &Types) {
Chris Lattner30c89792001-09-07 16:35:17 +0000217 Types.push_back(Ty);
218}
219
220static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000221 switch (D.Type) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000222 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000223 unsigned Num = (unsigned)D.Num;
224
225 // Module constants occupy the lowest numbered slots...
226 if (Num < CurModule.Types.size())
227 return CurModule.Types[Num];
228
229 Num -= CurModule.Types.size();
230
231 // Check that the number is within bounds...
Chris Lattner394f2eb2003-10-16 20:12:13 +0000232 if (Num <= CurFun.Types.size())
233 return CurFun.Types[Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000234 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000235 }
Chris Lattnerf8dff732002-07-18 05:18:37 +0000236 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000237 std::string Name(D.Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000238 SymbolTable *SymTab = 0;
Chris Lattner6e6026b2002-11-20 18:36:02 +0000239 Value *N = 0;
240 if (inFunctionScope()) {
Chris Lattner394f2eb2003-10-16 20:12:13 +0000241 SymTab = &CurFun.CurrentFunction->getSymbolTable();
Reid Spencerb152f9f2004-05-25 17:29:21 +0000242 N = SymTab->lookupType(Name);
Chris Lattner6e6026b2002-11-20 18:36:02 +0000243 }
Chris Lattner30c89792001-09-07 16:35:17 +0000244
245 if (N == 0) {
Chris Lattner7e708292002-06-25 16:13:24 +0000246 // Symbol table doesn't automatically chain yet... because the function
Chris Lattner30c89792001-09-07 16:35:17 +0000247 // hasn't been added to the module...
248 //
Chris Lattner6e6026b2002-11-20 18:36:02 +0000249 SymTab = &CurModule.CurrentModule->getSymbolTable();
Reid Spencerb152f9f2004-05-25 17:29:21 +0000250 N = SymTab->lookupType(Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000251 if (N == 0) break;
252 }
253
254 D.destroy(); // Free old strdup'd memory...
Chris Lattner949a3622003-07-23 15:30:06 +0000255 return cast<Type>(N);
Chris Lattner30c89792001-09-07 16:35:17 +0000256 }
257 default:
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000258 ThrowException("Internal parser error: Invalid symbol type reference!");
Chris Lattner30c89792001-09-07 16:35:17 +0000259 }
260
261 // If we reached here, we referenced either a symbol that we don't know about
262 // or an id number that hasn't been read yet. We may be referencing something
263 // forward, so just create an entry to be resolved later and get to it...
264 //
265 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
266
Chris Lattner9232b992003-04-22 18:42:41 +0000267 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000268 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
Chris Lattner4a42e902001-10-22 05:56:09 +0000269
Chris Lattner9232b992003-04-22 18:42:41 +0000270 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000271 if (I != LateResolver.end()) {
272 return I->second;
273 }
Chris Lattner30c89792001-09-07 16:35:17 +0000274
Chris Lattner82269592001-10-22 06:01:08 +0000275 Type *Typ = OpaqueType::get();
Chris Lattner9232b992003-04-22 18:42:41 +0000276 LateResolver.insert(std::make_pair(D, Typ));
Chris Lattner30c89792001-09-07 16:35:17 +0000277 return Typ;
278}
279
Chris Lattner9232b992003-04-22 18:42:41 +0000280static Value *lookupInSymbolTable(const Type *Ty, const std::string &Name) {
Chris Lattner6e6026b2002-11-20 18:36:02 +0000281 SymbolTable &SymTab =
Chris Lattner394f2eb2003-10-16 20:12:13 +0000282 inFunctionScope() ? CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner9705a152002-05-02 19:27:42 +0000283 CurModule.CurrentModule->getSymbolTable();
Chris Lattner6e6026b2002-11-20 18:36:02 +0000284 return SymTab.lookup(Ty, Name);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000285}
286
Chris Lattner2079fde2001-10-13 06:41:08 +0000287// getValNonImprovising - Look up the value specified by the provided type and
288// the provided ValID. If the value exists and has already been defined, return
289// it. Otherwise return null.
290//
291static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000292 if (isa<FunctionType>(Ty))
293 ThrowException("Functions are not values and "
294 "must be referenced as pointers");
Chris Lattner386a3b72001-10-16 19:54:17 +0000295
Chris Lattner30c89792001-09-07 16:35:17 +0000296 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000297 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000298 unsigned type = Ty->getUniqueID();
Chris Lattner00950542001-06-06 20:29:01 +0000299 unsigned Num = (unsigned)D.Num;
300
301 // Module constants occupy the lowest numbered slots...
Chris Lattner16af11d2004-02-09 21:03:38 +0000302 std::map<unsigned,ValueList>::iterator VI = CurModule.Values.find(type);
303 if (VI != CurModule.Values.end()) {
304 if (Num < VI->second.size())
305 return VI->second[Num];
306 Num -= VI->second.size();
Chris Lattner00950542001-06-06 20:29:01 +0000307 }
308
309 // Make sure that our type is within bounds
Chris Lattner16af11d2004-02-09 21:03:38 +0000310 VI = CurFun.Values.find(type);
311 if (VI == CurFun.Values.end()) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000312
313 // Check that the number is within bounds...
Chris Lattner16af11d2004-02-09 21:03:38 +0000314 if (VI->second.size() <= Num) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000315
Chris Lattner16af11d2004-02-09 21:03:38 +0000316 return VI->second[Num];
Chris Lattner00950542001-06-06 20:29:01 +0000317 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000318
Chris Lattner1a1cb112001-09-30 22:46:54 +0000319 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000320 Value *N = lookupInSymbolTable(Ty, std::string(D.Name));
Chris Lattner2079fde2001-10-13 06:41:08 +0000321 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000322
323 D.destroy(); // Free old strdup'd memory...
324 return N;
325 }
326
Chris Lattner2079fde2001-10-13 06:41:08 +0000327 // Check to make sure that "Ty" is an integral type, and that our
328 // value will fit into the specified type...
329 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattnerd78700d2002-08-16 21:14:40 +0000330 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
331 ThrowException("Signed integral constant '" +
332 itostr(D.ConstPool64) + "' is invalid for type '" +
333 Ty->getDescription() + "'!");
334 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000335
336 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000337 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
338 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000339 ThrowException("Integral constant '" + utostr(D.UConstPool64) +
340 "' is invalid or out of range!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000341 } else { // This is really a signed reference. Transmogrify.
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000342 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000343 }
344 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000345 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000346 }
347
Chris Lattner2079fde2001-10-13 06:41:08 +0000348 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000349 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner2079fde2001-10-13 06:41:08 +0000350 ThrowException("FP constant invalid for type!!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000351 return ConstantFP::get(Ty, D.ConstPoolFP);
Chris Lattner2079fde2001-10-13 06:41:08 +0000352
353 case ValID::ConstNullVal: // Is it a null value?
Chris Lattner9b625032002-05-06 16:15:30 +0000354 if (!isa<PointerType>(Ty))
Chris Lattner2079fde2001-10-13 06:41:08 +0000355 ThrowException("Cannot create a a non pointer null!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000356 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner2079fde2001-10-13 06:41:08 +0000357
Chris Lattnerd78700d2002-08-16 21:14:40 +0000358 case ValID::ConstantVal: // Fully resolved constant?
359 if (D.ConstantValue->getType() != Ty)
360 ThrowException("Constant expression type different from required type!");
361 return D.ConstantValue;
362
Chris Lattner30c89792001-09-07 16:35:17 +0000363 default:
364 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000365 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000366 } // End of switch
367
Chris Lattner2079fde2001-10-13 06:41:08 +0000368 assert(0 && "Unhandled case!");
369 return 0;
370}
371
372
373// getVal - This function is identical to getValNonImprovising, except that if a
374// value is not already defined, it "improvises" by creating a placeholder var
375// that looks and acts just like the requested variable. When the value is
376// defined later, all uses of the placeholder variable are replaced with the
377// real thing.
378//
379static Value *getVal(const Type *Ty, const ValID &D) {
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 Lattner42fbc7e2004-05-26 17:08:25 +0000518 Value *Existing;
519 // FIXME: this is really gross
520 if (V->getType() != Type::TypeTy)
521 Existing = ST.lookup(V->getType(), Name);
522 else
523 Existing = ST.lookupType(Name);
524
Chris Lattner30c89792001-09-07 16:35:17 +0000525 if (Existing) { // Inserting a name that is already defined???
526 // There is only one case where this is allowed: when we are refining an
527 // opaque type. In this case, Existing will be an opaque type.
Chris Lattner949a3622003-07-23 15:30:06 +0000528 if (const Type *Ty = dyn_cast<Type>(Existing)) {
Chris Lattner51727be2002-06-04 21:58:56 +0000529 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Ty)) {
Chris Lattner30c89792001-09-07 16:35:17 +0000530 // We ARE replacing an opaque type!
Chris Lattner51727be2002-06-04 21:58:56 +0000531 ((OpaqueType*)OpTy)->refineAbstractTypeTo(cast<Type>(V));
Chris Lattnerb7474512001-10-03 15:39:04 +0000532 return true;
Chris Lattner30c89792001-09-07 16:35:17 +0000533 }
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000534 }
Chris Lattner30c89792001-09-07 16:35:17 +0000535
Chris Lattner9636a912001-10-01 16:18:37 +0000536 // Otherwise, we are a simple redefinition of a value, check to see if it
537 // is defined the same as the old one...
Chris Lattneradf99702003-03-03 23:28:55 +0000538 if (const Type *Ty = dyn_cast<Type>(Existing)) {
539 if (Ty == cast<Type>(V)) return true; // Yes, it's equal.
Chris Lattner699f1eb2002-08-14 17:12:33 +0000540 // std::cerr << "Type: " << Ty->getDescription() << " != "
Chris Lattner949a3622003-07-23 15:30:06 +0000541 // << cast<Type>(V)->getDescription() << "!\n";
Chris Lattneradf99702003-03-03 23:28:55 +0000542 } else if (const Constant *C = dyn_cast<Constant>(Existing)) {
543 if (C == V) return true; // Constants are equal to themselves
Chris Lattnerb7474512001-10-03 15:39:04 +0000544 } else if (GlobalVariable *EGV = dyn_cast<GlobalVariable>(Existing)) {
Chris Lattner43efcbf2001-10-03 19:35:57 +0000545 // We are allowed to redefine a global variable in two circumstances:
546 // 1. If at least one of the globals is uninitialized or
547 // 2. If both initializers have the same value.
548 //
Chris Lattner89219832001-10-03 19:35:04 +0000549 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000550 if (!EGV->hasInitializer() || !GV->hasInitializer() ||
551 EGV->getInitializer() == GV->getInitializer()) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000552
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000553 // Make sure the existing global version gets the initializer! Make
554 // sure that it also gets marked const if the new version is.
Chris Lattner89219832001-10-03 19:35:04 +0000555 if (GV->hasInitializer() && !EGV->hasInitializer())
556 EGV->setInitializer(GV->getInitializer());
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000557 if (GV->isConstant())
558 EGV->setConstant(true);
Chris Lattner4ad02e72003-04-16 20:28:45 +0000559 EGV->setLinkage(GV->getLinkage());
Chris Lattner89219832001-10-03 19:35:04 +0000560
Chris Lattner2079fde2001-10-13 06:41:08 +0000561 delete GV; // Destroy the duplicate!
Chris Lattner89219832001-10-03 19:35:04 +0000562 return true; // They are equivalent!
563 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000564 }
Chris Lattner9636a912001-10-01 16:18:37 +0000565 }
Chris Lattner0b268fb2003-11-25 03:54:16 +0000566
Chris Lattner2079fde2001-10-13 06:41:08 +0000567 ThrowException("Redefinition of value named '" + Name + "' in the '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000568 V->getType()->getDescription() + "' type plane!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000569 }
Chris Lattner00950542001-06-06 20:29:01 +0000570
Chris Lattner15b69722003-11-12 04:40:30 +0000571 // Set the name
Chris Lattner6e6026b2002-11-20 18:36:02 +0000572 V->setName(Name, &ST);
Chris Lattner15b69722003-11-12 04:40:30 +0000573
574 // If we're in function scope
575 if (inFunctionScope()) {
576 // Look up the symbol in the function's local symboltable
577 Existing = CurFun.LocalSymtab.lookup(V->getType(),Name);
578
579 // If it already exists
580 if (Existing) {
Chris Lattner15b69722003-11-12 04:40:30 +0000581 // Bail
582 ThrowException("Redefinition of value named '" + Name + "' in the '" +
583 V->getType()->getDescription() + "' type plane!");
584
585 // otherwise, since it doesn't exist
586 } else {
587 // Insert it.
588 CurFun.LocalSymtab.insert(V);
589 }
590 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000591 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000592}
593
Chris Lattner8896eda2001-07-09 19:38:36 +0000594
Chris Lattner30c89792001-09-07 16:35:17 +0000595//===----------------------------------------------------------------------===//
596// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000597//
Chris Lattner8896eda2001-07-09 19:38:36 +0000598
Chris Lattner3b073862004-02-09 03:19:29 +0000599// TypeContains - Returns true if Ty directly contains E in it.
Chris Lattner30c89792001-09-07 16:35:17 +0000600//
601static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3b073862004-02-09 03:19:29 +0000602 return find(Ty->subtype_begin(), Ty->subtype_end(), E) != Ty->subtype_end();
603}
604
605namespace {
606 struct UpRefRecord {
607 // NestingLevel - The number of nesting levels that need to be popped before
608 // this type is resolved.
609 unsigned NestingLevel;
610
611 // LastContainedTy - This is the type at the current binding level for the
612 // type. Every time we reduce the nesting level, this gets updated.
613 const Type *LastContainedTy;
614
615 // UpRefTy - This is the actual opaque type that the upreference is
616 // represented with.
617 OpaqueType *UpRefTy;
618
619 UpRefRecord(unsigned NL, OpaqueType *URTy)
620 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
621 };
Chris Lattner30c89792001-09-07 16:35:17 +0000622}
Chris Lattner698b56e2001-07-20 19:15:08 +0000623
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000624// UpRefs - A list of the outstanding upreferences that need to be resolved.
Chris Lattner3b073862004-02-09 03:19:29 +0000625static std::vector<UpRefRecord> UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000626
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000627/// HandleUpRefs - Every time we finish a new layer of types, this function is
628/// called. It loops through the UpRefs vector, which is a list of the
629/// currently active types. For each type, if the up reference is contained in
630/// the newly completed type, we decrement the level count. When the level
631/// count reaches zero, the upreferenced type is the type that is passed in:
632/// thus we can complete the cycle.
633///
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000634static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner2c37c182004-02-09 03:03:10 +0000635 if (!ty->isAbstract()) return ty;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000636 PATypeHolder Ty(ty);
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000637 UR_OUT("Type '" << Ty->getDescription() <<
Chris Lattner5084d032001-11-02 07:46:26 +0000638 "' newly formed. Resolving upreferences.\n" <<
639 UpRefs.size() << " upreferences active!\n");
Chris Lattner026a8ce2004-02-09 18:53:54 +0000640
641 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
642 // to zero), we resolve them all together before we resolve them to Ty. At
643 // the end of the loop, if there is anything to resolve to Ty, it will be in
644 // this variable.
645 OpaqueType *TypeToResolve = 0;
646
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000647 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Chris Lattner5084d032001-11-02 07:46:26 +0000648 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Chris Lattner30c89792001-09-07 16:35:17 +0000649 << UpRefs[i].second->getDescription() << ") = "
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000650 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner3b073862004-02-09 03:19:29 +0000651 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
652 // Decrement level of upreference
653 unsigned Level = --UpRefs[i].NestingLevel;
654 UpRefs[i].LastContainedTy = Ty;
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000655 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Chris Lattner30c89792001-09-07 16:35:17 +0000656 if (Level == 0) { // Upreference should be resolved!
Chris Lattner026a8ce2004-02-09 18:53:54 +0000657 if (!TypeToResolve) {
658 TypeToResolve = UpRefs[i].UpRefTy;
659 } else {
660 UR_OUT(" * Resolving upreference for "
661 << UpRefs[i].second->getDescription() << "\n";
662 std::string OldName = UpRefs[i].UpRefTy->getDescription());
663 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
664 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
665 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
666 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000667 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
668 --i; // Do not skip the next element...
Chris Lattner30c89792001-09-07 16:35:17 +0000669 }
670 }
Chris Lattner8896eda2001-07-09 19:38:36 +0000671 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000672
Chris Lattner026a8ce2004-02-09 18:53:54 +0000673 if (TypeToResolve) {
674 UR_OUT(" * Resolving upreference for "
675 << UpRefs[i].second->getDescription() << "\n";
Chris Lattner16af11d2004-02-09 21:03:38 +0000676 std::string OldName = TypeToResolve->getDescription());
Chris Lattner026a8ce2004-02-09 18:53:54 +0000677 TypeToResolve->refineAbstractTypeTo(Ty);
678 }
679
Chris Lattner8896eda2001-07-09 19:38:36 +0000680 return Ty;
681}
682
Chris Lattner30c89792001-09-07 16:35:17 +0000683
Chris Lattner00950542001-06-06 20:29:01 +0000684//===----------------------------------------------------------------------===//
685// RunVMAsmParser - Define an interface to this parser
686//===----------------------------------------------------------------------===//
687//
Chris Lattner9232b992003-04-22 18:42:41 +0000688Module *RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000689 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000690 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000691 llvmAsmlineno = 1; // Reset the current line number...
Chris Lattner99e7ab72003-10-18 05:53:13 +0000692 ObsoleteVarArgs = false;
Chris Lattner00950542001-06-06 20:29:01 +0000693
Chris Lattner75f20532003-04-22 18:02:52 +0000694 // Allocate a new module to read
695 CurModule.CurrentModule = new Module(Filename);
Chris Lattner42570982003-11-26 07:24:58 +0000696
697 try {
698 yyparse(); // Parse the file.
699 } catch (...) {
700 // Clear the symbol table so it doesn't complain when it
701 // gets destructed
702 CurFun.LocalSymtab.clear();
703 throw;
704 }
Chris Lattner99e7ab72003-10-18 05:53:13 +0000705
Chris Lattner00950542001-06-06 20:29:01 +0000706 Module *Result = ParserResult;
Chris Lattner99e7ab72003-10-18 05:53:13 +0000707
708 // Check to see if they called va_start but not va_arg..
709 if (!ObsoleteVarArgs)
710 if (Function *F = Result->getNamedFunction("llvm.va_start"))
711 if (F->asize() == 1) {
712 std::cerr << "WARNING: this file uses obsolete features. "
713 << "Assemble and disassemble to update it.\n";
714 ObsoleteVarArgs = true;
715 }
716
717
718 if (ObsoleteVarArgs) {
719 // If the user is making use of obsolete varargs intrinsics, adjust them for
720 // the user.
721 if (Function *F = Result->getNamedFunction("llvm.va_start")) {
722 assert(F->asize() == 1 && "Obsolete va_start takes 1 argument!");
723
724 const Type *RetTy = F->getFunctionType()->getParamType(0);
725 RetTy = cast<PointerType>(RetTy)->getElementType();
726 Function *NF = Result->getOrInsertFunction("llvm.va_start", RetTy, 0);
727
728 while (!F->use_empty()) {
729 CallInst *CI = cast<CallInst>(F->use_back());
730 Value *V = new CallInst(NF, "", CI);
731 new StoreInst(V, CI->getOperand(1), CI);
732 CI->getParent()->getInstList().erase(CI);
733 }
734 Result->getFunctionList().erase(F);
735 }
736
737 if (Function *F = Result->getNamedFunction("llvm.va_end")) {
738 assert(F->asize() == 1 && "Obsolete va_end takes 1 argument!");
739 const Type *ArgTy = F->getFunctionType()->getParamType(0);
740 ArgTy = cast<PointerType>(ArgTy)->getElementType();
741 Function *NF = Result->getOrInsertFunction("llvm.va_end", Type::VoidTy,
742 ArgTy, 0);
743
744 while (!F->use_empty()) {
745 CallInst *CI = cast<CallInst>(F->use_back());
746 Value *V = new LoadInst(CI->getOperand(1), "", CI);
747 new CallInst(NF, V, "", CI);
748 CI->getParent()->getInstList().erase(CI);
749 }
750 Result->getFunctionList().erase(F);
751 }
752
753 if (Function *F = Result->getNamedFunction("llvm.va_copy")) {
754 assert(F->asize() == 2 && "Obsolete va_copy takes 2 argument!");
755 const Type *ArgTy = F->getFunctionType()->getParamType(0);
756 ArgTy = cast<PointerType>(ArgTy)->getElementType();
757 Function *NF = Result->getOrInsertFunction("llvm.va_copy", ArgTy,
758 ArgTy, 0);
759
760 while (!F->use_empty()) {
761 CallInst *CI = cast<CallInst>(F->use_back());
762 Value *V = new CallInst(NF, CI->getOperand(2), "", CI);
763 new StoreInst(V, CI->getOperand(1), CI);
764 CI->getParent()->getInstList().erase(CI);
765 }
766 Result->getFunctionList().erase(F);
767 }
768 }
769
Chris Lattner00950542001-06-06 20:29:01 +0000770 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
771 ParserResult = 0;
772
773 return Result;
774}
775
Brian Gaeked0fde302003-11-11 22:41:34 +0000776} // End llvm namespace
777
778using namespace llvm;
779
Chris Lattner00950542001-06-06 20:29:01 +0000780%}
781
782%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000783 llvm::Module *ModuleVal;
784 llvm::Function *FunctionVal;
785 std::pair<llvm::PATypeHolder*, char*> *ArgVal;
786 llvm::BasicBlock *BasicBlockVal;
787 llvm::TerminatorInst *TermInstVal;
788 llvm::Instruction *InstVal;
789 llvm::Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000790
Brian Gaeked0fde302003-11-11 22:41:34 +0000791 const llvm::Type *PrimType;
792 llvm::PATypeHolder *TypeVal;
793 llvm::Value *ValueVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000794
Brian Gaeked0fde302003-11-11 22:41:34 +0000795 std::vector<std::pair<llvm::PATypeHolder*,char*> > *ArgList;
796 std::vector<llvm::Value*> *ValueList;
797 std::list<llvm::PATypeHolder> *TypeList;
798 std::list<std::pair<llvm::Value*,
799 llvm::BasicBlock*> > *PHIList; // Represent the RHS of PHI node
800 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
801 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000802
Brian Gaeked0fde302003-11-11 22:41:34 +0000803 llvm::GlobalValue::LinkageTypes Linkage;
Chris Lattner30c89792001-09-07 16:35:17 +0000804 int64_t SInt64Val;
805 uint64_t UInt64Val;
806 int SIntVal;
807 unsigned UIntVal;
808 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000809 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000810
Chris Lattner30c89792001-09-07 16:35:17 +0000811 char *StrVal; // This memory is strdup'd!
Brian Gaeked0fde302003-11-11 22:41:34 +0000812 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000813
Brian Gaeked0fde302003-11-11 22:41:34 +0000814 llvm::Instruction::BinaryOps BinaryOpVal;
815 llvm::Instruction::TermOps TermOpVal;
816 llvm::Instruction::MemoryOps MemOpVal;
817 llvm::Instruction::OtherOps OtherOpVal;
818 llvm::Module::Endianness Endianness;
Chris Lattner00950542001-06-06 20:29:01 +0000819}
820
Chris Lattner79df7c02002-03-26 18:01:55 +0000821%type <ModuleVal> Module FunctionList
822%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000823%type <BasicBlockVal> BasicBlock InstructionList
824%type <TermInstVal> BBTerminatorInst
825%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000826%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000827%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000828%type <ArgList> ArgList ArgListH
829%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000830%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000831%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000832%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000833%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000834%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +0000835%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner15c9c032003-09-08 18:20:29 +0000836%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattner4ad02e72003-04-16 20:28:45 +0000837%type <Linkage> OptLinkage
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000838%type <Endianness> BigOrLittle
Chris Lattner00950542001-06-06 20:29:01 +0000839
Chris Lattner2079fde2001-10-13 06:41:08 +0000840// ValueRef - Unresolved reference to a definition or BB
841%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000842%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000843// Tokens and types for handling constant integer values
844//
845// ESINT64VAL - A negative number within long long range
846%token <SInt64Val> ESINT64VAL
847
848// EUINT64VAL - A positive number within uns. long long range
849%token <UInt64Val> EUINT64VAL
850%type <SInt64Val> EINT64VAL
851
852%token <SIntVal> SINTVAL // Signed 32 bit ints...
853%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
854%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000855%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000856
857// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000858%type <TypeVal> Types TypesV UpRTypes UpRTypesV
859%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000860%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
861%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000862
Chris Lattner6c23f572003-08-22 05:42:10 +0000863%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
864%type <StrVal> Name OptName OptAssign
Chris Lattner00950542001-06-06 20:29:01 +0000865
866
Chris Lattner91ef4602004-03-31 03:49:47 +0000867%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Chris Lattner15c9c032003-09-08 18:20:29 +0000868%token DECLARE GLOBAL CONSTANT VOLATILE
Chris Lattnera58e3a12004-02-08 21:48:25 +0000869%token TO DOTDOTDOT NULL_TOK CONST INTERNAL LINKONCE WEAK APPENDING
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000870%token OPAQUE NOT EXTERNAL TARGET ENDIAN POINTERSIZE LITTLE BIG
Chris Lattner00950542001-06-06 20:29:01 +0000871
872// Basic Block Terminating Operators
Chris Lattner36143fc2003-09-08 18:54:55 +0000873%token <TermOpVal> RET BR SWITCH INVOKE UNWIND
Chris Lattner00950542001-06-06 20:29:01 +0000874
Chris Lattner00950542001-06-06 20:29:01 +0000875// Binary Operators
876%type <BinaryOpVal> BinaryOps // all the binary operators
Chris Lattner4a6482b2002-09-10 19:57:26 +0000877%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattner42c9e772001-10-20 09:32:59 +0000878%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000879%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000880
881// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000882%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000883
Chris Lattner027dcc52001-07-08 21:10:27 +0000884// Other Operators
885%type <OtherOpVal> ShiftOps
Chris Lattner76f0ff32004-03-12 05:51:36 +0000886%token <OtherOpVal> PHI_TOK CALL CAST SELECT SHL SHR VAARG VANEXT
Chris Lattner99e7ab72003-10-18 05:53:13 +0000887%token VA_ARG // FIXME: OBSOLETE
Chris Lattner027dcc52001-07-08 21:10:27 +0000888
Chris Lattner00950542001-06-06 20:29:01 +0000889%start Module
890%%
891
892// Handle constant integer size restriction and conversion...
893//
Chris Lattner51727be2002-06-04 21:58:56 +0000894INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +0000895INTVAL : UINTVAL {
896 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
897 ThrowException("Value too large for type!");
898 $$ = (int32_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000899};
Chris Lattner00950542001-06-06 20:29:01 +0000900
901
Chris Lattner51727be2002-06-04 21:58:56 +0000902EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +0000903EINT64VAL : EUINT64VAL {
904 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
905 ThrowException("Value too large for type!");
906 $$ = (int64_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000907};
Chris Lattner00950542001-06-06 20:29:01 +0000908
Chris Lattner00950542001-06-06 20:29:01 +0000909// Operations that are notably excluded from this list include:
910// RET, BR, & SWITCH because they end basic blocks and are treated specially.
911//
Chris Lattner4a6482b2002-09-10 19:57:26 +0000912ArithmeticOps: ADD | SUB | MUL | DIV | REM;
913LogicalOps : AND | OR | XOR;
914SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
915BinaryOps : ArithmeticOps | LogicalOps | SetCondOps;
916
Chris Lattner51727be2002-06-04 21:58:56 +0000917ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +0000918
Chris Lattnere98dda62001-07-14 06:10:16 +0000919// These are some types that allow classification if we only want a particular
920// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +0000921SIntType : LONG | INT | SHORT | SBYTE;
922UIntType : ULONG | UINT | USHORT | UBYTE;
923IntType : SIntType | UIntType;
924FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +0000925
Chris Lattnere98dda62001-07-14 06:10:16 +0000926// OptAssign - Value producing statements have an optional assignment component
Chris Lattner6c23f572003-08-22 05:42:10 +0000927OptAssign : Name '=' {
Chris Lattner00950542001-06-06 20:29:01 +0000928 $$ = $1;
929 }
930 | /*empty*/ {
931 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +0000932 };
Chris Lattner00950542001-06-06 20:29:01 +0000933
Chris Lattner4ad02e72003-04-16 20:28:45 +0000934OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
935 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
Chris Lattner72ac148d2003-10-16 18:29:00 +0000936 WEAK { $$ = GlobalValue::WeakLinkage; } |
Chris Lattner4ad02e72003-04-16 20:28:45 +0000937 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
938 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattner30c89792001-09-07 16:35:17 +0000939
940//===----------------------------------------------------------------------===//
941// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +0000942// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +0000943// access to it, a user must explicitly use TypesV.
944//
945
946// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +0000947TypesV : Types | VOID { $$ = new PATypeHolder($1); };
948UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +0000949
950Types : UpRTypes {
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000951 if (!UpRefs.empty())
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000952 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
953 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000954 };
Chris Lattner30c89792001-09-07 16:35:17 +0000955
956
957// Derived types are added later...
958//
Chris Lattner51727be2002-06-04 21:58:56 +0000959PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
960PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000961UpRTypes : OPAQUE {
962 $$ = new PATypeHolder(OpaqueType::get());
963 }
964 | PrimType {
965 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +0000966 };
Chris Lattnerd78700d2002-08-16 21:14:40 +0000967UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000968 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +0000969};
Chris Lattner30c89792001-09-07 16:35:17 +0000970
Chris Lattner30c89792001-09-07 16:35:17 +0000971// Include derived types in the Types production.
972//
973UpRTypes : '\\' EUINT64VAL { // Type UpReference
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000974 if ($2 > (uint64_t)~0U) ThrowException("Value out of range!");
Chris Lattner30c89792001-09-07 16:35:17 +0000975 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3b073862004-02-09 03:19:29 +0000976 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000977 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +0000978 UR_OUT("New Upreference!\n");
979 }
Chris Lattner79df7c02002-03-26 18:01:55 +0000980 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner9232b992003-04-22 18:42:41 +0000981 std::vector<const Type*> Params;
Chris Lattner697954c2002-01-20 22:54:45 +0000982 mapto($3->begin(), $3->end(), std::back_inserter(Params),
Chris Lattner876dc572003-10-02 19:00:34 +0000983 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner2079fde2001-10-13 06:41:08 +0000984 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
985 if (isVarArg) Params.pop_back();
986
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000987 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +0000988 delete $3; // Delete the argument list
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000989 delete $1; // Delete the return type handle
Chris Lattner30c89792001-09-07 16:35:17 +0000990 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000991 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000992 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000993 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +0000994 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000995 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +0000996 std::vector<const Type*> Elements;
Chris Lattner697954c2002-01-20 22:54:45 +0000997 mapto($2->begin(), $2->end(), std::back_inserter(Elements),
Chris Lattner876dc572003-10-02 19:00:34 +0000998 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner30c89792001-09-07 16:35:17 +0000999
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001000 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001001 delete $2;
1002 }
1003 | '{' '}' { // Empty structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001004 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001005 }
1006 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001007 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001008 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001009 };
Chris Lattner30c89792001-09-07 16:35:17 +00001010
Chris Lattner7e708292002-06-25 16:13:24 +00001011// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +00001012// declaration type lists
1013//
1014TypeListI : UpRTypes {
Chris Lattner9232b992003-04-22 18:42:41 +00001015 $$ = new std::list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +00001016 $$->push_back(*$1); delete $1;
1017 }
1018 | TypeListI ',' UpRTypes {
1019 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +00001020 };
Chris Lattner30c89792001-09-07 16:35:17 +00001021
Chris Lattner7e708292002-06-25 16:13:24 +00001022// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +00001023ArgTypeListI : TypeListI
1024 | TypeListI ',' DOTDOTDOT {
1025 ($$=$1)->push_back(Type::VoidTy);
1026 }
1027 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001028 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +00001029 }
1030 | /*empty*/ {
Chris Lattner9232b992003-04-22 18:42:41 +00001031 $$ = new std::list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +00001032 };
Chris Lattner30c89792001-09-07 16:35:17 +00001033
Chris Lattnere98dda62001-07-14 06:10:16 +00001034// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +00001035// production is used ONLY to represent constants that show up AFTER a 'const',
1036// 'constant' or 'global' token at global scope. Constants that can be inlined
1037// into other expressions (such as integers and constexprs) are handled by the
1038// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +00001039//
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001040ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Chris Lattner949a3622003-07-23 15:30:06 +00001041 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001042 if (ATy == 0)
1043 ThrowException("Cannot make array constant with type: '" +
1044 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001045 const Type *ETy = ATy->getElementType();
1046 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +00001047
Chris Lattner30c89792001-09-07 16:35:17 +00001048 // Verify that we have the correct size...
1049 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +00001050 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +00001051 utostr($3->size()) + " arguments, but has size of " +
1052 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +00001053
Chris Lattner30c89792001-09-07 16:35:17 +00001054 // Verify all elements are correct type!
1055 for (unsigned i = 0; i < $3->size(); i++) {
1056 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +00001057 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001058 ETy->getDescription() +"' as required!\nIt is of type '"+
1059 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +00001060 }
1061
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001062 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001063 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001064 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001065 | Types '[' ']' {
Chris Lattner949a3622003-07-23 15:30:06 +00001066 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001067 if (ATy == 0)
1068 ThrowException("Cannot make array constant with type: '" +
1069 (*$1)->getDescription() + "'!");
1070
1071 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +00001072 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +00001073 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +00001074 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattner9232b992003-04-22 18:42:41 +00001075 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +00001076 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001077 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001078 | Types 'c' STRINGCONSTANT {
Chris Lattner949a3622003-07-23 15:30:06 +00001079 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001080 if (ATy == 0)
1081 ThrowException("Cannot make array constant with type: '" +
1082 (*$1)->getDescription() + "'!");
1083
Chris Lattner30c89792001-09-07 16:35:17 +00001084 int NumElements = ATy->getNumElements();
1085 const Type *ETy = ATy->getElementType();
1086 char *EndStr = UnEscapeLexed($3, true);
1087 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +00001088 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +00001089 itostr((int)(EndStr-$3)) +
1090 " when array has size " + itostr(NumElements) + "!");
Chris Lattner9232b992003-04-22 18:42:41 +00001091 std::vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +00001092 if (ETy == Type::SByteTy) {
1093 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001094 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +00001095 } else if (ETy == Type::UByteTy) {
1096 for (char *C = $3; C != EndStr; ++C)
Chris Lattnerbae362f2003-01-30 22:24:26 +00001097 Vals.push_back(ConstantUInt::get(ETy, (unsigned char)*C));
Chris Lattner93750fa2001-07-28 17:48:55 +00001098 } else {
Chris Lattner30c89792001-09-07 16:35:17 +00001099 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +00001100 ThrowException("Cannot build string arrays of non byte sized elements!");
1101 }
Chris Lattner30c89792001-09-07 16:35:17 +00001102 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001103 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +00001104 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001105 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001106 | Types '{' ConstVector '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001107 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001108 if (STy == 0)
1109 ThrowException("Cannot make struct constant with type: '" +
1110 (*$1)->getDescription() + "'!");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001111
Chris Lattnerc6212f12003-05-21 16:06:56 +00001112 if ($3->size() != STy->getNumContainedTypes())
1113 ThrowException("Illegal number of initializers for structure type!");
1114
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001115 // Check to ensure that constants are compatible with the type initializer!
1116 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Chris Lattnerd21cd802004-02-09 04:37:31 +00001117 if ((*$3)[i]->getType() != STy->getElementType(i))
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001118 ThrowException("Expected type '" +
Chris Lattnerd21cd802004-02-09 04:37:31 +00001119 STy->getElementType(i)->getDescription() +
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001120 "' for element #" + utostr(i) +
1121 " of structure initializer!");
1122
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001123 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001124 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001125 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001126 | Types '{' '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001127 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001128 if (STy == 0)
1129 ThrowException("Cannot make struct constant with type: '" +
1130 (*$1)->getDescription() + "'!");
1131
1132 if (STy->getNumContainedTypes() != 0)
1133 ThrowException("Illegal number of initializers for structure type!");
1134
1135 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1136 delete $1;
1137 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001138 | Types NULL_TOK {
Chris Lattner949a3622003-07-23 15:30:06 +00001139 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001140 if (PTy == 0)
1141 ThrowException("Cannot make null pointer constant with type: '" +
1142 (*$1)->getDescription() + "'!");
1143
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001144 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001145 delete $1;
1146 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001147 | Types SymbolicValueRef {
Chris Lattner949a3622003-07-23 15:30:06 +00001148 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001149 if (Ty == 0)
1150 ThrowException("Global const reference must be a pointer type!");
1151
Chris Lattner3101c252002-08-15 17:58:33 +00001152 // ConstExprs can exist in the body of a function, thus creating
1153 // ConstantPointerRefs whenever they refer to a variable. Because we are in
1154 // the context of a function, getValNonImprovising will search the functions
1155 // symbol table instead of the module symbol table for the global symbol,
1156 // which throws things all off. To get around this, we just tell
1157 // getValNonImprovising that we are at global scope here.
1158 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001159 Function *SavedCurFn = CurFun.CurrentFunction;
1160 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001161
Chris Lattner2079fde2001-10-13 06:41:08 +00001162 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001163
Chris Lattner394f2eb2003-10-16 20:12:13 +00001164 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001165
Chris Lattner2079fde2001-10-13 06:41:08 +00001166 // If this is an initializer for a constant pointer, which is referencing a
1167 // (currently) undefined variable, create a stub now that shall be replaced
1168 // in the future with the right type of variable.
1169 //
1170 if (V == 0) {
1171 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1172 const PointerType *PT = cast<PointerType>(Ty);
1173
1174 // First check to see if the forward references value is already created!
1175 PerModuleInfo::GlobalRefsType::iterator I =
Chris Lattner9232b992003-04-22 18:42:41 +00001176 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001177
1178 if (I != CurModule.GlobalRefs.end()) {
1179 V = I->second; // Placeholder already exists, use it...
Chris Lattnera989b232003-12-23 20:05:15 +00001180 $2.destroy();
Chris Lattner2079fde2001-10-13 06:41:08 +00001181 } else {
Chris Lattner2079fde2001-10-13 06:41:08 +00001182 // Create a placeholder for the global variable reference...
Chris Lattner7a176752001-12-04 00:03:30 +00001183 GlobalVariable *GV = new GlobalVariable(PT->getElementType(),
Chris Lattner4ad02e72003-04-16 20:28:45 +00001184 false,
1185 GlobalValue::ExternalLinkage);
Chris Lattner2079fde2001-10-13 06:41:08 +00001186 // Keep track of the fact that we have a forward ref to recycle it
Chris Lattner9232b992003-04-22 18:42:41 +00001187 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
Chris Lattner2079fde2001-10-13 06:41:08 +00001188
1189 // Must temporarily push this value into the module table...
1190 CurModule.CurrentModule->getGlobalList().push_back(GV);
1191 V = GV;
1192 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001193 }
1194
Chris Lattner2079fde2001-10-13 06:41:08 +00001195 GlobalValue *GV = cast<GlobalValue>(V);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001196 $$ = ConstantPointerRef::get(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001197 delete $1; // Free the type handle
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001198 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001199 | Types ConstExpr {
1200 if ($1->get() != $2->getType())
1201 ThrowException("Mismatched types for constant expression!");
1202 $$ = $2;
1203 delete $1;
Chris Lattnerf4600482003-06-28 20:01:34 +00001204 }
1205 | Types ZEROINITIALIZER {
1206 $$ = Constant::getNullValue($1->get());
1207 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001208 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001209
Chris Lattnere43f40b2002-10-09 00:25:32 +00001210ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001211 if (!ConstantSInt::isValueValidForType($1, $2))
1212 ThrowException("Constant value doesn't fit in type!");
1213 $$ = ConstantSInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001214 }
1215 | UIntType EUINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001216 if (!ConstantUInt::isValueValidForType($1, $2))
1217 ThrowException("Constant value doesn't fit in type!");
1218 $$ = ConstantUInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001219 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001220 | BOOL TRUETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001221 $$ = ConstantBool::True;
1222 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001223 | BOOL FALSETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001224 $$ = ConstantBool::False;
1225 }
1226 | FPType FPVAL { // Float & Double constants
1227 $$ = ConstantFP::get($1, $2);
1228 };
1229
Chris Lattner00950542001-06-06 20:29:01 +00001230
Chris Lattnerd78700d2002-08-16 21:14:40 +00001231ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattnerae711a82003-11-21 20:27:35 +00001232 if (!$3->getType()->isFirstClassType())
1233 ThrowException("cast constant expression from a non-primitive type: '" +
1234 $3->getType()->getDescription() + "'!");
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001235 if (!$5->get()->isFirstClassType())
1236 ThrowException("cast constant expression to a non-primitive type: '" +
1237 $5->get()->getDescription() + "'!");
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001238 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001239 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001240 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001241 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1242 if (!isa<PointerType>($3->getType()))
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001243 ThrowException("GetElementPtr requires a pointer operand!");
1244
Chris Lattner830b6f92004-04-05 01:30:04 +00001245 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
1246 // indices to uint struct indices for compatibility.
1247 generic_gep_type_iterator<std::vector<Value*>::iterator>
1248 GTI = gep_type_begin($3->getType(), $4->begin(), $4->end()),
1249 GTE = gep_type_end($3->getType(), $4->begin(), $4->end());
1250 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
1251 if (isa<StructType>(*GTI)) // Only change struct indices
1252 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
1253 if (CUI->getType() == Type::UByteTy)
1254 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
1255
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001256 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001257 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001258 if (!IdxTy)
1259 ThrowException("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001260
Chris Lattner9232b992003-04-22 18:42:41 +00001261 std::vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001262 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1263 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001264 IdxVec.push_back(C);
1265 else
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001266 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001267
Chris Lattnerd78700d2002-08-16 21:14:40 +00001268 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001269
Chris Lattnerd78700d2002-08-16 21:14:40 +00001270 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001271 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001272 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1273 if ($3->getType() != Type::BoolTy)
1274 ThrowException("Select condition must be of boolean type!");
1275 if ($5->getType() != $7->getType())
1276 ThrowException("Select operand types must match!");
1277 $$ = ConstantExpr::getSelect($3, $5, $7);
1278 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001279 | BinaryOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001280 if ($3->getType() != $5->getType())
1281 ThrowException("Binary operator types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001282 $$ = ConstantExpr::get($1, $3, $5);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001283 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001284 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001285 if ($5->getType() != Type::UByteTy)
1286 ThrowException("Shift count for shift constant must be unsigned byte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001287 if (!$3->getType()->isInteger())
1288 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerb16689b2004-01-12 19:08:43 +00001289 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner699f1eb2002-08-14 17:12:33 +00001290 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001291
1292
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001293// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001294ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001295 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001296 }
1297 | ConstVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001298 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001299 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001300 };
Chris Lattner00950542001-06-06 20:29:01 +00001301
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001302
Chris Lattner1781aca2001-09-18 04:00:54 +00001303// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001304GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001305
Chris Lattner00950542001-06-06 20:29:01 +00001306
Chris Lattner0e73ce62002-05-02 19:11:13 +00001307//===----------------------------------------------------------------------===//
1308// Rules to match Modules
1309//===----------------------------------------------------------------------===//
1310
1311// Module rule: Capture the result of parsing the whole file into a result
1312// variable...
1313//
1314Module : FunctionList {
1315 $$ = ParserResult = $1;
1316 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001317};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001318
Chris Lattner7e708292002-06-25 16:13:24 +00001319// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001320//
1321FunctionList : FunctionList Function {
1322 $$ = $1;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001323 CurFun.FunctionDone();
Chris Lattner0e73ce62002-05-02 19:11:13 +00001324 }
1325 | FunctionList FunctionProto {
1326 $$ = $1;
1327 }
1328 | FunctionList IMPLEMENTATION {
1329 $$ = $1;
1330 }
1331 | ConstPool {
1332 $$ = CurModule.CurrentModule;
1333 // Resolve circular types before we parse the body of the module
1334 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001335 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001336
Chris Lattnere98dda62001-07-14 06:10:16 +00001337// ConstPool - Constants with optional names assigned to them.
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001338ConstPool : ConstPool OptAssign CONST ConstVal {
Chris Lattneradf99702003-03-03 23:28:55 +00001339 if (!setValueName($4, $2))
1340 InsertValue($4);
Chris Lattner00950542001-06-06 20:29:01 +00001341 }
Chris Lattner30c89792001-09-07 16:35:17 +00001342 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner4a42e902001-10-22 05:56:09 +00001343 // Eagerly resolve types. This is not an optimization, this is a
1344 // requirement that is due to the fact that we could have this:
1345 //
1346 // %list = type { %list * }
1347 // %list = type { %list * } ; repeated type decl
1348 //
1349 // If types are not resolved eagerly, then the two types will not be
1350 // determined to be the same type!
1351 //
1352 ResolveTypeTo($2, $4->get());
1353
Chris Lattner1781aca2001-09-18 04:00:54 +00001354 // TODO: FIXME when Type are not const
Chris Lattnerb7474512001-10-03 15:39:04 +00001355 if (!setValueName(const_cast<Type*>($4->get()), $2)) {
1356 // If this is not a redefinition of a type...
1357 if (!$2) {
1358 InsertType($4->get(),
Chris Lattner394f2eb2003-10-16 20:12:13 +00001359 inFunctionScope() ? CurFun.Types : CurModule.Types);
Chris Lattnerb7474512001-10-03 15:39:04 +00001360 }
Chris Lattner30c89792001-09-07 16:35:17 +00001361 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001362
1363 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001364 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001365 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001366 }
Chris Lattner4ad02e72003-04-16 20:28:45 +00001367 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Chris Lattnerdda71962001-11-26 18:54:16 +00001368 const Type *Ty = $5->getType();
Chris Lattner1781aca2001-09-18 04:00:54 +00001369 // Global declarations appear in Constant Pool
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001370 Constant *Initializer = $5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001371 if (Initializer == 0)
1372 ThrowException("Global value initializer is not a constant!");
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001373
Chris Lattnerdda71962001-11-26 18:54:16 +00001374 GlobalVariable *GV = new GlobalVariable(Ty, $4, $3, Initializer);
Chris Lattnerb7474512001-10-03 15:39:04 +00001375 if (!setValueName(GV, $2)) { // If not redefining...
1376 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001377 int Slot = InsertValue(GV, CurModule.Values);
1378
1379 if (Slot != -1) {
1380 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1381 } else {
1382 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1383 (char*)GV->getName().c_str()));
1384 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001385 }
Chris Lattner1781aca2001-09-18 04:00:54 +00001386 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001387 | ConstPool OptAssign EXTERNAL GlobalType Types {
1388 const Type *Ty = *$5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001389 // Global declarations appear in Constant Pool
Chris Lattner4ad02e72003-04-16 20:28:45 +00001390 GlobalVariable *GV = new GlobalVariable(Ty,$4,GlobalValue::ExternalLinkage);
Chris Lattnerb7474512001-10-03 15:39:04 +00001391 if (!setValueName(GV, $2)) { // If not redefining...
1392 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001393 int Slot = InsertValue(GV, CurModule.Values);
1394
1395 if (Slot != -1) {
1396 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1397 } else {
1398 assert(GV->hasName() && "Not named and not numbered!?");
1399 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1400 (char*)GV->getName().c_str()));
1401 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001402 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001403 delete $5;
Chris Lattnere98dda62001-07-14 06:10:16 +00001404 }
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001405 | ConstPool TARGET TargetDefinition {
1406 }
Chris Lattner00950542001-06-06 20:29:01 +00001407 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001408 };
Chris Lattner00950542001-06-06 20:29:01 +00001409
1410
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001411
1412BigOrLittle : BIG { $$ = Module::BigEndian; };
1413BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1414
1415TargetDefinition : ENDIAN '=' BigOrLittle {
1416 CurModule.CurrentModule->setEndianness($3);
1417 }
1418 | POINTERSIZE '=' EUINT64VAL {
1419 if ($3 == 32)
1420 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1421 else if ($3 == 64)
1422 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1423 else
1424 ThrowException("Invalid pointer size: '" + utostr($3) + "'!");
1425 };
1426
1427
Chris Lattner00950542001-06-06 20:29:01 +00001428//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001429// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001430//===----------------------------------------------------------------------===//
1431
Chris Lattner6c23f572003-08-22 05:42:10 +00001432Name : VAR_ID | STRINGCONSTANT;
1433OptName : Name | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001434
Chris Lattner6c23f572003-08-22 05:42:10 +00001435ArgVal : Types OptName {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001436 if (*$1 == Type::VoidTy)
1437 ThrowException("void typed arguments are invalid!");
Chris Lattner9232b992003-04-22 18:42:41 +00001438 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
Chris Lattner51727be2002-06-04 21:58:56 +00001439};
Chris Lattner00950542001-06-06 20:29:01 +00001440
Chris Lattner69da5cf2002-10-13 20:57:00 +00001441ArgListH : ArgListH ',' ArgVal {
1442 $$ = $1;
1443 $1->push_back(*$3);
1444 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001445 }
1446 | ArgVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001447 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
Chris Lattner69da5cf2002-10-13 20:57:00 +00001448 $$->push_back(*$1);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001449 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001450 };
Chris Lattner00950542001-06-06 20:29:01 +00001451
1452ArgList : ArgListH {
1453 $$ = $1;
1454 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001455 | ArgListH ',' DOTDOTDOT {
1456 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001457 $$->push_back(std::pair<PATypeHolder*,
1458 char*>(new PATypeHolder(Type::VoidTy), 0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001459 }
1460 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001461 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1462 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001463 }
Chris Lattner00950542001-06-06 20:29:01 +00001464 | /* empty */ {
1465 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001466 };
Chris Lattner00950542001-06-06 20:29:01 +00001467
Chris Lattner6c23f572003-08-22 05:42:10 +00001468FunctionHeaderH : TypesV Name '(' ArgList ')' {
Chris Lattner1f862af2003-04-16 18:13:57 +00001469 UnEscapeLexed($2);
Chris Lattner9232b992003-04-22 18:42:41 +00001470 std::string FunctionName($2);
Chris Lattnerdda71962001-11-26 18:54:16 +00001471
Chris Lattnerc282f5a2003-11-21 22:32:23 +00001472 if (!(*$1)->isFirstClassType() && *$1 != Type::VoidTy)
1473 ThrowException("LLVM functions cannot return aggregate types!");
1474
Chris Lattner9232b992003-04-22 18:42:41 +00001475 std::vector<const Type*> ParamTypeList;
Chris Lattner1f862af2003-04-16 18:13:57 +00001476 if ($4) { // If there are arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001477 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001478 I != $4->end(); ++I)
Chris Lattner69da5cf2002-10-13 20:57:00 +00001479 ParamTypeList.push_back(I->first->get());
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001480 }
Chris Lattner00950542001-06-06 20:29:01 +00001481
Chris Lattner2079fde2001-10-13 06:41:08 +00001482 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1483 if (isVarArg) ParamTypeList.pop_back();
1484
Chris Lattner1f862af2003-04-16 18:13:57 +00001485 const FunctionType *FT = FunctionType::get(*$1, ParamTypeList, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001486 const PointerType *PFT = PointerType::get(FT);
Chris Lattner1f862af2003-04-16 18:13:57 +00001487 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001488
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001489 Function *Fn = 0;
1490 // Is the function already in symtab?
1491 if ((Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1492 // Yes it is. If this is the case, either we need to be a forward decl,
1493 // or it needs to be.
Chris Lattner394f2eb2003-10-16 20:12:13 +00001494 if (!CurFun.isDeclare && !Fn->isExternal())
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001495 ThrowException("Redefinition of function '" + FunctionName + "'!");
1496
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001497 // Make sure to strip off any argument names so we can't get conflicts...
1498 for (Function::aiterator AI = Fn->abegin(), AE = Fn->aend(); AI != AE; ++AI)
1499 AI->setName("");
Chris Lattner5659dd12002-07-15 00:10:33 +00001500
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001501 } else { // Not already defined?
Chris Lattner63a22502004-03-08 16:14:19 +00001502 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
1503 CurModule.CurrentModule);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001504 InsertValue(Fn, CurModule.Values);
Chris Lattner1f862af2003-04-16 18:13:57 +00001505 CurModule.DeclareNewGlobalValue(Fn, ValID::create($2));
Chris Lattnere1815642001-07-15 06:35:53 +00001506 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001507 free($2); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001508
Chris Lattner394f2eb2003-10-16 20:12:13 +00001509 CurFun.FunctionStart(Fn);
Chris Lattner00950542001-06-06 20:29:01 +00001510
Chris Lattner7e708292002-06-25 16:13:24 +00001511 // Add all of the arguments we parsed to the function...
Chris Lattner1f862af2003-04-16 18:13:57 +00001512 if ($4) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001513 if (isVarArg) { // Nuke the last entry
Chris Lattner1f862af2003-04-16 18:13:57 +00001514 assert($4->back().first->get() == Type::VoidTy && $4->back().second == 0&&
Chris Lattner69da5cf2002-10-13 20:57:00 +00001515 "Not a varargs marker!");
Chris Lattner1f862af2003-04-16 18:13:57 +00001516 delete $4->back().first;
1517 $4->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00001518 }
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001519 Function::aiterator ArgIt = Fn->abegin();
Chris Lattner9232b992003-04-22 18:42:41 +00001520 for (std::vector<std::pair<PATypeHolder*, char*> >::iterator I =$4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001521 I != $4->end(); ++I, ++ArgIt) {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001522 delete I->first; // Delete the typeholder...
1523
1524 if (setValueName(ArgIt, I->second)) // Insert arg into symtab...
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001525 assert(0 && "No arg redef allowed!");
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001526
Chris Lattner69da5cf2002-10-13 20:57:00 +00001527 InsertValue(ArgIt);
Chris Lattner00950542001-06-06 20:29:01 +00001528 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001529
Chris Lattner1f862af2003-04-16 18:13:57 +00001530 delete $4; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00001531 }
Chris Lattner51727be2002-06-04 21:58:56 +00001532};
Chris Lattner00950542001-06-06 20:29:01 +00001533
Chris Lattner9b02cc32002-05-03 18:23:48 +00001534BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1535
Chris Lattner4ad02e72003-04-16 20:28:45 +00001536FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00001537 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001538
Chris Lattner4ad02e72003-04-16 20:28:45 +00001539 // Make sure that we keep track of the linkage type even if there was a
1540 // previous "declare".
1541 $$->setLinkage($1);
Chris Lattner1f862af2003-04-16 18:13:57 +00001542
Chris Lattner7e708292002-06-25 16:13:24 +00001543 // Resolve circular types before we parse the body of the function.
Chris Lattner394f2eb2003-10-16 20:12:13 +00001544 ResolveTypes(CurFun.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001545};
Chris Lattner00950542001-06-06 20:29:01 +00001546
Chris Lattner9b02cc32002-05-03 18:23:48 +00001547END : ENDTOK | '}'; // Allow end of '}' to end a function
1548
Chris Lattner79df7c02002-03-26 18:01:55 +00001549Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001550 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001551};
Chris Lattner00950542001-06-06 20:29:01 +00001552
Chris Lattner394f2eb2003-10-16 20:12:13 +00001553FunctionProto : DECLARE { CurFun.isDeclare = true; } FunctionHeaderH {
1554 $$ = CurFun.CurrentFunction;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001555 CurFun.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001556};
Chris Lattner00950542001-06-06 20:29:01 +00001557
1558//===----------------------------------------------------------------------===//
1559// Rules to match Basic Blocks
1560//===----------------------------------------------------------------------===//
1561
1562ConstValueRef : ESINT64VAL { // A reference to a direct constant
1563 $$ = ValID::create($1);
1564 }
1565 | EUINT64VAL {
1566 $$ = ValID::create($1);
1567 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001568 | FPVAL { // Perhaps it's an FP constant?
1569 $$ = ValID::create($1);
1570 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001571 | TRUETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001572 $$ = ValID::create(ConstantBool::True);
Chris Lattner00950542001-06-06 20:29:01 +00001573 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001574 | FALSETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001575 $$ = ValID::create(ConstantBool::False);
Chris Lattner00950542001-06-06 20:29:01 +00001576 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001577 | NULL_TOK {
1578 $$ = ValID::createNull();
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001579 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001580 | ConstExpr {
1581 $$ = ValID::create($1);
1582 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00001583
Chris Lattner2079fde2001-10-13 06:41:08 +00001584// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1585// another value.
1586//
1587SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001588 $$ = ValID::create($1);
1589 }
Chris Lattner6c23f572003-08-22 05:42:10 +00001590 | Name { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001591 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001592 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001593
1594// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001595ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001596
Chris Lattner00950542001-06-06 20:29:01 +00001597
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001598// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1599// type immediately preceeds the value reference, and allows complex constant
1600// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001601ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001602 $$ = getVal(*$1, $2); delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001603 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001604
Chris Lattner00950542001-06-06 20:29:01 +00001605BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner7e708292002-06-25 16:13:24 +00001606 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner00950542001-06-06 20:29:01 +00001607 }
Chris Lattner7e708292002-06-25 16:13:24 +00001608 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
1609 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner51727be2002-06-04 21:58:56 +00001610 };
Chris Lattner00950542001-06-06 20:29:01 +00001611
1612
1613// Basic blocks are terminated by branching instructions:
1614// br, br/cc, switch, ret
1615//
Chris Lattner2079fde2001-10-13 06:41:08 +00001616BasicBlock : InstructionList OptAssign BBTerminatorInst {
1617 if (setValueName($3, $2)) { assert(0 && "No redefn allowed!"); }
1618 InsertValue($3);
1619
1620 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001621 InsertValue($1);
1622 $$ = $1;
1623 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001624 | LABELSTR InstructionList OptAssign BBTerminatorInst {
1625 if (setValueName($4, $3)) { assert(0 && "No redefn allowed!"); }
1626 InsertValue($4);
1627
1628 $2->getInstList().push_back($4);
Chris Lattnerb7474512001-10-03 15:39:04 +00001629 if (setValueName($2, $1)) { assert(0 && "No label redef allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001630
1631 InsertValue($2);
1632 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001633 };
Chris Lattner00950542001-06-06 20:29:01 +00001634
1635InstructionList : InstructionList Inst {
1636 $1->getInstList().push_back($2);
1637 $$ = $1;
1638 }
1639 | /* empty */ {
Chris Lattner99e7ab72003-10-18 05:53:13 +00001640 $$ = CurBB = new BasicBlock();
Chris Lattner51727be2002-06-04 21:58:56 +00001641 };
Chris Lattner00950542001-06-06 20:29:01 +00001642
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001643BBTerminatorInst : RET ResolvedVal { // Return with a result...
1644 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001645 }
1646 | RET VOID { // Return with no result...
1647 $$ = new ReturnInst();
1648 }
1649 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner9636a912001-10-01 16:18:37 +00001650 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
Chris Lattner00950542001-06-06 20:29:01 +00001651 } // Conditional Branch...
1652 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner9636a912001-10-01 16:18:37 +00001653 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1654 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
Chris Lattner00950542001-06-06 20:29:01 +00001655 getVal(Type::BoolTy, $3));
1656 }
1657 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1658 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001659 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
Chris Lattner00950542001-06-06 20:29:01 +00001660 $$ = S;
1661
Chris Lattner9232b992003-04-22 18:42:41 +00001662 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00001663 E = $8->end();
1664 for (; I != E; ++I)
Chris Lattner4354f562003-08-23 23:14:52 +00001665 S->addCase(I->first, I->second);
Chris Lattnerf57a43d2004-04-17 23:49:15 +00001666 delete $8;
Chris Lattner00950542001-06-06 20:29:01 +00001667 }
Chris Lattner196850c2003-05-15 21:30:00 +00001668 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
1669 SwitchInst *S = new SwitchInst(getVal($2, $3),
1670 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
1671 $$ = S;
1672 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001673 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO ResolvedVal
Chris Lattnera58e3a12004-02-08 21:48:25 +00001674 UNWIND ResolvedVal {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001675 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001676 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001677
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001678 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1679 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001680 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001681 std::vector<const Type*> ParamTypes;
Chris Lattner2079fde2001-10-13 06:41:08 +00001682 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001683 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1684 I != E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001685 ParamTypes.push_back((*I)->getType());
1686 }
1687
1688 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1689 if (isVarArg) ParamTypes.pop_back();
1690
Chris Lattner79df7c02002-03-26 18:01:55 +00001691 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001692 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00001693 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001694
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001695 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00001696
1697 BasicBlock *Normal = dyn_cast<BasicBlock>($8);
1698 BasicBlock *Except = dyn_cast<BasicBlock>($10);
1699
1700 if (Normal == 0 || Except == 0)
1701 ThrowException("Invoke instruction without label destinations!");
1702
1703 // Create the call node...
1704 if (!$5) { // Has no arguments?
Chris Lattner9232b992003-04-22 18:42:41 +00001705 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001706 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001707 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00001708 // correctly!
1709 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001710 FunctionType::param_iterator I = Ty->param_begin();
1711 FunctionType::param_iterator E = Ty->param_end();
Chris Lattner9232b992003-04-22 18:42:41 +00001712 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001713
1714 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1715 if ((*ArgI)->getType() != *I)
1716 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001717 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001718
1719 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1720 ThrowException("Invalid number of parameters detected!");
1721
Chris Lattner6cdb0112001-11-26 16:54:11 +00001722 $$ = new InvokeInst(V, Normal, Except, *$5);
Chris Lattner2079fde2001-10-13 06:41:08 +00001723 }
Chris Lattner9cd42572003-12-23 22:18:36 +00001724 delete $2;
Chris Lattner2079fde2001-10-13 06:41:08 +00001725 delete $5;
Chris Lattner36143fc2003-09-08 18:54:55 +00001726 }
1727 | UNWIND {
1728 $$ = new UnwindInst();
Chris Lattner51727be2002-06-04 21:58:56 +00001729 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001730
1731
Chris Lattner00950542001-06-06 20:29:01 +00001732
1733JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1734 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001735 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001736 if (V == 0)
1737 ThrowException("May only switch on a constant pool value!");
1738
Chris Lattner9232b992003-04-22 18:42:41 +00001739 $$->push_back(std::make_pair(V, cast<BasicBlock>(getVal($5, $6))));
Chris Lattner00950542001-06-06 20:29:01 +00001740 }
1741 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00001742 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001743 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001744
1745 if (V == 0)
1746 ThrowException("May only switch on a constant pool value!");
1747
Chris Lattner9232b992003-04-22 18:42:41 +00001748 $$->push_back(std::make_pair(V, cast<BasicBlock>(getVal($4, $5))));
Chris Lattner51727be2002-06-04 21:58:56 +00001749 };
Chris Lattner00950542001-06-06 20:29:01 +00001750
1751Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001752 // Is this definition named?? if so, assign the name...
1753 if (setValueName($2, $1)) { assert(0 && "No redefin allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001754 InsertValue($2);
1755 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001756};
Chris Lattner00950542001-06-06 20:29:01 +00001757
Chris Lattnerc24d2082001-06-11 15:04:20 +00001758PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Chris Lattner9232b992003-04-22 18:42:41 +00001759 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
1760 $$->push_back(std::make_pair(getVal(*$1, $3),
1761 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
Chris Lattner30c89792001-09-07 16:35:17 +00001762 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001763 }
1764 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1765 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001766 $1->push_back(std::make_pair(getVal($1->front().first->getType(), $4),
1767 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
Chris Lattner51727be2002-06-04 21:58:56 +00001768 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00001769
1770
Chris Lattner30c89792001-09-07 16:35:17 +00001771ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner9232b992003-04-22 18:42:41 +00001772 $$ = new std::vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001773 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001774 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001775 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001776 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001777 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00001778 };
Chris Lattner00950542001-06-06 20:29:01 +00001779
1780// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00001781ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001782
Chris Lattner4a6482b2002-09-10 19:57:26 +00001783InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1784 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint())
1785 ThrowException("Arithmetic operator requires integer or FP operands!");
1786 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1787 if ($$ == 0)
1788 ThrowException("binary operator returned null!");
1789 delete $2;
1790 }
1791 | LogicalOps Types ValueRef ',' ValueRef {
1792 if (!(*$2)->isIntegral())
1793 ThrowException("Logical operator requires integral operands!");
1794 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1795 if ($$ == 0)
1796 ThrowException("binary operator returned null!");
1797 delete $2;
1798 }
1799 | SetCondOps Types ValueRef ',' ValueRef {
Chris Lattner1cff96a2002-09-10 22:37:46 +00001800 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001801 if ($$ == 0)
1802 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001803 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001804 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00001805 | NOT ResolvedVal {
1806 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
1807 << " Replacing with 'xor'.\n";
1808
1809 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
1810 if (Ones == 0)
1811 ThrowException("Expected integral type for not instruction!");
1812
1813 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00001814 if ($$ == 0)
Chris Lattner699f1eb2002-08-14 17:12:33 +00001815 ThrowException("Could not create a xor instruction!");
Chris Lattner09083092001-07-08 04:57:15 +00001816 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001817 | ShiftOps ResolvedVal ',' ResolvedVal {
1818 if ($4->getType() != Type::UByteTy)
1819 ThrowException("Shift amount must be ubyte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001820 if (!$2->getType()->isInteger())
1821 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001822 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001823 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001824 | CAST ResolvedVal TO Types {
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001825 if (!$4->get()->isFirstClassType())
1826 ThrowException("cast instruction to a non-primitive type: '" +
1827 $4->get()->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001828 $$ = new CastInst($2, *$4);
1829 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001830 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001831 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1832 if ($2->getType() != Type::BoolTy)
1833 ThrowException("select condition must be boolean!");
1834 if ($4->getType() != $6->getType())
1835 ThrowException("select value types should match!");
1836 $$ = new SelectInst($2, $4, $6);
1837 }
Chris Lattner8f77dae2003-05-08 02:44:12 +00001838 | VA_ARG ResolvedVal ',' Types {
Chris Lattner99e7ab72003-10-18 05:53:13 +00001839 // FIXME: This is emulation code for an obsolete syntax. This should be
1840 // removed at some point.
1841 if (!ObsoleteVarArgs) {
1842 std::cerr << "WARNING: this file uses obsolete features. "
1843 << "Assemble and disassemble to update it.\n";
1844 ObsoleteVarArgs = true;
1845 }
1846
1847 // First, load the valist...
1848 Instruction *CurVAList = new LoadInst($2, "");
1849 CurBB->getInstList().push_back(CurVAList);
1850
1851 // Emit the vaarg instruction.
1852 $$ = new VAArgInst(CurVAList, *$4);
1853
1854 // Now we must advance the pointer and update it in memory.
1855 Instruction *TheVANext = new VANextInst(CurVAList, *$4);
1856 CurBB->getInstList().push_back(TheVANext);
1857
1858 CurBB->getInstList().push_back(new StoreInst(TheVANext, $2));
1859 delete $4;
1860 }
1861 | VAARG ResolvedVal ',' Types {
1862 $$ = new VAArgInst($2, *$4);
1863 delete $4;
1864 }
1865 | VANEXT ResolvedVal ',' Types {
1866 $$ = new VANextInst($2, *$4);
Chris Lattner8f77dae2003-05-08 02:44:12 +00001867 delete $4;
1868 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00001869 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001870 const Type *Ty = $2->front().first->getType();
Chris Lattner8ea8f362003-10-30 01:38:18 +00001871 if (!Ty->isFirstClassType())
1872 ThrowException("PHI node operands must be of first class type!");
Chris Lattnerc24d2082001-06-11 15:04:20 +00001873 $$ = new PHINode(Ty);
Chris Lattnerbd2531f2003-10-10 16:34:58 +00001874 $$->op_reserve($2->size()*2);
Chris Lattner00950542001-06-06 20:29:01 +00001875 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001876 if ($2->front().first->getType() != Ty)
1877 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001878 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001879 $2->pop_front();
1880 }
1881 delete $2; // Free the list...
1882 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001883 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001884 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001885 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001886
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001887 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1888 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001889 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001890 std::vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001891 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001892 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1893 I != E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00001894 ParamTypes.push_back((*I)->getType());
1895 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001896
1897 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1898 if (isVarArg) ParamTypes.pop_back();
1899
Chris Lattner79df7c02002-03-26 18:01:55 +00001900 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001901 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001902 }
Chris Lattner00950542001-06-06 20:29:01 +00001903
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001904 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001905
Chris Lattner8b81bf52001-07-25 22:47:46 +00001906 // Create the call node...
1907 if (!$5) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00001908 // Make sure no arguments is a good thing!
1909 if (Ty->getNumParams() != 0)
1910 ThrowException("No arguments passed to a function that "
1911 "expects arguments!");
1912
Chris Lattner9232b992003-04-22 18:42:41 +00001913 $$ = new CallInst(V, std::vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001914 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001915 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00001916 // correctly!
1917 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001918 FunctionType::param_iterator I = Ty->param_begin();
1919 FunctionType::param_iterator E = Ty->param_end();
Chris Lattner9232b992003-04-22 18:42:41 +00001920 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001921
1922 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1923 if ((*ArgI)->getType() != *I)
1924 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001925 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001926
Chris Lattner8b81bf52001-07-25 22:47:46 +00001927 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001928 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001929
Chris Lattner6cdb0112001-11-26 16:54:11 +00001930 $$ = new CallInst(V, *$5);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001931 }
Chris Lattnerf64d57a2003-12-23 20:39:17 +00001932 delete $2;
Chris Lattner8b81bf52001-07-25 22:47:46 +00001933 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001934 }
1935 | MemoryInst {
1936 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001937 };
Chris Lattner00950542001-06-06 20:29:01 +00001938
Chris Lattner6cdb0112001-11-26 16:54:11 +00001939
1940// IndexList - List of indices for GEP based instructions...
1941IndexList : ',' ValueRefList {
Chris Lattner15c9c032003-09-08 18:20:29 +00001942 $$ = $2;
1943 } | /* empty */ {
1944 $$ = new std::vector<Value*>();
1945 };
1946
1947OptVolatile : VOLATILE {
1948 $$ = true;
1949 }
1950 | /* empty */ {
1951 $$ = false;
1952 };
1953
Chris Lattner027dcc52001-07-08 21:10:27 +00001954
Chris Lattner00950542001-06-06 20:29:01 +00001955MemoryInst : MALLOC Types {
Chris Lattner05804b72002-09-13 22:28:45 +00001956 $$ = new MallocInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00001957 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001958 }
1959 | MALLOC Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00001960 $$ = new MallocInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001961 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001962 }
1963 | ALLOCA Types {
Chris Lattner05804b72002-09-13 22:28:45 +00001964 $$ = new AllocaInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00001965 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001966 }
1967 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00001968 $$ = new AllocaInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001969 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001970 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001971 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00001972 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001973 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00001974 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001975 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001976 }
1977
Chris Lattner15c9c032003-09-08 18:20:29 +00001978 | OptVolatile LOAD Types ValueRef {
1979 if (!isa<PointerType>($3->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00001980 ThrowException("Can't load from nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00001981 (*$3)->getDescription());
Chris Lattner79ad13802003-09-08 20:29:46 +00001982 $$ = new LoadInst(getVal(*$3, $4), "", $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00001983 delete $3;
Chris Lattner027dcc52001-07-08 21:10:27 +00001984 }
Chris Lattner15c9c032003-09-08 18:20:29 +00001985 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
1986 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00001987 if (!PT)
Chris Lattner72e00252001-12-14 16:28:42 +00001988 ThrowException("Can't store to a nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00001989 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00001990 const Type *ElTy = PT->getElementType();
Chris Lattner15c9c032003-09-08 18:20:29 +00001991 if (ElTy != $3->getType())
1992 ThrowException("Can't store '" + $3->getType()->getDescription() +
Chris Lattner72e00252001-12-14 16:28:42 +00001993 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner0383cc42002-08-21 23:51:21 +00001994
Chris Lattner79ad13802003-09-08 20:29:46 +00001995 $$ = new StoreInst($3, getVal(*$5, $6), $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00001996 delete $5;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001997 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00001998 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00001999 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002000 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner830b6f92004-04-05 01:30:04 +00002001
2002 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
2003 // indices to uint struct indices for compatibility.
2004 generic_gep_type_iterator<std::vector<Value*>::iterator>
2005 GTI = gep_type_begin($2->get(), $4->begin(), $4->end()),
2006 GTE = gep_type_end($2->get(), $4->begin(), $4->end());
2007 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
2008 if (isa<StructType>(*GTI)) // Only change struct indices
2009 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
2010 if (CUI->getType() == Type::UByteTy)
2011 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
2012
Chris Lattner30c89792001-09-07 16:35:17 +00002013 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner830b6f92004-04-05 01:30:04 +00002014 ThrowException("Invalid getelementptr indices for type '" +
2015 (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00002016 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
2017 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00002018 };
Chris Lattner027dcc52001-07-08 21:10:27 +00002019
Brian Gaeked0fde302003-11-11 22:41:34 +00002020
Chris Lattner00950542001-06-06 20:29:01 +00002021%%
Chris Lattner09083092001-07-08 04:57:15 +00002022int yyerror(const char *ErrorMsg) {
Chris Lattner9232b992003-04-22 18:42:41 +00002023 std::string where
2024 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002025 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Chris Lattner9232b992003-04-22 18:42:41 +00002026 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
Chris Lattnerbc280ab2004-03-30 20:58:25 +00002027 if (yychar == YYEMPTY || yychar == 0)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002028 errMsg += "end-of-file.";
2029 else
Chris Lattner9232b992003-04-22 18:42:41 +00002030 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002031 ThrowException(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00002032 return 0;
2033}