blob: 3543c55751db197c26772679c1dfc3265f9e4d90 [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files --------*- C++ -*-===//
John Criswell856ba762003-10-21 15:17:13 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This file implements the bison parser for LLVM assembly languages files.
11//
Chris Lattnercf3056d2003-10-13 03:32:08 +000012//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +000013
Chris Lattner00950542001-06-06 20:29:01 +000014%{
15#include "ParserInternals.h"
Chris Lattner00950542001-06-06 20:29:01 +000016#include "llvm/SymbolTable.h"
17#include "llvm/Module.h"
Chris Lattner00950542001-06-06 20:29:01 +000018#include "llvm/iTerminators.h"
19#include "llvm/iMemory.h"
Chris Lattner1cff96a2002-09-10 22:37:46 +000020#include "llvm/iOperators.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000021#include "llvm/iPHINode.h"
Chris Lattner830b6f92004-04-05 01:30:04 +000022#include "llvm/Support/GetElementPtrTypeIterator.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000023#include "Support/STLExtras.h"
Reid Spencer8ce1da72004-07-04 12:19:05 +000024#include <algorithm>
25#include <iostream>
Chris Lattner00950542001-06-06 20:29:01 +000026#include <list>
Chris Lattnerc188eeb2002-07-30 18:54:25 +000027#include <utility>
Chris Lattner00950542001-06-06 20:29:01 +000028
Chris Lattner386a3b72001-10-16 19:54:17 +000029int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
Chris Lattner09083092001-07-08 04:57:15 +000030int yylex(); // declaration" of xxx warnings.
Chris Lattner00950542001-06-06 20:29:01 +000031int yyparse();
32
Brian Gaeked0fde302003-11-11 22:41:34 +000033namespace llvm {
34
Chris Lattner00950542001-06-06 20:29:01 +000035static Module *ParserResult;
Chris Lattner9232b992003-04-22 18:42:41 +000036std::string CurFilename;
Chris Lattner00950542001-06-06 20:29:01 +000037
Chris Lattner30c89792001-09-07 16:35:17 +000038// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
39// relating to upreferences in the input stream.
40//
41//#define DEBUG_UPREFS 1
42#ifdef DEBUG_UPREFS
Chris Lattner699f1eb2002-08-14 17:12:33 +000043#define UR_OUT(X) std::cerr << X
Chris Lattner30c89792001-09-07 16:35:17 +000044#else
45#define UR_OUT(X)
46#endif
47
Vikram S. Adved3f7eb02002-07-14 22:59:28 +000048#define YYERROR_VERBOSE 1
49
Chris Lattner99e7ab72003-10-18 05:53:13 +000050// HACK ALERT: This variable is used to implement the automatic conversion of
51// variable argument instructions from their old to new forms. When this
52// compatiblity "Feature" is removed, this should be too.
53//
54static BasicBlock *CurBB;
55static bool ObsoleteVarArgs;
56
57
Chris Lattner7e708292002-06-25 16:13:24 +000058// This contains info used when building the body of a function. It is
59// destroyed when the function is completed.
Chris Lattner00950542001-06-06 20:29:01 +000060//
Chris Lattner9232b992003-04-22 18:42:41 +000061typedef std::vector<Value *> ValueList; // Numbered defs
Chris Lattner735f2702004-07-08 22:30:50 +000062static void ResolveDefinitions(std::map<const Type *,ValueList> &LateResolvers,
63 std::map<const Type *,ValueList> *FutureLateResolvers = 0);
Chris Lattner00950542001-06-06 20:29:01 +000064
65static struct PerModuleInfo {
66 Module *CurrentModule;
Chris Lattner735f2702004-07-08 22:30:50 +000067 std::map<const Type *, ValueList> Values; // Module level numbered definitions
68 std::map<const Type *,ValueList> LateResolveValues;
Chris Lattner16af11d2004-02-09 21:03:38 +000069 std::vector<PATypeHolder> Types;
Chris Lattner9232b992003-04-22 18:42:41 +000070 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000071
Chris Lattner2079fde2001-10-13 06:41:08 +000072 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
73 // references to global values. Global values may be referenced before they
74 // are defined, and if so, the temporary object that they represent is held
Chris Lattnere9bb2df2001-12-03 22:26:30 +000075 // here. This is used for forward references of ConstantPointerRefs.
Chris Lattner2079fde2001-10-13 06:41:08 +000076 //
Chris Lattner9232b992003-04-22 18:42:41 +000077 typedef std::map<std::pair<const PointerType *,
Chris Lattnerbc207592004-03-08 06:09:57 +000078 ValID>, GlobalValue*> GlobalRefsType;
Chris Lattner2079fde2001-10-13 06:41:08 +000079 GlobalRefsType GlobalRefs;
80
Chris Lattner00950542001-06-06 20:29:01 +000081 void ModuleDone() {
Chris Lattner7e708292002-06-25 16:13:24 +000082 // If we could not resolve some functions at function compilation time
83 // (calls to functions before they are defined), resolve them now... Types
84 // are resolved when the constant pool has been completely parsed.
Chris Lattner30c89792001-09-07 16:35:17 +000085 //
Chris Lattner00950542001-06-06 20:29:01 +000086 ResolveDefinitions(LateResolveValues);
87
Chris Lattner2079fde2001-10-13 06:41:08 +000088 // Check to make sure that all global value forward references have been
89 // resolved!
90 //
91 if (!GlobalRefs.empty()) {
Chris Lattner9232b992003-04-22 18:42:41 +000092 std::string UndefinedReferences = "Unresolved global references exist:\n";
Chris Lattner749ce032002-03-11 22:12:39 +000093
94 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
95 I != E; ++I) {
96 UndefinedReferences += " " + I->first.first->getDescription() + " " +
97 I->first.second.getName() + "\n";
98 }
99 ThrowException(UndefinedReferences);
Chris Lattner2079fde2001-10-13 06:41:08 +0000100 }
101
Chris Lattner7e708292002-06-25 16:13:24 +0000102 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000103 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +0000104 CurrentModule = 0;
105 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000106
107
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000108 // DeclareNewGlobalValue - Called every time a new GV has been defined. This
Chris Lattner2079fde2001-10-13 06:41:08 +0000109 // is used to remove things from the forward declaration map, resolving them
110 // to the correct thing as needed.
111 //
112 void DeclareNewGlobalValue(GlobalValue *GV, ValID D) {
113 // Check to see if there is a forward reference to this global variable...
114 // if there is, eliminate it and patch the reference to use the new def'n.
Chris Lattner9232b992003-04-22 18:42:41 +0000115 GlobalRefsType::iterator I =
116 GlobalRefs.find(std::make_pair(GV->getType(), D));
Chris Lattner2079fde2001-10-13 06:41:08 +0000117
118 if (I != GlobalRefs.end()) {
Chris Lattnerbc207592004-03-08 06:09:57 +0000119 GlobalValue *OldGV = I->second; // Get the placeholder...
Misha Brukman5560c9d2003-08-18 14:43:39 +0000120 I->first.second.destroy(); // Free string memory if necessary
Chris Lattner38ab6bf2004-07-13 06:58:07 +0000121
122 // Replace all uses of the placeholder with the new GV
123 OldGV->replaceAllUsesWith(GV);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000124
125 // Remove OldGV from the module...
Chris Lattnerbc207592004-03-08 06:09:57 +0000126 if (GlobalVariable *GVar = dyn_cast<GlobalVariable>(OldGV))
127 CurrentModule->getGlobalList().erase(GVar);
128 else
129 CurrentModule->getFunctionList().erase(cast<Function>(OldGV));
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000130
Chris Lattner2079fde2001-10-13 06:41:08 +0000131 // Remove the map entry for the global now that it has been created...
132 GlobalRefs.erase(I);
133 }
134 }
135
Chris Lattner00950542001-06-06 20:29:01 +0000136} CurModule;
137
Chris Lattner79df7c02002-03-26 18:01:55 +0000138static struct PerFunctionInfo {
Chris Lattner7e708292002-06-25 16:13:24 +0000139 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner00950542001-06-06 20:29:01 +0000140
Chris Lattner735f2702004-07-08 22:30:50 +0000141 std::map<const Type*, ValueList> Values; // Keep track of #'d definitions
142 std::map<const Type*, ValueList> LateResolveValues;
Chris Lattner9232b992003-04-22 18:42:41 +0000143 std::vector<PATypeHolder> Types;
144 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner7e708292002-06-25 16:13:24 +0000145 bool isDeclare; // Is this function a forward declararation?
Chris Lattner00950542001-06-06 20:29:01 +0000146
Chris Lattner79df7c02002-03-26 18:01:55 +0000147 inline PerFunctionInfo() {
148 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000149 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000150 }
151
Chris Lattner79df7c02002-03-26 18:01:55 +0000152 inline void FunctionStart(Function *M) {
153 CurrentFunction = M;
Chris Lattner00950542001-06-06 20:29:01 +0000154 }
155
Chris Lattner79df7c02002-03-26 18:01:55 +0000156 void FunctionDone() {
Chris Lattner00950542001-06-06 20:29:01 +0000157 // If we could not resolve some blocks at parsing time (forward branches)
158 // resolve the branches now...
Chris Lattner386a3b72001-10-16 19:54:17 +0000159 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000160
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000161 // Make sure to resolve any constant expr references that might exist within
162 // the function we just declared itself.
163 ValID FID;
164 if (CurrentFunction->hasName()) {
165 FID = ValID::create((char*)CurrentFunction->getName().c_str());
166 } else {
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000167 // Figure out which slot number if is...
Chris Lattner735f2702004-07-08 22:30:50 +0000168 ValueList &List = CurModule.Values[CurrentFunction->getType()];
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000169 for (unsigned i = 0; ; ++i) {
Chris Lattner16af11d2004-02-09 21:03:38 +0000170 assert(i < List.size() && "Function not found!");
171 if (List[i] == CurrentFunction) {
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000172 FID = ValID::create((int)i);
173 break;
174 }
175 }
176 }
177 CurModule.DeclareNewGlobalValue(CurrentFunction, FID);
178
Chris Lattner7e708292002-06-25 16:13:24 +0000179 Values.clear(); // Clear out function local definitions
Chris Lattner15b69722003-11-12 04:40:30 +0000180 Types.clear(); // Clear out function local types
Chris Lattner79df7c02002-03-26 18:01:55 +0000181 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000182 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000183 }
Chris Lattner394f2eb2003-10-16 20:12:13 +0000184} CurFun; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000185
Chris Lattner394f2eb2003-10-16 20:12:13 +0000186static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000187
Chris Lattner00950542001-06-06 20:29:01 +0000188
189//===----------------------------------------------------------------------===//
190// Code to handle definitions of all the types
191//===----------------------------------------------------------------------===//
192
Chris Lattner735f2702004-07-08 22:30:50 +0000193static int InsertValue(Value *V,
194 std::map<const Type*,ValueList> &ValueTab = CurFun.Values) {
195 if (V->hasName()) return -1; // Is this a numbered definition?
Chris Lattner2079fde2001-10-13 06:41:08 +0000196
197 // Yes, insert the value into the value table...
Chris Lattner735f2702004-07-08 22:30:50 +0000198 ValueList &List = ValueTab[V->getType()];
199 List.push_back(V);
Chris Lattner16af11d2004-02-09 21:03:38 +0000200 return List.size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000201}
202
Chris Lattner30c89792001-09-07 16:35:17 +0000203// TODO: FIXME when Type are not const
Chris Lattner9232b992003-04-22 18:42:41 +0000204static void InsertType(const Type *Ty, std::vector<PATypeHolder> &Types) {
Chris Lattner30c89792001-09-07 16:35:17 +0000205 Types.push_back(Ty);
206}
207
208static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000209 switch (D.Type) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000210 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000211 unsigned Num = (unsigned)D.Num;
212
213 // Module constants occupy the lowest numbered slots...
214 if (Num < CurModule.Types.size())
215 return CurModule.Types[Num];
216
217 Num -= CurModule.Types.size();
218
219 // Check that the number is within bounds...
Chris Lattner394f2eb2003-10-16 20:12:13 +0000220 if (Num <= CurFun.Types.size())
221 return CurFun.Types[Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000222 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000223 }
Chris Lattnerf8dff732002-07-18 05:18:37 +0000224 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000225 std::string Name(D.Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000226 SymbolTable *SymTab = 0;
Reid Spencer8ce1da72004-07-04 12:19:05 +0000227 Type *N = 0;
Chris Lattner6e6026b2002-11-20 18:36:02 +0000228 if (inFunctionScope()) {
Chris Lattner394f2eb2003-10-16 20:12:13 +0000229 SymTab = &CurFun.CurrentFunction->getSymbolTable();
Reid Spencerb152f9f2004-05-25 17:29:21 +0000230 N = SymTab->lookupType(Name);
Chris Lattner6e6026b2002-11-20 18:36:02 +0000231 }
Chris Lattner30c89792001-09-07 16:35:17 +0000232
233 if (N == 0) {
Chris Lattner7e708292002-06-25 16:13:24 +0000234 // Symbol table doesn't automatically chain yet... because the function
Chris Lattner30c89792001-09-07 16:35:17 +0000235 // hasn't been added to the module...
236 //
Chris Lattner6e6026b2002-11-20 18:36:02 +0000237 SymTab = &CurModule.CurrentModule->getSymbolTable();
Reid Spencerb152f9f2004-05-25 17:29:21 +0000238 N = SymTab->lookupType(Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000239 if (N == 0) break;
240 }
241
242 D.destroy(); // Free old strdup'd memory...
Chris Lattner949a3622003-07-23 15:30:06 +0000243 return cast<Type>(N);
Chris Lattner30c89792001-09-07 16:35:17 +0000244 }
245 default:
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000246 ThrowException("Internal parser error: Invalid symbol type reference!");
Chris Lattner30c89792001-09-07 16:35:17 +0000247 }
248
249 // If we reached here, we referenced either a symbol that we don't know about
250 // or an id number that hasn't been read yet. We may be referencing something
251 // forward, so just create an entry to be resolved later and get to it...
252 //
253 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
254
Chris Lattner9232b992003-04-22 18:42:41 +0000255 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000256 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
Chris Lattner4a42e902001-10-22 05:56:09 +0000257
Chris Lattner9232b992003-04-22 18:42:41 +0000258 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000259 if (I != LateResolver.end()) {
260 return I->second;
261 }
Chris Lattner30c89792001-09-07 16:35:17 +0000262
Chris Lattner82269592001-10-22 06:01:08 +0000263 Type *Typ = OpaqueType::get();
Chris Lattner9232b992003-04-22 18:42:41 +0000264 LateResolver.insert(std::make_pair(D, Typ));
Chris Lattner30c89792001-09-07 16:35:17 +0000265 return Typ;
266}
267
Chris Lattner9232b992003-04-22 18:42:41 +0000268static Value *lookupInSymbolTable(const Type *Ty, const std::string &Name) {
Chris Lattner6e6026b2002-11-20 18:36:02 +0000269 SymbolTable &SymTab =
Chris Lattner394f2eb2003-10-16 20:12:13 +0000270 inFunctionScope() ? CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner9705a152002-05-02 19:27:42 +0000271 CurModule.CurrentModule->getSymbolTable();
Chris Lattner6e6026b2002-11-20 18:36:02 +0000272 return SymTab.lookup(Ty, Name);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000273}
274
Chris Lattner2079fde2001-10-13 06:41:08 +0000275// getValNonImprovising - Look up the value specified by the provided type and
276// the provided ValID. If the value exists and has already been defined, return
277// it. Otherwise return null.
278//
279static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000280 if (isa<FunctionType>(Ty))
281 ThrowException("Functions are not values and "
282 "must be referenced as pointers");
Chris Lattner386a3b72001-10-16 19:54:17 +0000283
Chris Lattner30c89792001-09-07 16:35:17 +0000284 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000285 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner00950542001-06-06 20:29:01 +0000286 unsigned Num = (unsigned)D.Num;
287
288 // Module constants occupy the lowest numbered slots...
Chris Lattner735f2702004-07-08 22:30:50 +0000289 std::map<const Type*,ValueList>::iterator VI = CurModule.Values.find(Ty);
Chris Lattner16af11d2004-02-09 21:03:38 +0000290 if (VI != CurModule.Values.end()) {
291 if (Num < VI->second.size())
292 return VI->second[Num];
293 Num -= VI->second.size();
Chris Lattner00950542001-06-06 20:29:01 +0000294 }
295
296 // Make sure that our type is within bounds
Chris Lattner735f2702004-07-08 22:30:50 +0000297 VI = CurFun.Values.find(Ty);
Chris Lattner16af11d2004-02-09 21:03:38 +0000298 if (VI == CurFun.Values.end()) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000299
300 // Check that the number is within bounds...
Chris Lattner16af11d2004-02-09 21:03:38 +0000301 if (VI->second.size() <= Num) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000302
Chris Lattner16af11d2004-02-09 21:03:38 +0000303 return VI->second[Num];
Chris Lattner00950542001-06-06 20:29:01 +0000304 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000305
Chris Lattner1a1cb112001-09-30 22:46:54 +0000306 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000307 Value *N = lookupInSymbolTable(Ty, std::string(D.Name));
Chris Lattner2079fde2001-10-13 06:41:08 +0000308 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000309
310 D.destroy(); // Free old strdup'd memory...
311 return N;
312 }
313
Chris Lattner2079fde2001-10-13 06:41:08 +0000314 // Check to make sure that "Ty" is an integral type, and that our
315 // value will fit into the specified type...
316 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattnerd78700d2002-08-16 21:14:40 +0000317 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
318 ThrowException("Signed integral constant '" +
319 itostr(D.ConstPool64) + "' is invalid for type '" +
320 Ty->getDescription() + "'!");
321 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000322
323 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000324 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
325 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000326 ThrowException("Integral constant '" + utostr(D.UConstPool64) +
327 "' is invalid or out of range!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000328 } else { // This is really a signed reference. Transmogrify.
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000329 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000330 }
331 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000332 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000333 }
334
Chris Lattner2079fde2001-10-13 06:41:08 +0000335 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000336 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner2079fde2001-10-13 06:41:08 +0000337 ThrowException("FP constant invalid for type!!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000338 return ConstantFP::get(Ty, D.ConstPoolFP);
Chris Lattner2079fde2001-10-13 06:41:08 +0000339
340 case ValID::ConstNullVal: // Is it a null value?
Chris Lattner9b625032002-05-06 16:15:30 +0000341 if (!isa<PointerType>(Ty))
Chris Lattner2079fde2001-10-13 06:41:08 +0000342 ThrowException("Cannot create a a non pointer null!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000343 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner2079fde2001-10-13 06:41:08 +0000344
Chris Lattnerd78700d2002-08-16 21:14:40 +0000345 case ValID::ConstantVal: // Fully resolved constant?
346 if (D.ConstantValue->getType() != Ty)
347 ThrowException("Constant expression type different from required type!");
348 return D.ConstantValue;
349
Chris Lattner30c89792001-09-07 16:35:17 +0000350 default:
351 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000352 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000353 } // End of switch
354
Chris Lattner2079fde2001-10-13 06:41:08 +0000355 assert(0 && "Unhandled case!");
356 return 0;
357}
358
359
360// getVal - This function is identical to getValNonImprovising, except that if a
361// value is not already defined, it "improvises" by creating a placeholder var
362// that looks and acts just like the requested variable. When the value is
363// defined later, all uses of the placeholder variable are replaced with the
364// real thing.
365//
366static Value *getVal(const Type *Ty, const ValID &D) {
Chris Lattner2079fde2001-10-13 06:41:08 +0000367
368 // See if the value has already been defined...
369 Value *V = getValNonImprovising(Ty, D);
370 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000371
372 // If we reached here, we referenced either a symbol that we don't know about
373 // or an id number that hasn't been read yet. We may be referencing something
374 // forward, so just create an entry to be resolved later and get to it...
375 //
Chris Lattner00950542001-06-06 20:29:01 +0000376 Value *d = 0;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000377 switch (Ty->getTypeID()) {
Chris Lattner30c89792001-09-07 16:35:17 +0000378 case Type::LabelTyID: d = new BBPlaceHolder(Ty, D); break;
Chris Lattner30c89792001-09-07 16:35:17 +0000379 default: d = new ValuePlaceHolder(Ty, D); break;
Chris Lattner00950542001-06-06 20:29:01 +0000380 }
381
382 assert(d != 0 && "How did we not make something?");
Chris Lattner79df7c02002-03-26 18:01:55 +0000383 if (inFunctionScope())
Chris Lattner394f2eb2003-10-16 20:12:13 +0000384 InsertValue(d, CurFun.LateResolveValues);
Chris Lattner386a3b72001-10-16 19:54:17 +0000385 else
386 InsertValue(d, CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000387 return d;
388}
389
390
391//===----------------------------------------------------------------------===//
392// Code to handle forward references in instructions
393//===----------------------------------------------------------------------===//
394//
395// This code handles the late binding needed with statements that reference
396// values not defined yet... for example, a forward branch, or the PHI node for
397// a loop body.
398//
Chris Lattner394f2eb2003-10-16 20:12:13 +0000399// This keeps a table (CurFun.LateResolveValues) of all such forward references
Chris Lattner00950542001-06-06 20:29:01 +0000400// and back patchs after we are done.
401//
402
403// ResolveDefinitions - If we could not resolve some defs at parsing
404// time (forward branches, phi functions for loops, etc...) resolve the
405// defs now...
406//
Chris Lattner735f2702004-07-08 22:30:50 +0000407static void ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers,
408 std::map<const Type*,ValueList> *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000409 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Chris Lattner735f2702004-07-08 22:30:50 +0000410 for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(),
Chris Lattner16af11d2004-02-09 21:03:38 +0000411 E = LateResolvers.end(); LRI != E; ++LRI) {
412 ValueList &List = LRI->second;
413 while (!List.empty()) {
414 Value *V = List.back();
415 List.pop_back();
Chris Lattner00950542001-06-06 20:29:01 +0000416 ValID &DID = getValIDFromPlaceHolder(V);
417
Chris Lattner735f2702004-07-08 22:30:50 +0000418 Value *TheRealValue = getValNonImprovising(LRI->first, DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000419 if (TheRealValue) {
420 V->replaceAllUsesWith(TheRealValue);
421 delete V;
422 } else if (FutureLateResolvers) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000423 // Functions have their unresolved items forwarded to the module late
Chris Lattner386a3b72001-10-16 19:54:17 +0000424 // resolver table
425 InsertValue(V, *FutureLateResolvers);
426 } else {
Chris Lattner9705a152002-05-02 19:27:42 +0000427 if (DID.Type == ValID::NameVal)
Chris Lattner30c89792001-09-07 16:35:17 +0000428 ThrowException("Reference to an invalid definition: '" +DID.getName()+
429 "' of type '" + V->getType()->getDescription() + "'",
430 getLineNumFromPlaceHolder(V));
431 else
432 ThrowException("Reference to an invalid definition: #" +
433 itostr(DID.Num) + " of type '" +
434 V->getType()->getDescription() + "'",
435 getLineNumFromPlaceHolder(V));
436 }
Chris Lattner00950542001-06-06 20:29:01 +0000437 }
438 }
439
440 LateResolvers.clear();
441}
442
Chris Lattner4a42e902001-10-22 05:56:09 +0000443// ResolveTypeTo - A brand new type was just declared. This means that (if
444// name is not null) things referencing Name can be resolved. Otherwise, things
445// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000446//
Chris Lattner4a42e902001-10-22 05:56:09 +0000447static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner9232b992003-04-22 18:42:41 +0000448 std::vector<PATypeHolder> &Types = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000449 CurFun.Types : CurModule.Types;
Chris Lattner00950542001-06-06 20:29:01 +0000450
Chris Lattner4a42e902001-10-22 05:56:09 +0000451 ValID D;
452 if (Name) D = ValID::create(Name);
453 else D = ValID::create((int)Types.size());
Chris Lattner30c89792001-09-07 16:35:17 +0000454
Chris Lattner9232b992003-04-22 18:42:41 +0000455 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000456 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
Chris Lattner4a42e902001-10-22 05:56:09 +0000457
Chris Lattner9232b992003-04-22 18:42:41 +0000458 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000459 if (I != LateResolver.end()) {
Chris Lattner51727be2002-06-04 21:58:56 +0000460 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner4a42e902001-10-22 05:56:09 +0000461 LateResolver.erase(I);
462 }
463}
464
465// ResolveTypes - At this point, all types should be resolved. Any that aren't
466// are errors.
467//
Chris Lattner9232b992003-04-22 18:42:41 +0000468static void ResolveTypes(std::map<ValID, PATypeHolder> &LateResolveTypes) {
Chris Lattner4a42e902001-10-22 05:56:09 +0000469 if (!LateResolveTypes.empty()) {
Chris Lattner82269592001-10-22 06:01:08 +0000470 const ValID &DID = LateResolveTypes.begin()->first;
Chris Lattner4a42e902001-10-22 05:56:09 +0000471
472 if (DID.Type == ValID::NameVal)
Chris Lattner82269592001-10-22 06:01:08 +0000473 ThrowException("Reference to an invalid type: '" +DID.getName() + "'");
Chris Lattner4a42e902001-10-22 05:56:09 +0000474 else
Chris Lattner82269592001-10-22 06:01:08 +0000475 ThrowException("Reference to an invalid type: #" + itostr(DID.Num));
Chris Lattner30c89792001-09-07 16:35:17 +0000476 }
477}
478
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000479
Chris Lattner8ab406d2004-07-13 07:59:27 +0000480static void setValueNameInternal(Value *V, const std::string &Name,
481 SymbolTable &ST) {
482 if (V->getType() == Type::VoidTy)
483 ThrowException("Can't assign name '" + Name + "' to value with void type!");
484
485 // Set the name
486 V->setName(Name, &ST);
Chris Lattner8ab406d2004-07-13 07:59:27 +0000487}
488
Chris Lattner1781aca2001-09-18 04:00:54 +0000489// setValueName - Set the specified value to the name given. The name may be
490// null potentially, in which case this is a noop. The string passed in is
Chris Lattner8ab406d2004-07-13 07:59:27 +0000491// assumed to be a malloc'd string buffer, and is free'd by this function.
492//
493static void setValueName(Value *V, char *NameStr) {
494 if (NameStr) {
495 std::string Name(NameStr); // Copy string
496 free(NameStr); // Free old string
497
498 assert(inFunctionScope() && "Must be in function scope!");
499 SymbolTable &ST = CurFun.CurrentFunction->getSymbolTable();
500 if (ST.lookup(V->getType(), Name))
501 ThrowException("Redefinition of value named '" + Name + "' in the '" +
502 V->getType()->getDescription() + "' type plane!");
503
504 setValueNameInternal(V, Name, ST);
505 }
506}
507
508// setValueNameMergingDuplicates - Set the specified value to the name given.
509// The name may be null potentially, in which case this is a noop. The string
510// passed in is assumed to be a malloc'd string buffer, and is free'd by this
511// function.
Chris Lattner1781aca2001-09-18 04:00:54 +0000512//
Chris Lattnerb7474512001-10-03 15:39:04 +0000513// This function returns true if the value has already been defined, but is
514// allowed to be redefined in the specified context. If the name is a new name
515// for the typeplane, false is returned.
516//
Chris Lattner8ab406d2004-07-13 07:59:27 +0000517static bool setValueNameMergingDuplicates(Value *V, char *NameStr) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000518 if (NameStr == 0) return false;
Chris Lattner8ab406d2004-07-13 07:59:27 +0000519
Chris Lattner9232b992003-04-22 18:42:41 +0000520 std::string Name(NameStr); // Copy string
Chris Lattner1781aca2001-09-18 04:00:54 +0000521 free(NameStr); // Free old string
522
Chris Lattner8ab406d2004-07-13 07:59:27 +0000523 // FIXME: If we eliminated the function constant pool (which we should), this
524 // would just unconditionally look at the module symtab.
Chris Lattner6e6026b2002-11-20 18:36:02 +0000525 SymbolTable &ST = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000526 CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner6e6026b2002-11-20 18:36:02 +0000527 CurModule.CurrentModule->getSymbolTable();
Chris Lattner30c89792001-09-07 16:35:17 +0000528
Reid Spencer75719bf2004-05-26 21:48:31 +0000529 Value *Existing = ST.lookup(V->getType(), Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000530 if (Existing) { // Inserting a name that is already defined???
Chris Lattner8ab406d2004-07-13 07:59:27 +0000531 // We are a simple redefinition of a value, check to see if it is defined
532 // the same as the old one...
533 if (GlobalVariable *EGV = dyn_cast<GlobalVariable>(Existing)) {
Chris Lattner43efcbf2001-10-03 19:35:57 +0000534 // We are allowed to redefine a global variable in two circumstances:
535 // 1. If at least one of the globals is uninitialized or
536 // 2. If both initializers have the same value.
537 //
Chris Lattner89219832001-10-03 19:35:04 +0000538 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000539 if (!EGV->hasInitializer() || !GV->hasInitializer() ||
540 EGV->getInitializer() == GV->getInitializer()) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000541
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000542 // Make sure the existing global version gets the initializer! Make
543 // sure that it also gets marked const if the new version is.
Chris Lattner89219832001-10-03 19:35:04 +0000544 if (GV->hasInitializer() && !EGV->hasInitializer())
545 EGV->setInitializer(GV->getInitializer());
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000546 if (GV->isConstant())
547 EGV->setConstant(true);
Chris Lattner4ad02e72003-04-16 20:28:45 +0000548 EGV->setLinkage(GV->getLinkage());
Chris Lattner89219832001-10-03 19:35:04 +0000549
Chris Lattner2079fde2001-10-13 06:41:08 +0000550 delete GV; // Destroy the duplicate!
Chris Lattner89219832001-10-03 19:35:04 +0000551 return true; // They are equivalent!
552 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000553 }
Chris Lattner8ab406d2004-07-13 07:59:27 +0000554 } else if (const Constant *C = dyn_cast<Constant>(Existing)) {
555 if (C == V) return true; // Constants are equal to themselves
Chris Lattner9636a912001-10-01 16:18:37 +0000556 }
Chris Lattner0b268fb2003-11-25 03:54:16 +0000557
Chris Lattner2079fde2001-10-13 06:41:08 +0000558 ThrowException("Redefinition of value named '" + Name + "' in the '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000559 V->getType()->getDescription() + "' type plane!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000560 }
Chris Lattner8ab406d2004-07-13 07:59:27 +0000561
562 setValueNameInternal(V, Name, ST);
Chris Lattnerb7474512001-10-03 15:39:04 +0000563 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000564}
565
Chris Lattner8ab406d2004-07-13 07:59:27 +0000566
567
Reid Spencer75719bf2004-05-26 21:48:31 +0000568// setTypeName - Set the specified type to the name given. The name may be
569// null potentially, in which case this is a noop. The string passed in is
570// assumed to be a malloc'd string buffer, and is freed by this function.
571//
572// This function returns true if the type has already been defined, but is
573// allowed to be redefined in the specified context. If the name is a new name
574// for the type plane, it is inserted and false is returned.
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000575static bool setTypeName(const Type *T, char *NameStr) {
Reid Spencer75719bf2004-05-26 21:48:31 +0000576 if (NameStr == 0) return false;
577
578 std::string Name(NameStr); // Copy string
579 free(NameStr); // Free old string
580
581 // We don't allow assigning names to void type
582 if (T == Type::VoidTy)
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000583 ThrowException("Can't assign name '" + Name + "' to the void type!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000584
585 SymbolTable &ST = inFunctionScope() ?
586 CurFun.CurrentFunction->getSymbolTable() :
587 CurModule.CurrentModule->getSymbolTable();
588
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000589 // Inserting a name that is already defined???
590 if (Type *Existing = ST.lookupType(Name)) {
Reid Spencer75719bf2004-05-26 21:48:31 +0000591 // There is only one case where this is allowed: when we are refining an
592 // opaque type. In this case, Existing will be an opaque type.
593 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
594 // We ARE replacing an opaque type!
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000595 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000596 return true;
597 }
598
599 // Otherwise, this is an attempt to redefine a type. That's okay if
600 // the redefinition is identical to the original. This will be so if
601 // Existing and T point to the same Type object. In this one case we
602 // allow the equivalent redefinition.
603 if (Existing == T) return true; // Yes, it's equal.
604
605 // Any other kind of (non-equivalent) redefinition is an error.
606 ThrowException("Redefinition of type named '" + Name + "' in the '" +
607 T->getDescription() + "' type plane!");
608 }
609
610 // Okay, its a newly named type. Set its name.
Chris Lattner8ca2dc02004-07-09 16:43:55 +0000611 if (!Name.empty()) ST.insert(Name, T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000612
Reid Spencer75719bf2004-05-26 21:48:31 +0000613 return false;
614}
Chris Lattner8896eda2001-07-09 19:38:36 +0000615
Chris Lattner30c89792001-09-07 16:35:17 +0000616//===----------------------------------------------------------------------===//
617// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000618//
Chris Lattner8896eda2001-07-09 19:38:36 +0000619
Chris Lattner3b073862004-02-09 03:19:29 +0000620// TypeContains - Returns true if Ty directly contains E in it.
Chris Lattner30c89792001-09-07 16:35:17 +0000621//
622static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3b073862004-02-09 03:19:29 +0000623 return find(Ty->subtype_begin(), Ty->subtype_end(), E) != Ty->subtype_end();
624}
625
626namespace {
627 struct UpRefRecord {
628 // NestingLevel - The number of nesting levels that need to be popped before
629 // this type is resolved.
630 unsigned NestingLevel;
631
632 // LastContainedTy - This is the type at the current binding level for the
633 // type. Every time we reduce the nesting level, this gets updated.
634 const Type *LastContainedTy;
635
636 // UpRefTy - This is the actual opaque type that the upreference is
637 // represented with.
638 OpaqueType *UpRefTy;
639
640 UpRefRecord(unsigned NL, OpaqueType *URTy)
641 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
642 };
Chris Lattner30c89792001-09-07 16:35:17 +0000643}
Chris Lattner698b56e2001-07-20 19:15:08 +0000644
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000645// UpRefs - A list of the outstanding upreferences that need to be resolved.
Chris Lattner3b073862004-02-09 03:19:29 +0000646static std::vector<UpRefRecord> UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000647
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000648/// HandleUpRefs - Every time we finish a new layer of types, this function is
649/// called. It loops through the UpRefs vector, which is a list of the
650/// currently active types. For each type, if the up reference is contained in
651/// the newly completed type, we decrement the level count. When the level
652/// count reaches zero, the upreferenced type is the type that is passed in:
653/// thus we can complete the cycle.
654///
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000655static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner2c37c182004-02-09 03:03:10 +0000656 if (!ty->isAbstract()) return ty;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000657 PATypeHolder Ty(ty);
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000658 UR_OUT("Type '" << Ty->getDescription() <<
Chris Lattner5084d032001-11-02 07:46:26 +0000659 "' newly formed. Resolving upreferences.\n" <<
660 UpRefs.size() << " upreferences active!\n");
Chris Lattner026a8ce2004-02-09 18:53:54 +0000661
662 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
663 // to zero), we resolve them all together before we resolve them to Ty. At
664 // the end of the loop, if there is anything to resolve to Ty, it will be in
665 // this variable.
666 OpaqueType *TypeToResolve = 0;
667
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000668 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Chris Lattner5084d032001-11-02 07:46:26 +0000669 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Chris Lattner30c89792001-09-07 16:35:17 +0000670 << UpRefs[i].second->getDescription() << ") = "
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000671 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner3b073862004-02-09 03:19:29 +0000672 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
673 // Decrement level of upreference
674 unsigned Level = --UpRefs[i].NestingLevel;
675 UpRefs[i].LastContainedTy = Ty;
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000676 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Chris Lattner30c89792001-09-07 16:35:17 +0000677 if (Level == 0) { // Upreference should be resolved!
Chris Lattner026a8ce2004-02-09 18:53:54 +0000678 if (!TypeToResolve) {
679 TypeToResolve = UpRefs[i].UpRefTy;
680 } else {
681 UR_OUT(" * Resolving upreference for "
682 << UpRefs[i].second->getDescription() << "\n";
683 std::string OldName = UpRefs[i].UpRefTy->getDescription());
684 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
685 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
686 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
687 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000688 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
689 --i; // Do not skip the next element...
Chris Lattner30c89792001-09-07 16:35:17 +0000690 }
691 }
Chris Lattner8896eda2001-07-09 19:38:36 +0000692 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000693
Chris Lattner026a8ce2004-02-09 18:53:54 +0000694 if (TypeToResolve) {
695 UR_OUT(" * Resolving upreference for "
696 << UpRefs[i].second->getDescription() << "\n";
Chris Lattner16af11d2004-02-09 21:03:38 +0000697 std::string OldName = TypeToResolve->getDescription());
Chris Lattner026a8ce2004-02-09 18:53:54 +0000698 TypeToResolve->refineAbstractTypeTo(Ty);
699 }
700
Chris Lattner8896eda2001-07-09 19:38:36 +0000701 return Ty;
702}
703
Chris Lattner30c89792001-09-07 16:35:17 +0000704
Chris Lattner00950542001-06-06 20:29:01 +0000705//===----------------------------------------------------------------------===//
706// RunVMAsmParser - Define an interface to this parser
707//===----------------------------------------------------------------------===//
708//
Chris Lattner9232b992003-04-22 18:42:41 +0000709Module *RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000710 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000711 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000712 llvmAsmlineno = 1; // Reset the current line number...
Chris Lattner99e7ab72003-10-18 05:53:13 +0000713 ObsoleteVarArgs = false;
Chris Lattner00950542001-06-06 20:29:01 +0000714
Chris Lattner75f20532003-04-22 18:02:52 +0000715 // Allocate a new module to read
716 CurModule.CurrentModule = new Module(Filename);
Chris Lattner42570982003-11-26 07:24:58 +0000717
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000718 yyparse(); // Parse the file, potentially throwing exception
Chris Lattner99e7ab72003-10-18 05:53:13 +0000719
Chris Lattner00950542001-06-06 20:29:01 +0000720 Module *Result = ParserResult;
Chris Lattner99e7ab72003-10-18 05:53:13 +0000721
722 // Check to see if they called va_start but not va_arg..
723 if (!ObsoleteVarArgs)
724 if (Function *F = Result->getNamedFunction("llvm.va_start"))
725 if (F->asize() == 1) {
726 std::cerr << "WARNING: this file uses obsolete features. "
727 << "Assemble and disassemble to update it.\n";
728 ObsoleteVarArgs = true;
729 }
730
Chris Lattner99e7ab72003-10-18 05:53:13 +0000731 if (ObsoleteVarArgs) {
732 // If the user is making use of obsolete varargs intrinsics, adjust them for
733 // the user.
734 if (Function *F = Result->getNamedFunction("llvm.va_start")) {
735 assert(F->asize() == 1 && "Obsolete va_start takes 1 argument!");
736
737 const Type *RetTy = F->getFunctionType()->getParamType(0);
738 RetTy = cast<PointerType>(RetTy)->getElementType();
739 Function *NF = Result->getOrInsertFunction("llvm.va_start", RetTy, 0);
740
741 while (!F->use_empty()) {
742 CallInst *CI = cast<CallInst>(F->use_back());
743 Value *V = new CallInst(NF, "", CI);
744 new StoreInst(V, CI->getOperand(1), CI);
745 CI->getParent()->getInstList().erase(CI);
746 }
747 Result->getFunctionList().erase(F);
748 }
749
750 if (Function *F = Result->getNamedFunction("llvm.va_end")) {
751 assert(F->asize() == 1 && "Obsolete va_end takes 1 argument!");
752 const Type *ArgTy = F->getFunctionType()->getParamType(0);
753 ArgTy = cast<PointerType>(ArgTy)->getElementType();
754 Function *NF = Result->getOrInsertFunction("llvm.va_end", Type::VoidTy,
755 ArgTy, 0);
756
757 while (!F->use_empty()) {
758 CallInst *CI = cast<CallInst>(F->use_back());
759 Value *V = new LoadInst(CI->getOperand(1), "", CI);
760 new CallInst(NF, V, "", CI);
761 CI->getParent()->getInstList().erase(CI);
762 }
763 Result->getFunctionList().erase(F);
764 }
765
766 if (Function *F = Result->getNamedFunction("llvm.va_copy")) {
767 assert(F->asize() == 2 && "Obsolete va_copy takes 2 argument!");
768 const Type *ArgTy = F->getFunctionType()->getParamType(0);
769 ArgTy = cast<PointerType>(ArgTy)->getElementType();
770 Function *NF = Result->getOrInsertFunction("llvm.va_copy", ArgTy,
771 ArgTy, 0);
772
773 while (!F->use_empty()) {
774 CallInst *CI = cast<CallInst>(F->use_back());
775 Value *V = new CallInst(NF, CI->getOperand(2), "", CI);
776 new StoreInst(V, CI->getOperand(1), CI);
777 CI->getParent()->getInstList().erase(CI);
778 }
779 Result->getFunctionList().erase(F);
780 }
781 }
782
Chris Lattner00950542001-06-06 20:29:01 +0000783 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
784 ParserResult = 0;
785
786 return Result;
787}
788
Brian Gaeked0fde302003-11-11 22:41:34 +0000789} // End llvm namespace
790
791using namespace llvm;
792
Chris Lattner00950542001-06-06 20:29:01 +0000793%}
794
795%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000796 llvm::Module *ModuleVal;
797 llvm::Function *FunctionVal;
798 std::pair<llvm::PATypeHolder*, char*> *ArgVal;
799 llvm::BasicBlock *BasicBlockVal;
800 llvm::TerminatorInst *TermInstVal;
801 llvm::Instruction *InstVal;
802 llvm::Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000803
Brian Gaeked0fde302003-11-11 22:41:34 +0000804 const llvm::Type *PrimType;
805 llvm::PATypeHolder *TypeVal;
806 llvm::Value *ValueVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000807
Brian Gaeked0fde302003-11-11 22:41:34 +0000808 std::vector<std::pair<llvm::PATypeHolder*,char*> > *ArgList;
809 std::vector<llvm::Value*> *ValueList;
810 std::list<llvm::PATypeHolder> *TypeList;
811 std::list<std::pair<llvm::Value*,
812 llvm::BasicBlock*> > *PHIList; // Represent the RHS of PHI node
813 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
814 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000815
Brian Gaeked0fde302003-11-11 22:41:34 +0000816 llvm::GlobalValue::LinkageTypes Linkage;
Chris Lattner30c89792001-09-07 16:35:17 +0000817 int64_t SInt64Val;
818 uint64_t UInt64Val;
819 int SIntVal;
820 unsigned UIntVal;
821 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000822 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000823
Chris Lattner30c89792001-09-07 16:35:17 +0000824 char *StrVal; // This memory is strdup'd!
Brian Gaeked0fde302003-11-11 22:41:34 +0000825 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000826
Brian Gaeked0fde302003-11-11 22:41:34 +0000827 llvm::Instruction::BinaryOps BinaryOpVal;
828 llvm::Instruction::TermOps TermOpVal;
829 llvm::Instruction::MemoryOps MemOpVal;
830 llvm::Instruction::OtherOps OtherOpVal;
831 llvm::Module::Endianness Endianness;
Chris Lattner00950542001-06-06 20:29:01 +0000832}
833
Chris Lattner79df7c02002-03-26 18:01:55 +0000834%type <ModuleVal> Module FunctionList
835%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000836%type <BasicBlockVal> BasicBlock InstructionList
837%type <TermInstVal> BBTerminatorInst
838%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000839%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000840%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000841%type <ArgList> ArgList ArgListH
842%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000843%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000844%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000845%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000846%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000847%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +0000848%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner15c9c032003-09-08 18:20:29 +0000849%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattner4ad02e72003-04-16 20:28:45 +0000850%type <Linkage> OptLinkage
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000851%type <Endianness> BigOrLittle
Chris Lattner00950542001-06-06 20:29:01 +0000852
Chris Lattner2079fde2001-10-13 06:41:08 +0000853// ValueRef - Unresolved reference to a definition or BB
854%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000855%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000856// Tokens and types for handling constant integer values
857//
858// ESINT64VAL - A negative number within long long range
859%token <SInt64Val> ESINT64VAL
860
861// EUINT64VAL - A positive number within uns. long long range
862%token <UInt64Val> EUINT64VAL
863%type <SInt64Val> EINT64VAL
864
865%token <SIntVal> SINTVAL // Signed 32 bit ints...
866%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
867%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000868%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000869
870// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000871%type <TypeVal> Types TypesV UpRTypes UpRTypesV
872%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000873%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
874%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000875
Chris Lattner6c23f572003-08-22 05:42:10 +0000876%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
877%type <StrVal> Name OptName OptAssign
Chris Lattner00950542001-06-06 20:29:01 +0000878
879
Chris Lattner91ef4602004-03-31 03:49:47 +0000880%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Chris Lattner15c9c032003-09-08 18:20:29 +0000881%token DECLARE GLOBAL CONSTANT VOLATILE
Chris Lattnera58e3a12004-02-08 21:48:25 +0000882%token TO DOTDOTDOT NULL_TOK CONST INTERNAL LINKONCE WEAK APPENDING
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000883%token OPAQUE NOT EXTERNAL TARGET ENDIAN POINTERSIZE LITTLE BIG
Chris Lattner00950542001-06-06 20:29:01 +0000884
885// Basic Block Terminating Operators
Chris Lattner36143fc2003-09-08 18:54:55 +0000886%token <TermOpVal> RET BR SWITCH INVOKE UNWIND
Chris Lattner00950542001-06-06 20:29:01 +0000887
Chris Lattner00950542001-06-06 20:29:01 +0000888// Binary Operators
889%type <BinaryOpVal> BinaryOps // all the binary operators
Chris Lattner4a6482b2002-09-10 19:57:26 +0000890%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattner42c9e772001-10-20 09:32:59 +0000891%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000892%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000893
894// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000895%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000896
Chris Lattner027dcc52001-07-08 21:10:27 +0000897// Other Operators
898%type <OtherOpVal> ShiftOps
Chris Lattner76f0ff32004-03-12 05:51:36 +0000899%token <OtherOpVal> PHI_TOK CALL CAST SELECT SHL SHR VAARG VANEXT
Chris Lattner99e7ab72003-10-18 05:53:13 +0000900%token VA_ARG // FIXME: OBSOLETE
Chris Lattner027dcc52001-07-08 21:10:27 +0000901
Chris Lattner00950542001-06-06 20:29:01 +0000902%start Module
903%%
904
905// Handle constant integer size restriction and conversion...
906//
Chris Lattner51727be2002-06-04 21:58:56 +0000907INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +0000908INTVAL : UINTVAL {
909 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
910 ThrowException("Value too large for type!");
911 $$ = (int32_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000912};
Chris Lattner00950542001-06-06 20:29:01 +0000913
914
Chris Lattner51727be2002-06-04 21:58:56 +0000915EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +0000916EINT64VAL : EUINT64VAL {
917 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
918 ThrowException("Value too large for type!");
919 $$ = (int64_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000920};
Chris Lattner00950542001-06-06 20:29:01 +0000921
Chris Lattner00950542001-06-06 20:29:01 +0000922// Operations that are notably excluded from this list include:
923// RET, BR, & SWITCH because they end basic blocks and are treated specially.
924//
Chris Lattner4a6482b2002-09-10 19:57:26 +0000925ArithmeticOps: ADD | SUB | MUL | DIV | REM;
926LogicalOps : AND | OR | XOR;
927SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
928BinaryOps : ArithmeticOps | LogicalOps | SetCondOps;
929
Chris Lattner51727be2002-06-04 21:58:56 +0000930ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +0000931
Chris Lattnere98dda62001-07-14 06:10:16 +0000932// These are some types that allow classification if we only want a particular
933// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +0000934SIntType : LONG | INT | SHORT | SBYTE;
935UIntType : ULONG | UINT | USHORT | UBYTE;
936IntType : SIntType | UIntType;
937FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +0000938
Chris Lattnere98dda62001-07-14 06:10:16 +0000939// OptAssign - Value producing statements have an optional assignment component
Chris Lattner6c23f572003-08-22 05:42:10 +0000940OptAssign : Name '=' {
Chris Lattner00950542001-06-06 20:29:01 +0000941 $$ = $1;
942 }
943 | /*empty*/ {
944 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +0000945 };
Chris Lattner00950542001-06-06 20:29:01 +0000946
Chris Lattner4ad02e72003-04-16 20:28:45 +0000947OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
948 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
Chris Lattner72ac148d2003-10-16 18:29:00 +0000949 WEAK { $$ = GlobalValue::WeakLinkage; } |
Chris Lattner4ad02e72003-04-16 20:28:45 +0000950 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
951 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattner30c89792001-09-07 16:35:17 +0000952
953//===----------------------------------------------------------------------===//
954// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +0000955// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +0000956// access to it, a user must explicitly use TypesV.
957//
958
959// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +0000960TypesV : Types | VOID { $$ = new PATypeHolder($1); };
961UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +0000962
963Types : UpRTypes {
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000964 if (!UpRefs.empty())
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000965 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
966 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000967 };
Chris Lattner30c89792001-09-07 16:35:17 +0000968
969
970// Derived types are added later...
971//
Chris Lattner51727be2002-06-04 21:58:56 +0000972PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
973PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000974UpRTypes : OPAQUE {
975 $$ = new PATypeHolder(OpaqueType::get());
976 }
977 | PrimType {
978 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +0000979 };
Chris Lattnerd78700d2002-08-16 21:14:40 +0000980UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000981 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +0000982};
Chris Lattner30c89792001-09-07 16:35:17 +0000983
Chris Lattner30c89792001-09-07 16:35:17 +0000984// Include derived types in the Types production.
985//
986UpRTypes : '\\' EUINT64VAL { // Type UpReference
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000987 if ($2 > (uint64_t)~0U) ThrowException("Value out of range!");
Chris Lattner30c89792001-09-07 16:35:17 +0000988 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3b073862004-02-09 03:19:29 +0000989 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000990 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +0000991 UR_OUT("New Upreference!\n");
992 }
Chris Lattner79df7c02002-03-26 18:01:55 +0000993 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner9232b992003-04-22 18:42:41 +0000994 std::vector<const Type*> Params;
Chris Lattner697954c2002-01-20 22:54:45 +0000995 mapto($3->begin(), $3->end(), std::back_inserter(Params),
Chris Lattner876dc572003-10-02 19:00:34 +0000996 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner2079fde2001-10-13 06:41:08 +0000997 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
998 if (isVarArg) Params.pop_back();
999
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001000 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +00001001 delete $3; // Delete the argument list
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001002 delete $1; // Delete the return type handle
Chris Lattner30c89792001-09-07 16:35:17 +00001003 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001004 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001005 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001006 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001007 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001008 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001009 std::vector<const Type*> Elements;
Chris Lattner697954c2002-01-20 22:54:45 +00001010 mapto($2->begin(), $2->end(), std::back_inserter(Elements),
Chris Lattner876dc572003-10-02 19:00:34 +00001011 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner30c89792001-09-07 16:35:17 +00001012
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001013 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001014 delete $2;
1015 }
1016 | '{' '}' { // Empty structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001017 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001018 }
1019 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001020 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001021 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001022 };
Chris Lattner30c89792001-09-07 16:35:17 +00001023
Chris Lattner7e708292002-06-25 16:13:24 +00001024// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +00001025// declaration type lists
1026//
1027TypeListI : UpRTypes {
Chris Lattner9232b992003-04-22 18:42:41 +00001028 $$ = new std::list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +00001029 $$->push_back(*$1); delete $1;
1030 }
1031 | TypeListI ',' UpRTypes {
1032 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +00001033 };
Chris Lattner30c89792001-09-07 16:35:17 +00001034
Chris Lattner7e708292002-06-25 16:13:24 +00001035// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +00001036ArgTypeListI : TypeListI
1037 | TypeListI ',' DOTDOTDOT {
1038 ($$=$1)->push_back(Type::VoidTy);
1039 }
1040 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001041 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +00001042 }
1043 | /*empty*/ {
Chris Lattner9232b992003-04-22 18:42:41 +00001044 $$ = new std::list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +00001045 };
Chris Lattner30c89792001-09-07 16:35:17 +00001046
Chris Lattnere98dda62001-07-14 06:10:16 +00001047// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +00001048// production is used ONLY to represent constants that show up AFTER a 'const',
1049// 'constant' or 'global' token at global scope. Constants that can be inlined
1050// into other expressions (such as integers and constexprs) are handled by the
1051// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +00001052//
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001053ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Chris Lattner949a3622003-07-23 15:30:06 +00001054 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001055 if (ATy == 0)
1056 ThrowException("Cannot make array constant with type: '" +
1057 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001058 const Type *ETy = ATy->getElementType();
1059 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +00001060
Chris Lattner30c89792001-09-07 16:35:17 +00001061 // Verify that we have the correct size...
1062 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +00001063 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +00001064 utostr($3->size()) + " arguments, but has size of " +
1065 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +00001066
Chris Lattner30c89792001-09-07 16:35:17 +00001067 // Verify all elements are correct type!
1068 for (unsigned i = 0; i < $3->size(); i++) {
1069 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +00001070 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001071 ETy->getDescription() +"' as required!\nIt is of type '"+
1072 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +00001073 }
1074
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001075 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001076 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001077 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001078 | Types '[' ']' {
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
1084 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +00001085 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +00001086 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +00001087 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattner9232b992003-04-22 18:42:41 +00001088 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +00001089 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001090 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001091 | Types 'c' STRINGCONSTANT {
Chris Lattner949a3622003-07-23 15:30:06 +00001092 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001093 if (ATy == 0)
1094 ThrowException("Cannot make array constant with type: '" +
1095 (*$1)->getDescription() + "'!");
1096
Chris Lattner30c89792001-09-07 16:35:17 +00001097 int NumElements = ATy->getNumElements();
1098 const Type *ETy = ATy->getElementType();
1099 char *EndStr = UnEscapeLexed($3, true);
1100 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +00001101 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +00001102 itostr((int)(EndStr-$3)) +
1103 " when array has size " + itostr(NumElements) + "!");
Chris Lattner9232b992003-04-22 18:42:41 +00001104 std::vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +00001105 if (ETy == Type::SByteTy) {
1106 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001107 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +00001108 } else if (ETy == Type::UByteTy) {
1109 for (char *C = $3; C != EndStr; ++C)
Chris Lattnerbae362f2003-01-30 22:24:26 +00001110 Vals.push_back(ConstantUInt::get(ETy, (unsigned char)*C));
Chris Lattner93750fa2001-07-28 17:48:55 +00001111 } else {
Chris Lattner30c89792001-09-07 16:35:17 +00001112 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +00001113 ThrowException("Cannot build string arrays of non byte sized elements!");
1114 }
Chris Lattner30c89792001-09-07 16:35:17 +00001115 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001116 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +00001117 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001118 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001119 | Types '{' ConstVector '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001120 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001121 if (STy == 0)
1122 ThrowException("Cannot make struct constant with type: '" +
1123 (*$1)->getDescription() + "'!");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001124
Chris Lattnerc6212f12003-05-21 16:06:56 +00001125 if ($3->size() != STy->getNumContainedTypes())
1126 ThrowException("Illegal number of initializers for structure type!");
1127
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001128 // Check to ensure that constants are compatible with the type initializer!
1129 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Chris Lattnerd21cd802004-02-09 04:37:31 +00001130 if ((*$3)[i]->getType() != STy->getElementType(i))
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001131 ThrowException("Expected type '" +
Chris Lattnerd21cd802004-02-09 04:37:31 +00001132 STy->getElementType(i)->getDescription() +
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001133 "' for element #" + utostr(i) +
1134 " of structure initializer!");
1135
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001136 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001137 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001138 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001139 | Types '{' '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001140 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001141 if (STy == 0)
1142 ThrowException("Cannot make struct constant with type: '" +
1143 (*$1)->getDescription() + "'!");
1144
1145 if (STy->getNumContainedTypes() != 0)
1146 ThrowException("Illegal number of initializers for structure type!");
1147
1148 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1149 delete $1;
1150 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001151 | Types NULL_TOK {
Chris Lattner949a3622003-07-23 15:30:06 +00001152 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001153 if (PTy == 0)
1154 ThrowException("Cannot make null pointer constant with type: '" +
1155 (*$1)->getDescription() + "'!");
1156
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001157 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001158 delete $1;
1159 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001160 | Types SymbolicValueRef {
Chris Lattner949a3622003-07-23 15:30:06 +00001161 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001162 if (Ty == 0)
1163 ThrowException("Global const reference must be a pointer type!");
1164
Chris Lattner3101c252002-08-15 17:58:33 +00001165 // ConstExprs can exist in the body of a function, thus creating
1166 // ConstantPointerRefs whenever they refer to a variable. Because we are in
1167 // the context of a function, getValNonImprovising will search the functions
1168 // symbol table instead of the module symbol table for the global symbol,
1169 // which throws things all off. To get around this, we just tell
1170 // getValNonImprovising that we are at global scope here.
1171 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001172 Function *SavedCurFn = CurFun.CurrentFunction;
1173 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001174
Chris Lattner2079fde2001-10-13 06:41:08 +00001175 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001176
Chris Lattner394f2eb2003-10-16 20:12:13 +00001177 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001178
Chris Lattner2079fde2001-10-13 06:41:08 +00001179 // If this is an initializer for a constant pointer, which is referencing a
1180 // (currently) undefined variable, create a stub now that shall be replaced
1181 // in the future with the right type of variable.
1182 //
1183 if (V == 0) {
1184 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1185 const PointerType *PT = cast<PointerType>(Ty);
1186
1187 // First check to see if the forward references value is already created!
1188 PerModuleInfo::GlobalRefsType::iterator I =
Chris Lattner9232b992003-04-22 18:42:41 +00001189 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001190
1191 if (I != CurModule.GlobalRefs.end()) {
1192 V = I->second; // Placeholder already exists, use it...
Chris Lattnera989b232003-12-23 20:05:15 +00001193 $2.destroy();
Chris Lattner2079fde2001-10-13 06:41:08 +00001194 } else {
Chris Lattner2079fde2001-10-13 06:41:08 +00001195 // Create a placeholder for the global variable reference...
Chris Lattner7a176752001-12-04 00:03:30 +00001196 GlobalVariable *GV = new GlobalVariable(PT->getElementType(),
Chris Lattner4ad02e72003-04-16 20:28:45 +00001197 false,
1198 GlobalValue::ExternalLinkage);
Chris Lattner2079fde2001-10-13 06:41:08 +00001199 // Keep track of the fact that we have a forward ref to recycle it
Chris Lattner9232b992003-04-22 18:42:41 +00001200 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
Chris Lattner2079fde2001-10-13 06:41:08 +00001201
1202 // Must temporarily push this value into the module table...
1203 CurModule.CurrentModule->getGlobalList().push_back(GV);
1204 V = GV;
1205 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001206 }
1207
Chris Lattner2079fde2001-10-13 06:41:08 +00001208 GlobalValue *GV = cast<GlobalValue>(V);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001209 $$ = ConstantPointerRef::get(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001210 delete $1; // Free the type handle
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001211 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001212 | Types ConstExpr {
1213 if ($1->get() != $2->getType())
1214 ThrowException("Mismatched types for constant expression!");
1215 $$ = $2;
1216 delete $1;
Chris Lattnerf4600482003-06-28 20:01:34 +00001217 }
1218 | Types ZEROINITIALIZER {
1219 $$ = Constant::getNullValue($1->get());
1220 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001221 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001222
Chris Lattnere43f40b2002-10-09 00:25:32 +00001223ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001224 if (!ConstantSInt::isValueValidForType($1, $2))
1225 ThrowException("Constant value doesn't fit in type!");
1226 $$ = ConstantSInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001227 }
1228 | UIntType EUINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001229 if (!ConstantUInt::isValueValidForType($1, $2))
1230 ThrowException("Constant value doesn't fit in type!");
1231 $$ = ConstantUInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001232 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001233 | BOOL TRUETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001234 $$ = ConstantBool::True;
1235 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001236 | BOOL FALSETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001237 $$ = ConstantBool::False;
1238 }
1239 | FPType FPVAL { // Float & Double constants
1240 $$ = ConstantFP::get($1, $2);
1241 };
1242
Chris Lattner00950542001-06-06 20:29:01 +00001243
Chris Lattnerd78700d2002-08-16 21:14:40 +00001244ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattnerae711a82003-11-21 20:27:35 +00001245 if (!$3->getType()->isFirstClassType())
1246 ThrowException("cast constant expression from a non-primitive type: '" +
1247 $3->getType()->getDescription() + "'!");
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001248 if (!$5->get()->isFirstClassType())
1249 ThrowException("cast constant expression to a non-primitive type: '" +
1250 $5->get()->getDescription() + "'!");
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001251 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001252 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001253 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001254 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1255 if (!isa<PointerType>($3->getType()))
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001256 ThrowException("GetElementPtr requires a pointer operand!");
1257
Chris Lattner830b6f92004-04-05 01:30:04 +00001258 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
1259 // indices to uint struct indices for compatibility.
1260 generic_gep_type_iterator<std::vector<Value*>::iterator>
1261 GTI = gep_type_begin($3->getType(), $4->begin(), $4->end()),
1262 GTE = gep_type_end($3->getType(), $4->begin(), $4->end());
1263 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
1264 if (isa<StructType>(*GTI)) // Only change struct indices
1265 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
1266 if (CUI->getType() == Type::UByteTy)
1267 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
1268
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001269 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001270 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001271 if (!IdxTy)
1272 ThrowException("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001273
Chris Lattner9232b992003-04-22 18:42:41 +00001274 std::vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001275 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1276 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001277 IdxVec.push_back(C);
1278 else
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001279 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001280
Chris Lattnerd78700d2002-08-16 21:14:40 +00001281 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001282
Chris Lattnerd78700d2002-08-16 21:14:40 +00001283 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001284 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001285 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1286 if ($3->getType() != Type::BoolTy)
1287 ThrowException("Select condition must be of boolean type!");
1288 if ($5->getType() != $7->getType())
1289 ThrowException("Select operand types must match!");
1290 $$ = ConstantExpr::getSelect($3, $5, $7);
1291 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001292 | BinaryOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001293 if ($3->getType() != $5->getType())
1294 ThrowException("Binary operator types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001295 $$ = ConstantExpr::get($1, $3, $5);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001296 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001297 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001298 if ($5->getType() != Type::UByteTy)
1299 ThrowException("Shift count for shift constant must be unsigned byte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001300 if (!$3->getType()->isInteger())
1301 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerb16689b2004-01-12 19:08:43 +00001302 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner699f1eb2002-08-14 17:12:33 +00001303 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001304
1305
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001306// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001307ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001308 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001309 }
1310 | ConstVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001311 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001312 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001313 };
Chris Lattner00950542001-06-06 20:29:01 +00001314
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001315
Chris Lattner1781aca2001-09-18 04:00:54 +00001316// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001317GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001318
Chris Lattner00950542001-06-06 20:29:01 +00001319
Chris Lattner0e73ce62002-05-02 19:11:13 +00001320//===----------------------------------------------------------------------===//
1321// Rules to match Modules
1322//===----------------------------------------------------------------------===//
1323
1324// Module rule: Capture the result of parsing the whole file into a result
1325// variable...
1326//
1327Module : FunctionList {
1328 $$ = ParserResult = $1;
1329 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001330};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001331
Chris Lattner7e708292002-06-25 16:13:24 +00001332// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001333//
1334FunctionList : FunctionList Function {
1335 $$ = $1;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001336 CurFun.FunctionDone();
Chris Lattner0e73ce62002-05-02 19:11:13 +00001337 }
1338 | FunctionList FunctionProto {
1339 $$ = $1;
1340 }
1341 | FunctionList IMPLEMENTATION {
1342 $$ = $1;
1343 }
1344 | ConstPool {
1345 $$ = CurModule.CurrentModule;
1346 // Resolve circular types before we parse the body of the module
1347 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001348 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001349
Chris Lattnere98dda62001-07-14 06:10:16 +00001350// ConstPool - Constants with optional names assigned to them.
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001351ConstPool : ConstPool OptAssign CONST ConstVal {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001352 // FIXME: THIS SHOULD REALLY BE ELIMINATED. It is totally unneeded.
1353 if (!setValueNameMergingDuplicates($4, $2))
Chris Lattneradf99702003-03-03 23:28:55 +00001354 InsertValue($4);
Chris Lattner00950542001-06-06 20:29:01 +00001355 }
Chris Lattner30c89792001-09-07 16:35:17 +00001356 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner4a42e902001-10-22 05:56:09 +00001357 // Eagerly resolve types. This is not an optimization, this is a
1358 // requirement that is due to the fact that we could have this:
1359 //
1360 // %list = type { %list * }
1361 // %list = type { %list * } ; repeated type decl
1362 //
1363 // If types are not resolved eagerly, then the two types will not be
1364 // determined to be the same type!
1365 //
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001366 ResolveTypeTo($2, *$4);
Chris Lattner4a42e902001-10-22 05:56:09 +00001367
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001368 if (!setTypeName(*$4, $2) && !$2) {
1369 // If this is a named type that is not a redefinition, add it to the slot
1370 // table.
1371 InsertType(*$4, inFunctionScope() ? CurFun.Types : CurModule.Types);
Chris Lattner30c89792001-09-07 16:35:17 +00001372 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001373
1374 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001375 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001376 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001377 }
Chris Lattner4ad02e72003-04-16 20:28:45 +00001378 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Chris Lattnerdda71962001-11-26 18:54:16 +00001379 const Type *Ty = $5->getType();
Chris Lattner1781aca2001-09-18 04:00:54 +00001380 // Global declarations appear in Constant Pool
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001381 Constant *Initializer = $5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001382 if (Initializer == 0)
1383 ThrowException("Global value initializer is not a constant!");
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001384
Chris Lattnerdda71962001-11-26 18:54:16 +00001385 GlobalVariable *GV = new GlobalVariable(Ty, $4, $3, Initializer);
Chris Lattner8ab406d2004-07-13 07:59:27 +00001386 if (!setValueNameMergingDuplicates(GV, $2)) { // If not redefining...
Chris Lattnerb7474512001-10-03 15:39:04 +00001387 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001388 int Slot = InsertValue(GV, CurModule.Values);
1389
1390 if (Slot != -1) {
1391 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1392 } else {
1393 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1394 (char*)GV->getName().c_str()));
1395 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001396 }
Chris Lattner1781aca2001-09-18 04:00:54 +00001397 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001398 | ConstPool OptAssign EXTERNAL GlobalType Types {
1399 const Type *Ty = *$5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001400 // Global declarations appear in Constant Pool
Chris Lattner4ad02e72003-04-16 20:28:45 +00001401 GlobalVariable *GV = new GlobalVariable(Ty,$4,GlobalValue::ExternalLinkage);
Chris Lattner8ab406d2004-07-13 07:59:27 +00001402 if (!setValueNameMergingDuplicates(GV, $2)) { // If not redefining...
Chris Lattnerb7474512001-10-03 15:39:04 +00001403 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001404 int Slot = InsertValue(GV, CurModule.Values);
1405
1406 if (Slot != -1) {
1407 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1408 } else {
1409 assert(GV->hasName() && "Not named and not numbered!?");
1410 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1411 (char*)GV->getName().c_str()));
1412 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001413 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001414 delete $5;
Chris Lattnere98dda62001-07-14 06:10:16 +00001415 }
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001416 | ConstPool TARGET TargetDefinition {
1417 }
Chris Lattner00950542001-06-06 20:29:01 +00001418 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001419 };
Chris Lattner00950542001-06-06 20:29:01 +00001420
1421
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001422
1423BigOrLittle : BIG { $$ = Module::BigEndian; };
1424BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1425
1426TargetDefinition : ENDIAN '=' BigOrLittle {
1427 CurModule.CurrentModule->setEndianness($3);
1428 }
1429 | POINTERSIZE '=' EUINT64VAL {
1430 if ($3 == 32)
1431 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1432 else if ($3 == 64)
1433 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1434 else
1435 ThrowException("Invalid pointer size: '" + utostr($3) + "'!");
1436 };
1437
1438
Chris Lattner00950542001-06-06 20:29:01 +00001439//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001440// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001441//===----------------------------------------------------------------------===//
1442
Chris Lattner6c23f572003-08-22 05:42:10 +00001443Name : VAR_ID | STRINGCONSTANT;
1444OptName : Name | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001445
Chris Lattner6c23f572003-08-22 05:42:10 +00001446ArgVal : Types OptName {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001447 if (*$1 == Type::VoidTy)
1448 ThrowException("void typed arguments are invalid!");
Chris Lattner9232b992003-04-22 18:42:41 +00001449 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
Chris Lattner51727be2002-06-04 21:58:56 +00001450};
Chris Lattner00950542001-06-06 20:29:01 +00001451
Chris Lattner69da5cf2002-10-13 20:57:00 +00001452ArgListH : ArgListH ',' ArgVal {
1453 $$ = $1;
1454 $1->push_back(*$3);
1455 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001456 }
1457 | ArgVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001458 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
Chris Lattner69da5cf2002-10-13 20:57:00 +00001459 $$->push_back(*$1);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001460 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001461 };
Chris Lattner00950542001-06-06 20:29:01 +00001462
1463ArgList : ArgListH {
1464 $$ = $1;
1465 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001466 | ArgListH ',' DOTDOTDOT {
1467 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001468 $$->push_back(std::pair<PATypeHolder*,
1469 char*>(new PATypeHolder(Type::VoidTy), 0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001470 }
1471 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001472 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1473 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001474 }
Chris Lattner00950542001-06-06 20:29:01 +00001475 | /* empty */ {
1476 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001477 };
Chris Lattner00950542001-06-06 20:29:01 +00001478
Chris Lattner6c23f572003-08-22 05:42:10 +00001479FunctionHeaderH : TypesV Name '(' ArgList ')' {
Chris Lattner1f862af2003-04-16 18:13:57 +00001480 UnEscapeLexed($2);
Chris Lattner9232b992003-04-22 18:42:41 +00001481 std::string FunctionName($2);
Chris Lattnerdda71962001-11-26 18:54:16 +00001482
Chris Lattnerc282f5a2003-11-21 22:32:23 +00001483 if (!(*$1)->isFirstClassType() && *$1 != Type::VoidTy)
1484 ThrowException("LLVM functions cannot return aggregate types!");
1485
Chris Lattner9232b992003-04-22 18:42:41 +00001486 std::vector<const Type*> ParamTypeList;
Chris Lattner1f862af2003-04-16 18:13:57 +00001487 if ($4) { // If there are arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001488 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001489 I != $4->end(); ++I)
Chris Lattner69da5cf2002-10-13 20:57:00 +00001490 ParamTypeList.push_back(I->first->get());
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001491 }
Chris Lattner00950542001-06-06 20:29:01 +00001492
Chris Lattner2079fde2001-10-13 06:41:08 +00001493 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1494 if (isVarArg) ParamTypeList.pop_back();
1495
Chris Lattner1f862af2003-04-16 18:13:57 +00001496 const FunctionType *FT = FunctionType::get(*$1, ParamTypeList, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001497 const PointerType *PFT = PointerType::get(FT);
Chris Lattner1f862af2003-04-16 18:13:57 +00001498 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001499
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001500 Function *Fn = 0;
1501 // Is the function already in symtab?
1502 if ((Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1503 // Yes it is. If this is the case, either we need to be a forward decl,
1504 // or it needs to be.
Chris Lattner394f2eb2003-10-16 20:12:13 +00001505 if (!CurFun.isDeclare && !Fn->isExternal())
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001506 ThrowException("Redefinition of function '" + FunctionName + "'!");
1507
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001508 // Make sure to strip off any argument names so we can't get conflicts...
1509 for (Function::aiterator AI = Fn->abegin(), AE = Fn->aend(); AI != AE; ++AI)
1510 AI->setName("");
Chris Lattner5659dd12002-07-15 00:10:33 +00001511
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001512 } else { // Not already defined?
Chris Lattner63a22502004-03-08 16:14:19 +00001513 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
1514 CurModule.CurrentModule);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001515 InsertValue(Fn, CurModule.Values);
Chris Lattner1f862af2003-04-16 18:13:57 +00001516 CurModule.DeclareNewGlobalValue(Fn, ValID::create($2));
Chris Lattnere1815642001-07-15 06:35:53 +00001517 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001518 free($2); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001519
Chris Lattner394f2eb2003-10-16 20:12:13 +00001520 CurFun.FunctionStart(Fn);
Chris Lattner00950542001-06-06 20:29:01 +00001521
Chris Lattner7e708292002-06-25 16:13:24 +00001522 // Add all of the arguments we parsed to the function...
Chris Lattner1f862af2003-04-16 18:13:57 +00001523 if ($4) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001524 if (isVarArg) { // Nuke the last entry
Chris Lattner1f862af2003-04-16 18:13:57 +00001525 assert($4->back().first->get() == Type::VoidTy && $4->back().second == 0&&
Chris Lattner69da5cf2002-10-13 20:57:00 +00001526 "Not a varargs marker!");
Chris Lattner1f862af2003-04-16 18:13:57 +00001527 delete $4->back().first;
1528 $4->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00001529 }
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001530 Function::aiterator ArgIt = Fn->abegin();
Chris Lattner9232b992003-04-22 18:42:41 +00001531 for (std::vector<std::pair<PATypeHolder*, char*> >::iterator I =$4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001532 I != $4->end(); ++I, ++ArgIt) {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001533 delete I->first; // Delete the typeholder...
1534
Chris Lattner8ab406d2004-07-13 07:59:27 +00001535 setValueName(ArgIt, I->second); // Insert arg into symtab...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001536 InsertValue(ArgIt);
Chris Lattner00950542001-06-06 20:29:01 +00001537 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001538
Chris Lattner1f862af2003-04-16 18:13:57 +00001539 delete $4; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00001540 }
Chris Lattner51727be2002-06-04 21:58:56 +00001541};
Chris Lattner00950542001-06-06 20:29:01 +00001542
Chris Lattner9b02cc32002-05-03 18:23:48 +00001543BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1544
Chris Lattner4ad02e72003-04-16 20:28:45 +00001545FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00001546 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001547
Chris Lattner4ad02e72003-04-16 20:28:45 +00001548 // Make sure that we keep track of the linkage type even if there was a
1549 // previous "declare".
1550 $$->setLinkage($1);
Chris Lattner1f862af2003-04-16 18:13:57 +00001551
Chris Lattner7e708292002-06-25 16:13:24 +00001552 // Resolve circular types before we parse the body of the function.
Chris Lattner394f2eb2003-10-16 20:12:13 +00001553 ResolveTypes(CurFun.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001554};
Chris Lattner00950542001-06-06 20:29:01 +00001555
Chris Lattner9b02cc32002-05-03 18:23:48 +00001556END : ENDTOK | '}'; // Allow end of '}' to end a function
1557
Chris Lattner79df7c02002-03-26 18:01:55 +00001558Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001559 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001560};
Chris Lattner00950542001-06-06 20:29:01 +00001561
Chris Lattner394f2eb2003-10-16 20:12:13 +00001562FunctionProto : DECLARE { CurFun.isDeclare = true; } FunctionHeaderH {
1563 $$ = CurFun.CurrentFunction;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001564 CurFun.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001565};
Chris Lattner00950542001-06-06 20:29:01 +00001566
1567//===----------------------------------------------------------------------===//
1568// Rules to match Basic Blocks
1569//===----------------------------------------------------------------------===//
1570
1571ConstValueRef : ESINT64VAL { // A reference to a direct constant
1572 $$ = ValID::create($1);
1573 }
1574 | EUINT64VAL {
1575 $$ = ValID::create($1);
1576 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001577 | FPVAL { // Perhaps it's an FP constant?
1578 $$ = ValID::create($1);
1579 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001580 | TRUETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001581 $$ = ValID::create(ConstantBool::True);
Chris Lattner00950542001-06-06 20:29:01 +00001582 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001583 | FALSETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001584 $$ = ValID::create(ConstantBool::False);
Chris Lattner00950542001-06-06 20:29:01 +00001585 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001586 | NULL_TOK {
1587 $$ = ValID::createNull();
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001588 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001589 | ConstExpr {
1590 $$ = ValID::create($1);
1591 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00001592
Chris Lattner2079fde2001-10-13 06:41:08 +00001593// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1594// another value.
1595//
1596SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001597 $$ = ValID::create($1);
1598 }
Chris Lattner6c23f572003-08-22 05:42:10 +00001599 | Name { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001600 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001601 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001602
1603// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001604ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001605
Chris Lattner00950542001-06-06 20:29:01 +00001606
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001607// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1608// type immediately preceeds the value reference, and allows complex constant
1609// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001610ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001611 $$ = getVal(*$1, $2); delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001612 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001613
Chris Lattner00950542001-06-06 20:29:01 +00001614BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001615 $$ = $1;
Chris Lattner00950542001-06-06 20:29:01 +00001616 }
Chris Lattner7e708292002-06-25 16:13:24 +00001617 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
Chris Lattner8ab406d2004-07-13 07:59:27 +00001618 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001619 };
Chris Lattner00950542001-06-06 20:29:01 +00001620
1621
1622// Basic blocks are terminated by branching instructions:
1623// br, br/cc, switch, ret
1624//
Chris Lattner2079fde2001-10-13 06:41:08 +00001625BasicBlock : InstructionList OptAssign BBTerminatorInst {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001626 setValueName($3, $2);
Chris Lattner2079fde2001-10-13 06:41:08 +00001627 InsertValue($3);
1628
1629 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001630 InsertValue($1);
1631 $$ = $1;
1632 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001633 | LABELSTR InstructionList OptAssign BBTerminatorInst {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001634 setValueName($4, $3);
Chris Lattner2079fde2001-10-13 06:41:08 +00001635 InsertValue($4);
1636
1637 $2->getInstList().push_back($4);
Chris Lattner8ab406d2004-07-13 07:59:27 +00001638 setValueName($2, $1);
Chris Lattner00950542001-06-06 20:29:01 +00001639
1640 InsertValue($2);
1641 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001642 };
Chris Lattner00950542001-06-06 20:29:01 +00001643
1644InstructionList : InstructionList Inst {
1645 $1->getInstList().push_back($2);
1646 $$ = $1;
1647 }
1648 | /* empty */ {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001649 $$ = CurBB = new BasicBlock("", CurFun.CurrentFunction);
Chris Lattner51727be2002-06-04 21:58:56 +00001650 };
Chris Lattner00950542001-06-06 20:29:01 +00001651
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001652BBTerminatorInst : RET ResolvedVal { // Return with a result...
1653 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001654 }
1655 | RET VOID { // Return with no result...
1656 $$ = new ReturnInst();
1657 }
1658 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner9636a912001-10-01 16:18:37 +00001659 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
Chris Lattner00950542001-06-06 20:29:01 +00001660 } // Conditional Branch...
1661 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner9636a912001-10-01 16:18:37 +00001662 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1663 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
Chris Lattner00950542001-06-06 20:29:01 +00001664 getVal(Type::BoolTy, $3));
1665 }
1666 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1667 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001668 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
Chris Lattner00950542001-06-06 20:29:01 +00001669 $$ = S;
1670
Chris Lattner9232b992003-04-22 18:42:41 +00001671 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00001672 E = $8->end();
1673 for (; I != E; ++I)
Chris Lattner4354f562003-08-23 23:14:52 +00001674 S->addCase(I->first, I->second);
Chris Lattnerf57a43d2004-04-17 23:49:15 +00001675 delete $8;
Chris Lattner00950542001-06-06 20:29:01 +00001676 }
Chris Lattner196850c2003-05-15 21:30:00 +00001677 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
1678 SwitchInst *S = new SwitchInst(getVal($2, $3),
1679 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
1680 $$ = S;
1681 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001682 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO ResolvedVal
Chris Lattnera58e3a12004-02-08 21:48:25 +00001683 UNWIND ResolvedVal {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001684 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001685 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001686
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001687 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1688 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001689 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001690 std::vector<const Type*> ParamTypes;
Chris Lattner2079fde2001-10-13 06:41:08 +00001691 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001692 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1693 I != E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001694 ParamTypes.push_back((*I)->getType());
1695 }
1696
1697 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1698 if (isVarArg) ParamTypes.pop_back();
1699
Chris Lattner79df7c02002-03-26 18:01:55 +00001700 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001701 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00001702 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001703
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001704 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00001705
1706 BasicBlock *Normal = dyn_cast<BasicBlock>($8);
1707 BasicBlock *Except = dyn_cast<BasicBlock>($10);
1708
1709 if (Normal == 0 || Except == 0)
1710 ThrowException("Invoke instruction without label destinations!");
1711
1712 // Create the call node...
1713 if (!$5) { // Has no arguments?
Chris Lattner9232b992003-04-22 18:42:41 +00001714 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001715 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001716 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00001717 // correctly!
1718 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001719 FunctionType::param_iterator I = Ty->param_begin();
1720 FunctionType::param_iterator E = Ty->param_end();
Chris Lattner9232b992003-04-22 18:42:41 +00001721 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001722
1723 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1724 if ((*ArgI)->getType() != *I)
1725 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001726 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001727
1728 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1729 ThrowException("Invalid number of parameters detected!");
1730
Chris Lattner6cdb0112001-11-26 16:54:11 +00001731 $$ = new InvokeInst(V, Normal, Except, *$5);
Chris Lattner2079fde2001-10-13 06:41:08 +00001732 }
Chris Lattner9cd42572003-12-23 22:18:36 +00001733 delete $2;
Chris Lattner2079fde2001-10-13 06:41:08 +00001734 delete $5;
Chris Lattner36143fc2003-09-08 18:54:55 +00001735 }
1736 | UNWIND {
1737 $$ = new UnwindInst();
Chris Lattner51727be2002-06-04 21:58:56 +00001738 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001739
1740
Chris Lattner00950542001-06-06 20:29:01 +00001741
1742JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1743 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001744 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001745 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($5, $6))));
Chris Lattner00950542001-06-06 20:29:01 +00001749 }
1750 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00001751 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001752 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001753
1754 if (V == 0)
1755 ThrowException("May only switch on a constant pool value!");
1756
Chris Lattner9232b992003-04-22 18:42:41 +00001757 $$->push_back(std::make_pair(V, cast<BasicBlock>(getVal($4, $5))));
Chris Lattner51727be2002-06-04 21:58:56 +00001758 };
Chris Lattner00950542001-06-06 20:29:01 +00001759
1760Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001761 // Is this definition named?? if so, assign the name...
Chris Lattner8ab406d2004-07-13 07:59:27 +00001762 setValueName($2, $1);
Chris Lattner00950542001-06-06 20:29:01 +00001763 InsertValue($2);
1764 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001765};
Chris Lattner00950542001-06-06 20:29:01 +00001766
Chris Lattnerc24d2082001-06-11 15:04:20 +00001767PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Chris Lattner9232b992003-04-22 18:42:41 +00001768 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
1769 $$->push_back(std::make_pair(getVal(*$1, $3),
1770 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
Chris Lattner30c89792001-09-07 16:35:17 +00001771 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001772 }
1773 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1774 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001775 $1->push_back(std::make_pair(getVal($1->front().first->getType(), $4),
1776 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
Chris Lattner51727be2002-06-04 21:58:56 +00001777 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00001778
1779
Chris Lattner30c89792001-09-07 16:35:17 +00001780ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner9232b992003-04-22 18:42:41 +00001781 $$ = new std::vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001782 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001783 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001784 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001785 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001786 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00001787 };
Chris Lattner00950542001-06-06 20:29:01 +00001788
1789// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00001790ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001791
Chris Lattner4a6482b2002-09-10 19:57:26 +00001792InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1793 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint())
1794 ThrowException("Arithmetic operator requires integer or FP operands!");
1795 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1796 if ($$ == 0)
1797 ThrowException("binary operator returned null!");
1798 delete $2;
1799 }
1800 | LogicalOps Types ValueRef ',' ValueRef {
1801 if (!(*$2)->isIntegral())
1802 ThrowException("Logical operator requires integral operands!");
1803 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1804 if ($$ == 0)
1805 ThrowException("binary operator returned null!");
1806 delete $2;
1807 }
1808 | SetCondOps Types ValueRef ',' ValueRef {
Chris Lattner1cff96a2002-09-10 22:37:46 +00001809 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001810 if ($$ == 0)
1811 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001812 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001813 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00001814 | NOT ResolvedVal {
1815 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
1816 << " Replacing with 'xor'.\n";
1817
1818 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
1819 if (Ones == 0)
1820 ThrowException("Expected integral type for not instruction!");
1821
1822 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00001823 if ($$ == 0)
Chris Lattner699f1eb2002-08-14 17:12:33 +00001824 ThrowException("Could not create a xor instruction!");
Chris Lattner09083092001-07-08 04:57:15 +00001825 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001826 | ShiftOps ResolvedVal ',' ResolvedVal {
1827 if ($4->getType() != Type::UByteTy)
1828 ThrowException("Shift amount must be ubyte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001829 if (!$2->getType()->isInteger())
1830 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001831 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001832 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001833 | CAST ResolvedVal TO Types {
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001834 if (!$4->get()->isFirstClassType())
1835 ThrowException("cast instruction to a non-primitive type: '" +
1836 $4->get()->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001837 $$ = new CastInst($2, *$4);
1838 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001839 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001840 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1841 if ($2->getType() != Type::BoolTy)
1842 ThrowException("select condition must be boolean!");
1843 if ($4->getType() != $6->getType())
1844 ThrowException("select value types should match!");
1845 $$ = new SelectInst($2, $4, $6);
1846 }
Chris Lattner8f77dae2003-05-08 02:44:12 +00001847 | VA_ARG ResolvedVal ',' Types {
Chris Lattner99e7ab72003-10-18 05:53:13 +00001848 // FIXME: This is emulation code for an obsolete syntax. This should be
1849 // removed at some point.
1850 if (!ObsoleteVarArgs) {
1851 std::cerr << "WARNING: this file uses obsolete features. "
1852 << "Assemble and disassemble to update it.\n";
1853 ObsoleteVarArgs = true;
1854 }
1855
1856 // First, load the valist...
1857 Instruction *CurVAList = new LoadInst($2, "");
1858 CurBB->getInstList().push_back(CurVAList);
1859
1860 // Emit the vaarg instruction.
1861 $$ = new VAArgInst(CurVAList, *$4);
1862
1863 // Now we must advance the pointer and update it in memory.
1864 Instruction *TheVANext = new VANextInst(CurVAList, *$4);
1865 CurBB->getInstList().push_back(TheVANext);
1866
1867 CurBB->getInstList().push_back(new StoreInst(TheVANext, $2));
1868 delete $4;
1869 }
1870 | VAARG ResolvedVal ',' Types {
1871 $$ = new VAArgInst($2, *$4);
1872 delete $4;
1873 }
1874 | VANEXT ResolvedVal ',' Types {
1875 $$ = new VANextInst($2, *$4);
Chris Lattner8f77dae2003-05-08 02:44:12 +00001876 delete $4;
1877 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00001878 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001879 const Type *Ty = $2->front().first->getType();
Chris Lattner8ea8f362003-10-30 01:38:18 +00001880 if (!Ty->isFirstClassType())
1881 ThrowException("PHI node operands must be of first class type!");
Chris Lattnerc24d2082001-06-11 15:04:20 +00001882 $$ = new PHINode(Ty);
Chris Lattnerbd2531f2003-10-10 16:34:58 +00001883 $$->op_reserve($2->size()*2);
Chris Lattner00950542001-06-06 20:29:01 +00001884 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001885 if ($2->front().first->getType() != Ty)
1886 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001887 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001888 $2->pop_front();
1889 }
1890 delete $2; // Free the list...
1891 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001892 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001893 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001894 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001895
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001896 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1897 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001898 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001899 std::vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001900 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001901 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1902 I != E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00001903 ParamTypes.push_back((*I)->getType());
1904 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001905
1906 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1907 if (isVarArg) ParamTypes.pop_back();
1908
Chris Lattner79df7c02002-03-26 18:01:55 +00001909 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001910 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001911 }
Chris Lattner00950542001-06-06 20:29:01 +00001912
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001913 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001914
Chris Lattner8b81bf52001-07-25 22:47:46 +00001915 // Create the call node...
1916 if (!$5) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00001917 // Make sure no arguments is a good thing!
1918 if (Ty->getNumParams() != 0)
1919 ThrowException("No arguments passed to a function that "
1920 "expects arguments!");
1921
Chris Lattner9232b992003-04-22 18:42:41 +00001922 $$ = new CallInst(V, std::vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001923 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001924 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00001925 // correctly!
1926 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001927 FunctionType::param_iterator I = Ty->param_begin();
1928 FunctionType::param_iterator E = Ty->param_end();
Chris Lattner9232b992003-04-22 18:42:41 +00001929 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001930
1931 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1932 if ((*ArgI)->getType() != *I)
1933 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001934 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001935
Chris Lattner8b81bf52001-07-25 22:47:46 +00001936 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001937 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001938
Chris Lattner6cdb0112001-11-26 16:54:11 +00001939 $$ = new CallInst(V, *$5);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001940 }
Chris Lattnerf64d57a2003-12-23 20:39:17 +00001941 delete $2;
Chris Lattner8b81bf52001-07-25 22:47:46 +00001942 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001943 }
1944 | MemoryInst {
1945 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001946 };
Chris Lattner00950542001-06-06 20:29:01 +00001947
Chris Lattner6cdb0112001-11-26 16:54:11 +00001948
1949// IndexList - List of indices for GEP based instructions...
1950IndexList : ',' ValueRefList {
Chris Lattner15c9c032003-09-08 18:20:29 +00001951 $$ = $2;
1952 } | /* empty */ {
1953 $$ = new std::vector<Value*>();
1954 };
1955
1956OptVolatile : VOLATILE {
1957 $$ = true;
1958 }
1959 | /* empty */ {
1960 $$ = false;
1961 };
1962
Chris Lattner027dcc52001-07-08 21:10:27 +00001963
Chris Lattner00950542001-06-06 20:29:01 +00001964MemoryInst : MALLOC Types {
Chris Lattner05804b72002-09-13 22:28:45 +00001965 $$ = new MallocInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00001966 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001967 }
1968 | MALLOC Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00001969 $$ = new MallocInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001970 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001971 }
1972 | ALLOCA Types {
Chris Lattner05804b72002-09-13 22:28:45 +00001973 $$ = new AllocaInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00001974 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001975 }
1976 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00001977 $$ = new AllocaInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001978 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001979 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001980 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00001981 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001982 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00001983 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001984 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001985 }
1986
Chris Lattner15c9c032003-09-08 18:20:29 +00001987 | OptVolatile LOAD Types ValueRef {
1988 if (!isa<PointerType>($3->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00001989 ThrowException("Can't load from nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00001990 (*$3)->getDescription());
Chris Lattner79ad13802003-09-08 20:29:46 +00001991 $$ = new LoadInst(getVal(*$3, $4), "", $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00001992 delete $3;
Chris Lattner027dcc52001-07-08 21:10:27 +00001993 }
Chris Lattner15c9c032003-09-08 18:20:29 +00001994 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
1995 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00001996 if (!PT)
Chris Lattner72e00252001-12-14 16:28:42 +00001997 ThrowException("Can't store to a nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00001998 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00001999 const Type *ElTy = PT->getElementType();
Chris Lattner15c9c032003-09-08 18:20:29 +00002000 if (ElTy != $3->getType())
2001 ThrowException("Can't store '" + $3->getType()->getDescription() +
Chris Lattner72e00252001-12-14 16:28:42 +00002002 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner0383cc42002-08-21 23:51:21 +00002003
Chris Lattner79ad13802003-09-08 20:29:46 +00002004 $$ = new StoreInst($3, getVal(*$5, $6), $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002005 delete $5;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002006 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00002007 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00002008 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002009 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner830b6f92004-04-05 01:30:04 +00002010
2011 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
2012 // indices to uint struct indices for compatibility.
2013 generic_gep_type_iterator<std::vector<Value*>::iterator>
2014 GTI = gep_type_begin($2->get(), $4->begin(), $4->end()),
2015 GTE = gep_type_end($2->get(), $4->begin(), $4->end());
2016 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
2017 if (isa<StructType>(*GTI)) // Only change struct indices
2018 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
2019 if (CUI->getType() == Type::UByteTy)
2020 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
2021
Chris Lattner30c89792001-09-07 16:35:17 +00002022 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner830b6f92004-04-05 01:30:04 +00002023 ThrowException("Invalid getelementptr indices for type '" +
2024 (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00002025 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
2026 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00002027 };
Chris Lattner027dcc52001-07-08 21:10:27 +00002028
Brian Gaeked0fde302003-11-11 22:41:34 +00002029
Chris Lattner00950542001-06-06 20:29:01 +00002030%%
Chris Lattner09083092001-07-08 04:57:15 +00002031int yyerror(const char *ErrorMsg) {
Chris Lattner9232b992003-04-22 18:42:41 +00002032 std::string where
2033 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002034 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Chris Lattner9232b992003-04-22 18:42:41 +00002035 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
Chris Lattnerbc280ab2004-03-30 20:58:25 +00002036 if (yychar == YYEMPTY || yychar == 0)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002037 errMsg += "end-of-file.";
2038 else
Chris Lattner9232b992003-04-22 18:42:41 +00002039 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002040 ThrowException(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00002041 return 0;
2042}