blob: 9c13bcf0a67d8b247e067fe09df2cd9fa82b1872 [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 Lattner1781aca2001-09-18 04:00:54 +0000479// setValueName - Set the specified value to the name given. The name may be
480// null potentially, in which case this is a noop. The string passed in is
Chris Lattner8ab406d2004-07-13 07:59:27 +0000481// assumed to be a malloc'd string buffer, and is free'd by this function.
482//
483static void setValueName(Value *V, char *NameStr) {
484 if (NameStr) {
485 std::string Name(NameStr); // Copy string
486 free(NameStr); // Free old string
Chris Lattnerc9aea522004-07-13 08:12:39 +0000487
488 if (V->getType() == Type::VoidTy)
489 ThrowException("Can't assign name '" + Name+"' to value with void type!");
Chris Lattner8ab406d2004-07-13 07:59:27 +0000490
491 assert(inFunctionScope() && "Must be in function scope!");
492 SymbolTable &ST = CurFun.CurrentFunction->getSymbolTable();
493 if (ST.lookup(V->getType(), Name))
494 ThrowException("Redefinition of value named '" + Name + "' in the '" +
495 V->getType()->getDescription() + "' type plane!");
496
Chris Lattnerc9aea522004-07-13 08:12:39 +0000497 // Set the name.
498 V->setName(Name, &ST);
Chris Lattner8ab406d2004-07-13 07:59:27 +0000499 }
500}
501
502// setValueNameMergingDuplicates - Set the specified value to the name given.
503// The name may be null potentially, in which case this is a noop. The string
504// passed in is assumed to be a malloc'd string buffer, and is free'd by this
505// function.
Chris Lattner1781aca2001-09-18 04:00:54 +0000506//
Chris Lattnerb7474512001-10-03 15:39:04 +0000507// This function returns true if the value has already been defined, but is
508// allowed to be redefined in the specified context. If the name is a new name
509// for the typeplane, false is returned.
510//
Chris Lattner8ab406d2004-07-13 07:59:27 +0000511static bool setValueNameMergingDuplicates(Value *V, char *NameStr) {
Chris Lattnerc9aea522004-07-13 08:12:39 +0000512 assert(V->getType() != Type::VoidTy && "Global or constant of type void?");
513
Chris Lattnerb7474512001-10-03 15:39:04 +0000514 if (NameStr == 0) return false;
Chris Lattner8ab406d2004-07-13 07:59:27 +0000515
Chris Lattner9232b992003-04-22 18:42:41 +0000516 std::string Name(NameStr); // Copy string
Chris Lattner1781aca2001-09-18 04:00:54 +0000517 free(NameStr); // Free old string
518
Chris Lattner8ab406d2004-07-13 07:59:27 +0000519 // FIXME: If we eliminated the function constant pool (which we should), this
520 // would just unconditionally look at the module symtab.
Chris Lattner6e6026b2002-11-20 18:36:02 +0000521 SymbolTable &ST = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000522 CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner6e6026b2002-11-20 18:36:02 +0000523 CurModule.CurrentModule->getSymbolTable();
Chris Lattner30c89792001-09-07 16:35:17 +0000524
Reid Spencer75719bf2004-05-26 21:48:31 +0000525 Value *Existing = ST.lookup(V->getType(), Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000526 if (Existing) { // Inserting a name that is already defined???
Chris Lattner8ab406d2004-07-13 07:59:27 +0000527 // We are a simple redefinition of a value, check to see if it is defined
528 // the same as the old one...
529 if (GlobalVariable *EGV = dyn_cast<GlobalVariable>(Existing)) {
Chris Lattner43efcbf2001-10-03 19:35:57 +0000530 // We are allowed to redefine a global variable in two circumstances:
531 // 1. If at least one of the globals is uninitialized or
532 // 2. If both initializers have the same value.
533 //
Chris Lattner89219832001-10-03 19:35:04 +0000534 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000535 if (!EGV->hasInitializer() || !GV->hasInitializer() ||
536 EGV->getInitializer() == GV->getInitializer()) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000537
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000538 // Make sure the existing global version gets the initializer! Make
539 // sure that it also gets marked const if the new version is.
Chris Lattner89219832001-10-03 19:35:04 +0000540 if (GV->hasInitializer() && !EGV->hasInitializer())
541 EGV->setInitializer(GV->getInitializer());
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000542 if (GV->isConstant())
543 EGV->setConstant(true);
Chris Lattner4ad02e72003-04-16 20:28:45 +0000544 EGV->setLinkage(GV->getLinkage());
Chris Lattner89219832001-10-03 19:35:04 +0000545
Chris Lattner2079fde2001-10-13 06:41:08 +0000546 delete GV; // Destroy the duplicate!
Chris Lattner89219832001-10-03 19:35:04 +0000547 return true; // They are equivalent!
548 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000549 }
Chris Lattner8ab406d2004-07-13 07:59:27 +0000550 } else if (const Constant *C = dyn_cast<Constant>(Existing)) {
551 if (C == V) return true; // Constants are equal to themselves
Chris Lattner9636a912001-10-01 16:18:37 +0000552 }
Chris Lattner0b268fb2003-11-25 03:54:16 +0000553
Chris Lattner2079fde2001-10-13 06:41:08 +0000554 ThrowException("Redefinition of value named '" + Name + "' in the '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000555 V->getType()->getDescription() + "' type plane!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000556 }
Chris Lattnerc9aea522004-07-13 08:12:39 +0000557
558 // Set the name.
559 V->setName(Name, &ST);
Chris Lattnerb7474512001-10-03 15:39:04 +0000560 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000561}
562
Chris Lattner8ab406d2004-07-13 07:59:27 +0000563
564
Reid Spencer75719bf2004-05-26 21:48:31 +0000565// setTypeName - Set the specified type to the name given. The name may be
566// null potentially, in which case this is a noop. The string passed in is
567// assumed to be a malloc'd string buffer, and is freed by this function.
568//
569// This function returns true if the type has already been defined, but is
570// allowed to be redefined in the specified context. If the name is a new name
571// for the type plane, it is inserted and false is returned.
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000572static bool setTypeName(const Type *T, char *NameStr) {
Reid Spencer75719bf2004-05-26 21:48:31 +0000573 if (NameStr == 0) return false;
574
575 std::string Name(NameStr); // Copy string
576 free(NameStr); // Free old string
577
578 // We don't allow assigning names to void type
579 if (T == Type::VoidTy)
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000580 ThrowException("Can't assign name '" + Name + "' to the void type!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000581
582 SymbolTable &ST = inFunctionScope() ?
583 CurFun.CurrentFunction->getSymbolTable() :
584 CurModule.CurrentModule->getSymbolTable();
585
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000586 // Inserting a name that is already defined???
587 if (Type *Existing = ST.lookupType(Name)) {
Reid Spencer75719bf2004-05-26 21:48:31 +0000588 // There is only one case where this is allowed: when we are refining an
589 // opaque type. In this case, Existing will be an opaque type.
590 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
591 // We ARE replacing an opaque type!
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000592 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000593 return true;
594 }
595
596 // Otherwise, this is an attempt to redefine a type. That's okay if
597 // the redefinition is identical to the original. This will be so if
598 // Existing and T point to the same Type object. In this one case we
599 // allow the equivalent redefinition.
600 if (Existing == T) return true; // Yes, it's equal.
601
602 // Any other kind of (non-equivalent) redefinition is an error.
603 ThrowException("Redefinition of type named '" + Name + "' in the '" +
604 T->getDescription() + "' type plane!");
605 }
606
607 // Okay, its a newly named type. Set its name.
Chris Lattner8ca2dc02004-07-09 16:43:55 +0000608 if (!Name.empty()) ST.insert(Name, T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000609
Reid Spencer75719bf2004-05-26 21:48:31 +0000610 return false;
611}
Chris Lattner8896eda2001-07-09 19:38:36 +0000612
Chris Lattner30c89792001-09-07 16:35:17 +0000613//===----------------------------------------------------------------------===//
614// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000615//
Chris Lattner8896eda2001-07-09 19:38:36 +0000616
Chris Lattner3b073862004-02-09 03:19:29 +0000617// TypeContains - Returns true if Ty directly contains E in it.
Chris Lattner30c89792001-09-07 16:35:17 +0000618//
619static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3b073862004-02-09 03:19:29 +0000620 return find(Ty->subtype_begin(), Ty->subtype_end(), E) != Ty->subtype_end();
621}
622
623namespace {
624 struct UpRefRecord {
625 // NestingLevel - The number of nesting levels that need to be popped before
626 // this type is resolved.
627 unsigned NestingLevel;
628
629 // LastContainedTy - This is the type at the current binding level for the
630 // type. Every time we reduce the nesting level, this gets updated.
631 const Type *LastContainedTy;
632
633 // UpRefTy - This is the actual opaque type that the upreference is
634 // represented with.
635 OpaqueType *UpRefTy;
636
637 UpRefRecord(unsigned NL, OpaqueType *URTy)
638 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
639 };
Chris Lattner30c89792001-09-07 16:35:17 +0000640}
Chris Lattner698b56e2001-07-20 19:15:08 +0000641
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000642// UpRefs - A list of the outstanding upreferences that need to be resolved.
Chris Lattner3b073862004-02-09 03:19:29 +0000643static std::vector<UpRefRecord> UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000644
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000645/// HandleUpRefs - Every time we finish a new layer of types, this function is
646/// called. It loops through the UpRefs vector, which is a list of the
647/// currently active types. For each type, if the up reference is contained in
648/// the newly completed type, we decrement the level count. When the level
649/// count reaches zero, the upreferenced type is the type that is passed in:
650/// thus we can complete the cycle.
651///
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000652static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner2c37c182004-02-09 03:03:10 +0000653 if (!ty->isAbstract()) return ty;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000654 PATypeHolder Ty(ty);
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000655 UR_OUT("Type '" << Ty->getDescription() <<
Chris Lattner5084d032001-11-02 07:46:26 +0000656 "' newly formed. Resolving upreferences.\n" <<
657 UpRefs.size() << " upreferences active!\n");
Chris Lattner026a8ce2004-02-09 18:53:54 +0000658
659 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
660 // to zero), we resolve them all together before we resolve them to Ty. At
661 // the end of the loop, if there is anything to resolve to Ty, it will be in
662 // this variable.
663 OpaqueType *TypeToResolve = 0;
664
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000665 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Chris Lattner5084d032001-11-02 07:46:26 +0000666 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Chris Lattner30c89792001-09-07 16:35:17 +0000667 << UpRefs[i].second->getDescription() << ") = "
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000668 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner3b073862004-02-09 03:19:29 +0000669 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
670 // Decrement level of upreference
671 unsigned Level = --UpRefs[i].NestingLevel;
672 UpRefs[i].LastContainedTy = Ty;
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000673 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Chris Lattner30c89792001-09-07 16:35:17 +0000674 if (Level == 0) { // Upreference should be resolved!
Chris Lattner026a8ce2004-02-09 18:53:54 +0000675 if (!TypeToResolve) {
676 TypeToResolve = UpRefs[i].UpRefTy;
677 } else {
678 UR_OUT(" * Resolving upreference for "
679 << UpRefs[i].second->getDescription() << "\n";
680 std::string OldName = UpRefs[i].UpRefTy->getDescription());
681 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
682 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
683 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
684 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000685 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
686 --i; // Do not skip the next element...
Chris Lattner30c89792001-09-07 16:35:17 +0000687 }
688 }
Chris Lattner8896eda2001-07-09 19:38:36 +0000689 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000690
Chris Lattner026a8ce2004-02-09 18:53:54 +0000691 if (TypeToResolve) {
692 UR_OUT(" * Resolving upreference for "
693 << UpRefs[i].second->getDescription() << "\n";
Chris Lattner16af11d2004-02-09 21:03:38 +0000694 std::string OldName = TypeToResolve->getDescription());
Chris Lattner026a8ce2004-02-09 18:53:54 +0000695 TypeToResolve->refineAbstractTypeTo(Ty);
696 }
697
Chris Lattner8896eda2001-07-09 19:38:36 +0000698 return Ty;
699}
700
Chris Lattner30c89792001-09-07 16:35:17 +0000701
Chris Lattner00950542001-06-06 20:29:01 +0000702//===----------------------------------------------------------------------===//
703// RunVMAsmParser - Define an interface to this parser
704//===----------------------------------------------------------------------===//
705//
Chris Lattner9232b992003-04-22 18:42:41 +0000706Module *RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000707 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000708 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000709 llvmAsmlineno = 1; // Reset the current line number...
Chris Lattner99e7ab72003-10-18 05:53:13 +0000710 ObsoleteVarArgs = false;
Chris Lattner00950542001-06-06 20:29:01 +0000711
Chris Lattner75f20532003-04-22 18:02:52 +0000712 // Allocate a new module to read
713 CurModule.CurrentModule = new Module(Filename);
Chris Lattner42570982003-11-26 07:24:58 +0000714
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000715 yyparse(); // Parse the file, potentially throwing exception
Chris Lattner99e7ab72003-10-18 05:53:13 +0000716
Chris Lattner00950542001-06-06 20:29:01 +0000717 Module *Result = ParserResult;
Chris Lattner99e7ab72003-10-18 05:53:13 +0000718
719 // Check to see if they called va_start but not va_arg..
720 if (!ObsoleteVarArgs)
721 if (Function *F = Result->getNamedFunction("llvm.va_start"))
722 if (F->asize() == 1) {
723 std::cerr << "WARNING: this file uses obsolete features. "
724 << "Assemble and disassemble to update it.\n";
725 ObsoleteVarArgs = true;
726 }
727
Chris Lattner99e7ab72003-10-18 05:53:13 +0000728 if (ObsoleteVarArgs) {
729 // If the user is making use of obsolete varargs intrinsics, adjust them for
730 // the user.
731 if (Function *F = Result->getNamedFunction("llvm.va_start")) {
732 assert(F->asize() == 1 && "Obsolete va_start takes 1 argument!");
733
734 const Type *RetTy = F->getFunctionType()->getParamType(0);
735 RetTy = cast<PointerType>(RetTy)->getElementType();
736 Function *NF = Result->getOrInsertFunction("llvm.va_start", RetTy, 0);
737
738 while (!F->use_empty()) {
739 CallInst *CI = cast<CallInst>(F->use_back());
740 Value *V = new CallInst(NF, "", CI);
741 new StoreInst(V, CI->getOperand(1), CI);
742 CI->getParent()->getInstList().erase(CI);
743 }
744 Result->getFunctionList().erase(F);
745 }
746
747 if (Function *F = Result->getNamedFunction("llvm.va_end")) {
748 assert(F->asize() == 1 && "Obsolete va_end takes 1 argument!");
749 const Type *ArgTy = F->getFunctionType()->getParamType(0);
750 ArgTy = cast<PointerType>(ArgTy)->getElementType();
751 Function *NF = Result->getOrInsertFunction("llvm.va_end", Type::VoidTy,
752 ArgTy, 0);
753
754 while (!F->use_empty()) {
755 CallInst *CI = cast<CallInst>(F->use_back());
756 Value *V = new LoadInst(CI->getOperand(1), "", CI);
757 new CallInst(NF, V, "", CI);
758 CI->getParent()->getInstList().erase(CI);
759 }
760 Result->getFunctionList().erase(F);
761 }
762
763 if (Function *F = Result->getNamedFunction("llvm.va_copy")) {
764 assert(F->asize() == 2 && "Obsolete va_copy takes 2 argument!");
765 const Type *ArgTy = F->getFunctionType()->getParamType(0);
766 ArgTy = cast<PointerType>(ArgTy)->getElementType();
767 Function *NF = Result->getOrInsertFunction("llvm.va_copy", ArgTy,
768 ArgTy, 0);
769
770 while (!F->use_empty()) {
771 CallInst *CI = cast<CallInst>(F->use_back());
772 Value *V = new CallInst(NF, CI->getOperand(2), "", CI);
773 new StoreInst(V, CI->getOperand(1), CI);
774 CI->getParent()->getInstList().erase(CI);
775 }
776 Result->getFunctionList().erase(F);
777 }
778 }
779
Chris Lattner00950542001-06-06 20:29:01 +0000780 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
781 ParserResult = 0;
782
783 return Result;
784}
785
Brian Gaeked0fde302003-11-11 22:41:34 +0000786} // End llvm namespace
787
788using namespace llvm;
789
Chris Lattner00950542001-06-06 20:29:01 +0000790%}
791
792%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000793 llvm::Module *ModuleVal;
794 llvm::Function *FunctionVal;
795 std::pair<llvm::PATypeHolder*, char*> *ArgVal;
796 llvm::BasicBlock *BasicBlockVal;
797 llvm::TerminatorInst *TermInstVal;
798 llvm::Instruction *InstVal;
799 llvm::Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000800
Brian Gaeked0fde302003-11-11 22:41:34 +0000801 const llvm::Type *PrimType;
802 llvm::PATypeHolder *TypeVal;
803 llvm::Value *ValueVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000804
Brian Gaeked0fde302003-11-11 22:41:34 +0000805 std::vector<std::pair<llvm::PATypeHolder*,char*> > *ArgList;
806 std::vector<llvm::Value*> *ValueList;
807 std::list<llvm::PATypeHolder> *TypeList;
808 std::list<std::pair<llvm::Value*,
809 llvm::BasicBlock*> > *PHIList; // Represent the RHS of PHI node
810 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
811 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000812
Brian Gaeked0fde302003-11-11 22:41:34 +0000813 llvm::GlobalValue::LinkageTypes Linkage;
Chris Lattner30c89792001-09-07 16:35:17 +0000814 int64_t SInt64Val;
815 uint64_t UInt64Val;
816 int SIntVal;
817 unsigned UIntVal;
818 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000819 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000820
Chris Lattner30c89792001-09-07 16:35:17 +0000821 char *StrVal; // This memory is strdup'd!
Brian Gaeked0fde302003-11-11 22:41:34 +0000822 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000823
Brian Gaeked0fde302003-11-11 22:41:34 +0000824 llvm::Instruction::BinaryOps BinaryOpVal;
825 llvm::Instruction::TermOps TermOpVal;
826 llvm::Instruction::MemoryOps MemOpVal;
827 llvm::Instruction::OtherOps OtherOpVal;
828 llvm::Module::Endianness Endianness;
Chris Lattner00950542001-06-06 20:29:01 +0000829}
830
Chris Lattner79df7c02002-03-26 18:01:55 +0000831%type <ModuleVal> Module FunctionList
832%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000833%type <BasicBlockVal> BasicBlock InstructionList
834%type <TermInstVal> BBTerminatorInst
835%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000836%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000837%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000838%type <ArgList> ArgList ArgListH
839%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000840%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000841%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000842%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000843%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000844%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +0000845%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner15c9c032003-09-08 18:20:29 +0000846%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattner4ad02e72003-04-16 20:28:45 +0000847%type <Linkage> OptLinkage
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000848%type <Endianness> BigOrLittle
Chris Lattner00950542001-06-06 20:29:01 +0000849
Chris Lattner2079fde2001-10-13 06:41:08 +0000850// ValueRef - Unresolved reference to a definition or BB
851%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000852%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000853// Tokens and types for handling constant integer values
854//
855// ESINT64VAL - A negative number within long long range
856%token <SInt64Val> ESINT64VAL
857
858// EUINT64VAL - A positive number within uns. long long range
859%token <UInt64Val> EUINT64VAL
860%type <SInt64Val> EINT64VAL
861
862%token <SIntVal> SINTVAL // Signed 32 bit ints...
863%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
864%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000865%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000866
867// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000868%type <TypeVal> Types TypesV UpRTypes UpRTypesV
869%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000870%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
871%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000872
Chris Lattner6c23f572003-08-22 05:42:10 +0000873%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
874%type <StrVal> Name OptName OptAssign
Chris Lattner00950542001-06-06 20:29:01 +0000875
876
Chris Lattner91ef4602004-03-31 03:49:47 +0000877%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Chris Lattner15c9c032003-09-08 18:20:29 +0000878%token DECLARE GLOBAL CONSTANT VOLATILE
Chris Lattnera58e3a12004-02-08 21:48:25 +0000879%token TO DOTDOTDOT NULL_TOK CONST INTERNAL LINKONCE WEAK APPENDING
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000880%token OPAQUE NOT EXTERNAL TARGET ENDIAN POINTERSIZE LITTLE BIG
Chris Lattner00950542001-06-06 20:29:01 +0000881
882// Basic Block Terminating Operators
Chris Lattner36143fc2003-09-08 18:54:55 +0000883%token <TermOpVal> RET BR SWITCH INVOKE UNWIND
Chris Lattner00950542001-06-06 20:29:01 +0000884
Chris Lattner00950542001-06-06 20:29:01 +0000885// Binary Operators
886%type <BinaryOpVal> BinaryOps // all the binary operators
Chris Lattner4a6482b2002-09-10 19:57:26 +0000887%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattner42c9e772001-10-20 09:32:59 +0000888%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000889%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000890
891// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000892%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000893
Chris Lattner027dcc52001-07-08 21:10:27 +0000894// Other Operators
895%type <OtherOpVal> ShiftOps
Chris Lattner76f0ff32004-03-12 05:51:36 +0000896%token <OtherOpVal> PHI_TOK CALL CAST SELECT SHL SHR VAARG VANEXT
Chris Lattner99e7ab72003-10-18 05:53:13 +0000897%token VA_ARG // FIXME: OBSOLETE
Chris Lattner027dcc52001-07-08 21:10:27 +0000898
Chris Lattner00950542001-06-06 20:29:01 +0000899%start Module
900%%
901
902// Handle constant integer size restriction and conversion...
903//
Chris Lattner51727be2002-06-04 21:58:56 +0000904INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +0000905INTVAL : UINTVAL {
906 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
907 ThrowException("Value too large for type!");
908 $$ = (int32_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000909};
Chris Lattner00950542001-06-06 20:29:01 +0000910
911
Chris Lattner51727be2002-06-04 21:58:56 +0000912EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +0000913EINT64VAL : EUINT64VAL {
914 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
915 ThrowException("Value too large for type!");
916 $$ = (int64_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000917};
Chris Lattner00950542001-06-06 20:29:01 +0000918
Chris Lattner00950542001-06-06 20:29:01 +0000919// Operations that are notably excluded from this list include:
920// RET, BR, & SWITCH because they end basic blocks and are treated specially.
921//
Chris Lattner4a6482b2002-09-10 19:57:26 +0000922ArithmeticOps: ADD | SUB | MUL | DIV | REM;
923LogicalOps : AND | OR | XOR;
924SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
925BinaryOps : ArithmeticOps | LogicalOps | SetCondOps;
926
Chris Lattner51727be2002-06-04 21:58:56 +0000927ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +0000928
Chris Lattnere98dda62001-07-14 06:10:16 +0000929// These are some types that allow classification if we only want a particular
930// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +0000931SIntType : LONG | INT | SHORT | SBYTE;
932UIntType : ULONG | UINT | USHORT | UBYTE;
933IntType : SIntType | UIntType;
934FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +0000935
Chris Lattnere98dda62001-07-14 06:10:16 +0000936// OptAssign - Value producing statements have an optional assignment component
Chris Lattner6c23f572003-08-22 05:42:10 +0000937OptAssign : Name '=' {
Chris Lattner00950542001-06-06 20:29:01 +0000938 $$ = $1;
939 }
940 | /*empty*/ {
941 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +0000942 };
Chris Lattner00950542001-06-06 20:29:01 +0000943
Chris Lattner4ad02e72003-04-16 20:28:45 +0000944OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
945 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
Chris Lattner72ac148d2003-10-16 18:29:00 +0000946 WEAK { $$ = GlobalValue::WeakLinkage; } |
Chris Lattner4ad02e72003-04-16 20:28:45 +0000947 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
948 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattner30c89792001-09-07 16:35:17 +0000949
950//===----------------------------------------------------------------------===//
951// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +0000952// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +0000953// access to it, a user must explicitly use TypesV.
954//
955
956// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +0000957TypesV : Types | VOID { $$ = new PATypeHolder($1); };
958UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +0000959
960Types : UpRTypes {
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000961 if (!UpRefs.empty())
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000962 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
963 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000964 };
Chris Lattner30c89792001-09-07 16:35:17 +0000965
966
967// Derived types are added later...
968//
Chris Lattner51727be2002-06-04 21:58:56 +0000969PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
970PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000971UpRTypes : OPAQUE {
972 $$ = new PATypeHolder(OpaqueType::get());
973 }
974 | PrimType {
975 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +0000976 };
Chris Lattnerd78700d2002-08-16 21:14:40 +0000977UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000978 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +0000979};
Chris Lattner30c89792001-09-07 16:35:17 +0000980
Chris Lattner30c89792001-09-07 16:35:17 +0000981// Include derived types in the Types production.
982//
983UpRTypes : '\\' EUINT64VAL { // Type UpReference
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000984 if ($2 > (uint64_t)~0U) ThrowException("Value out of range!");
Chris Lattner30c89792001-09-07 16:35:17 +0000985 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3b073862004-02-09 03:19:29 +0000986 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000987 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +0000988 UR_OUT("New Upreference!\n");
989 }
Chris Lattner79df7c02002-03-26 18:01:55 +0000990 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner9232b992003-04-22 18:42:41 +0000991 std::vector<const Type*> Params;
Chris Lattner697954c2002-01-20 22:54:45 +0000992 mapto($3->begin(), $3->end(), std::back_inserter(Params),
Chris Lattner876dc572003-10-02 19:00:34 +0000993 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner2079fde2001-10-13 06:41:08 +0000994 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
995 if (isVarArg) Params.pop_back();
996
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000997 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +0000998 delete $3; // Delete the argument list
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000999 delete $1; // Delete the return type handle
Chris Lattner30c89792001-09-07 16:35:17 +00001000 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001001 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001002 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001003 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001004 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001005 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001006 std::vector<const Type*> Elements;
Chris Lattner697954c2002-01-20 22:54:45 +00001007 mapto($2->begin(), $2->end(), std::back_inserter(Elements),
Chris Lattner876dc572003-10-02 19:00:34 +00001008 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner30c89792001-09-07 16:35:17 +00001009
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001010 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001011 delete $2;
1012 }
1013 | '{' '}' { // Empty structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001014 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001015 }
1016 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001017 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001018 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001019 };
Chris Lattner30c89792001-09-07 16:35:17 +00001020
Chris Lattner7e708292002-06-25 16:13:24 +00001021// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +00001022// declaration type lists
1023//
1024TypeListI : UpRTypes {
Chris Lattner9232b992003-04-22 18:42:41 +00001025 $$ = new std::list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +00001026 $$->push_back(*$1); delete $1;
1027 }
1028 | TypeListI ',' UpRTypes {
1029 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +00001030 };
Chris Lattner30c89792001-09-07 16:35:17 +00001031
Chris Lattner7e708292002-06-25 16:13:24 +00001032// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +00001033ArgTypeListI : TypeListI
1034 | TypeListI ',' DOTDOTDOT {
1035 ($$=$1)->push_back(Type::VoidTy);
1036 }
1037 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001038 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +00001039 }
1040 | /*empty*/ {
Chris Lattner9232b992003-04-22 18:42:41 +00001041 $$ = new std::list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +00001042 };
Chris Lattner30c89792001-09-07 16:35:17 +00001043
Chris Lattnere98dda62001-07-14 06:10:16 +00001044// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +00001045// production is used ONLY to represent constants that show up AFTER a 'const',
1046// 'constant' or 'global' token at global scope. Constants that can be inlined
1047// into other expressions (such as integers and constexprs) are handled by the
1048// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +00001049//
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001050ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Chris Lattner949a3622003-07-23 15:30:06 +00001051 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001052 if (ATy == 0)
1053 ThrowException("Cannot make array constant with type: '" +
1054 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001055 const Type *ETy = ATy->getElementType();
1056 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +00001057
Chris Lattner30c89792001-09-07 16:35:17 +00001058 // Verify that we have the correct size...
1059 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +00001060 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +00001061 utostr($3->size()) + " arguments, but has size of " +
1062 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +00001063
Chris Lattner30c89792001-09-07 16:35:17 +00001064 // Verify all elements are correct type!
1065 for (unsigned i = 0; i < $3->size(); i++) {
1066 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +00001067 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001068 ETy->getDescription() +"' as required!\nIt is of type '"+
1069 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +00001070 }
1071
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001072 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001073 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001074 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001075 | Types '[' ']' {
Chris Lattner949a3622003-07-23 15:30:06 +00001076 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001077 if (ATy == 0)
1078 ThrowException("Cannot make array constant with type: '" +
1079 (*$1)->getDescription() + "'!");
1080
1081 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +00001082 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +00001083 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +00001084 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattner9232b992003-04-22 18:42:41 +00001085 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +00001086 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001087 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001088 | Types 'c' STRINGCONSTANT {
Chris Lattner949a3622003-07-23 15:30:06 +00001089 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001090 if (ATy == 0)
1091 ThrowException("Cannot make array constant with type: '" +
1092 (*$1)->getDescription() + "'!");
1093
Chris Lattner30c89792001-09-07 16:35:17 +00001094 int NumElements = ATy->getNumElements();
1095 const Type *ETy = ATy->getElementType();
1096 char *EndStr = UnEscapeLexed($3, true);
1097 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +00001098 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +00001099 itostr((int)(EndStr-$3)) +
1100 " when array has size " + itostr(NumElements) + "!");
Chris Lattner9232b992003-04-22 18:42:41 +00001101 std::vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +00001102 if (ETy == Type::SByteTy) {
1103 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001104 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +00001105 } else if (ETy == Type::UByteTy) {
1106 for (char *C = $3; C != EndStr; ++C)
Chris Lattnerbae362f2003-01-30 22:24:26 +00001107 Vals.push_back(ConstantUInt::get(ETy, (unsigned char)*C));
Chris Lattner93750fa2001-07-28 17:48:55 +00001108 } else {
Chris Lattner30c89792001-09-07 16:35:17 +00001109 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +00001110 ThrowException("Cannot build string arrays of non byte sized elements!");
1111 }
Chris Lattner30c89792001-09-07 16:35:17 +00001112 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001113 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +00001114 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001115 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001116 | Types '{' ConstVector '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001117 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001118 if (STy == 0)
1119 ThrowException("Cannot make struct constant with type: '" +
1120 (*$1)->getDescription() + "'!");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001121
Chris Lattnerc6212f12003-05-21 16:06:56 +00001122 if ($3->size() != STy->getNumContainedTypes())
1123 ThrowException("Illegal number of initializers for structure type!");
1124
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001125 // Check to ensure that constants are compatible with the type initializer!
1126 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Chris Lattnerd21cd802004-02-09 04:37:31 +00001127 if ((*$3)[i]->getType() != STy->getElementType(i))
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001128 ThrowException("Expected type '" +
Chris Lattnerd21cd802004-02-09 04:37:31 +00001129 STy->getElementType(i)->getDescription() +
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001130 "' for element #" + utostr(i) +
1131 " of structure initializer!");
1132
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001133 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001134 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001135 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001136 | Types '{' '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001137 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001138 if (STy == 0)
1139 ThrowException("Cannot make struct constant with type: '" +
1140 (*$1)->getDescription() + "'!");
1141
1142 if (STy->getNumContainedTypes() != 0)
1143 ThrowException("Illegal number of initializers for structure type!");
1144
1145 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1146 delete $1;
1147 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001148 | Types NULL_TOK {
Chris Lattner949a3622003-07-23 15:30:06 +00001149 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001150 if (PTy == 0)
1151 ThrowException("Cannot make null pointer constant with type: '" +
1152 (*$1)->getDescription() + "'!");
1153
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001154 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001155 delete $1;
1156 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001157 | Types SymbolicValueRef {
Chris Lattner949a3622003-07-23 15:30:06 +00001158 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001159 if (Ty == 0)
1160 ThrowException("Global const reference must be a pointer type!");
1161
Chris Lattner3101c252002-08-15 17:58:33 +00001162 // ConstExprs can exist in the body of a function, thus creating
1163 // ConstantPointerRefs whenever they refer to a variable. Because we are in
1164 // the context of a function, getValNonImprovising will search the functions
1165 // symbol table instead of the module symbol table for the global symbol,
1166 // which throws things all off. To get around this, we just tell
1167 // getValNonImprovising that we are at global scope here.
1168 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001169 Function *SavedCurFn = CurFun.CurrentFunction;
1170 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001171
Chris Lattner2079fde2001-10-13 06:41:08 +00001172 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001173
Chris Lattner394f2eb2003-10-16 20:12:13 +00001174 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001175
Chris Lattner2079fde2001-10-13 06:41:08 +00001176 // If this is an initializer for a constant pointer, which is referencing a
1177 // (currently) undefined variable, create a stub now that shall be replaced
1178 // in the future with the right type of variable.
1179 //
1180 if (V == 0) {
1181 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1182 const PointerType *PT = cast<PointerType>(Ty);
1183
1184 // First check to see if the forward references value is already created!
1185 PerModuleInfo::GlobalRefsType::iterator I =
Chris Lattner9232b992003-04-22 18:42:41 +00001186 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001187
1188 if (I != CurModule.GlobalRefs.end()) {
1189 V = I->second; // Placeholder already exists, use it...
Chris Lattnera989b232003-12-23 20:05:15 +00001190 $2.destroy();
Chris Lattner2079fde2001-10-13 06:41:08 +00001191 } else {
Chris Lattner2079fde2001-10-13 06:41:08 +00001192 // Create a placeholder for the global variable reference...
Chris Lattner7a176752001-12-04 00:03:30 +00001193 GlobalVariable *GV = new GlobalVariable(PT->getElementType(),
Chris Lattner4ad02e72003-04-16 20:28:45 +00001194 false,
1195 GlobalValue::ExternalLinkage);
Chris Lattner2079fde2001-10-13 06:41:08 +00001196 // Keep track of the fact that we have a forward ref to recycle it
Chris Lattner9232b992003-04-22 18:42:41 +00001197 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
Chris Lattner2079fde2001-10-13 06:41:08 +00001198
1199 // Must temporarily push this value into the module table...
1200 CurModule.CurrentModule->getGlobalList().push_back(GV);
1201 V = GV;
1202 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001203 }
1204
Chris Lattner2079fde2001-10-13 06:41:08 +00001205 GlobalValue *GV = cast<GlobalValue>(V);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001206 $$ = ConstantPointerRef::get(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001207 delete $1; // Free the type handle
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001208 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001209 | Types ConstExpr {
1210 if ($1->get() != $2->getType())
1211 ThrowException("Mismatched types for constant expression!");
1212 $$ = $2;
1213 delete $1;
Chris Lattnerf4600482003-06-28 20:01:34 +00001214 }
1215 | Types ZEROINITIALIZER {
1216 $$ = Constant::getNullValue($1->get());
1217 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001218 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001219
Chris Lattnere43f40b2002-10-09 00:25:32 +00001220ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001221 if (!ConstantSInt::isValueValidForType($1, $2))
1222 ThrowException("Constant value doesn't fit in type!");
1223 $$ = ConstantSInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001224 }
1225 | UIntType EUINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001226 if (!ConstantUInt::isValueValidForType($1, $2))
1227 ThrowException("Constant value doesn't fit in type!");
1228 $$ = ConstantUInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001229 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001230 | BOOL TRUETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001231 $$ = ConstantBool::True;
1232 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001233 | BOOL FALSETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001234 $$ = ConstantBool::False;
1235 }
1236 | FPType FPVAL { // Float & Double constants
1237 $$ = ConstantFP::get($1, $2);
1238 };
1239
Chris Lattner00950542001-06-06 20:29:01 +00001240
Chris Lattnerd78700d2002-08-16 21:14:40 +00001241ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattnerae711a82003-11-21 20:27:35 +00001242 if (!$3->getType()->isFirstClassType())
1243 ThrowException("cast constant expression from a non-primitive type: '" +
1244 $3->getType()->getDescription() + "'!");
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001245 if (!$5->get()->isFirstClassType())
1246 ThrowException("cast constant expression to a non-primitive type: '" +
1247 $5->get()->getDescription() + "'!");
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001248 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001249 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001250 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001251 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1252 if (!isa<PointerType>($3->getType()))
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001253 ThrowException("GetElementPtr requires a pointer operand!");
1254
Chris Lattner830b6f92004-04-05 01:30:04 +00001255 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
1256 // indices to uint struct indices for compatibility.
1257 generic_gep_type_iterator<std::vector<Value*>::iterator>
1258 GTI = gep_type_begin($3->getType(), $4->begin(), $4->end()),
1259 GTE = gep_type_end($3->getType(), $4->begin(), $4->end());
1260 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
1261 if (isa<StructType>(*GTI)) // Only change struct indices
1262 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
1263 if (CUI->getType() == Type::UByteTy)
1264 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
1265
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001266 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001267 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001268 if (!IdxTy)
1269 ThrowException("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001270
Chris Lattner9232b992003-04-22 18:42:41 +00001271 std::vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001272 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1273 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001274 IdxVec.push_back(C);
1275 else
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001276 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001277
Chris Lattnerd78700d2002-08-16 21:14:40 +00001278 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001279
Chris Lattnerd78700d2002-08-16 21:14:40 +00001280 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001281 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001282 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1283 if ($3->getType() != Type::BoolTy)
1284 ThrowException("Select condition must be of boolean type!");
1285 if ($5->getType() != $7->getType())
1286 ThrowException("Select operand types must match!");
1287 $$ = ConstantExpr::getSelect($3, $5, $7);
1288 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001289 | BinaryOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001290 if ($3->getType() != $5->getType())
1291 ThrowException("Binary operator types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001292 $$ = ConstantExpr::get($1, $3, $5);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001293 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001294 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001295 if ($5->getType() != Type::UByteTy)
1296 ThrowException("Shift count for shift constant must be unsigned byte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001297 if (!$3->getType()->isInteger())
1298 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerb16689b2004-01-12 19:08:43 +00001299 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner699f1eb2002-08-14 17:12:33 +00001300 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001301
1302
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001303// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001304ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001305 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001306 }
1307 | ConstVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001308 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001309 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001310 };
Chris Lattner00950542001-06-06 20:29:01 +00001311
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001312
Chris Lattner1781aca2001-09-18 04:00:54 +00001313// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001314GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001315
Chris Lattner00950542001-06-06 20:29:01 +00001316
Chris Lattner0e73ce62002-05-02 19:11:13 +00001317//===----------------------------------------------------------------------===//
1318// Rules to match Modules
1319//===----------------------------------------------------------------------===//
1320
1321// Module rule: Capture the result of parsing the whole file into a result
1322// variable...
1323//
1324Module : FunctionList {
1325 $$ = ParserResult = $1;
1326 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001327};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001328
Chris Lattner7e708292002-06-25 16:13:24 +00001329// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001330//
1331FunctionList : FunctionList Function {
1332 $$ = $1;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001333 CurFun.FunctionDone();
Chris Lattner0e73ce62002-05-02 19:11:13 +00001334 }
1335 | FunctionList FunctionProto {
1336 $$ = $1;
1337 }
1338 | FunctionList IMPLEMENTATION {
1339 $$ = $1;
1340 }
1341 | ConstPool {
1342 $$ = CurModule.CurrentModule;
1343 // Resolve circular types before we parse the body of the module
1344 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001345 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001346
Chris Lattnere98dda62001-07-14 06:10:16 +00001347// ConstPool - Constants with optional names assigned to them.
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001348ConstPool : ConstPool OptAssign CONST ConstVal {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001349 // FIXME: THIS SHOULD REALLY BE ELIMINATED. It is totally unneeded.
1350 if (!setValueNameMergingDuplicates($4, $2))
Chris Lattneradf99702003-03-03 23:28:55 +00001351 InsertValue($4);
Chris Lattner00950542001-06-06 20:29:01 +00001352 }
Chris Lattner30c89792001-09-07 16:35:17 +00001353 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner4a42e902001-10-22 05:56:09 +00001354 // Eagerly resolve types. This is not an optimization, this is a
1355 // requirement that is due to the fact that we could have this:
1356 //
1357 // %list = type { %list * }
1358 // %list = type { %list * } ; repeated type decl
1359 //
1360 // If types are not resolved eagerly, then the two types will not be
1361 // determined to be the same type!
1362 //
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001363 ResolveTypeTo($2, *$4);
Chris Lattner4a42e902001-10-22 05:56:09 +00001364
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001365 if (!setTypeName(*$4, $2) && !$2) {
1366 // If this is a named type that is not a redefinition, add it to the slot
1367 // table.
1368 InsertType(*$4, inFunctionScope() ? CurFun.Types : CurModule.Types);
Chris Lattner30c89792001-09-07 16:35:17 +00001369 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001370
1371 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001372 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001373 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001374 }
Chris Lattner4ad02e72003-04-16 20:28:45 +00001375 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Chris Lattnerdda71962001-11-26 18:54:16 +00001376 const Type *Ty = $5->getType();
Chris Lattner1781aca2001-09-18 04:00:54 +00001377 // Global declarations appear in Constant Pool
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001378 Constant *Initializer = $5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001379 if (Initializer == 0)
1380 ThrowException("Global value initializer is not a constant!");
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001381
Chris Lattnerdda71962001-11-26 18:54:16 +00001382 GlobalVariable *GV = new GlobalVariable(Ty, $4, $3, Initializer);
Chris Lattner8ab406d2004-07-13 07:59:27 +00001383 if (!setValueNameMergingDuplicates(GV, $2)) { // If not redefining...
Chris Lattnerb7474512001-10-03 15:39:04 +00001384 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001385 int Slot = InsertValue(GV, CurModule.Values);
1386
1387 if (Slot != -1) {
1388 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1389 } else {
1390 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1391 (char*)GV->getName().c_str()));
1392 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001393 }
Chris Lattner1781aca2001-09-18 04:00:54 +00001394 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001395 | ConstPool OptAssign EXTERNAL GlobalType Types {
1396 const Type *Ty = *$5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001397 // Global declarations appear in Constant Pool
Chris Lattner4ad02e72003-04-16 20:28:45 +00001398 GlobalVariable *GV = new GlobalVariable(Ty,$4,GlobalValue::ExternalLinkage);
Chris Lattner8ab406d2004-07-13 07:59:27 +00001399 if (!setValueNameMergingDuplicates(GV, $2)) { // If not redefining...
Chris Lattnerb7474512001-10-03 15:39:04 +00001400 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001401 int Slot = InsertValue(GV, CurModule.Values);
1402
1403 if (Slot != -1) {
1404 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1405 } else {
1406 assert(GV->hasName() && "Not named and not numbered!?");
1407 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1408 (char*)GV->getName().c_str()));
1409 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001410 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001411 delete $5;
Chris Lattnere98dda62001-07-14 06:10:16 +00001412 }
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001413 | ConstPool TARGET TargetDefinition {
1414 }
Chris Lattner00950542001-06-06 20:29:01 +00001415 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001416 };
Chris Lattner00950542001-06-06 20:29:01 +00001417
1418
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001419
1420BigOrLittle : BIG { $$ = Module::BigEndian; };
1421BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1422
1423TargetDefinition : ENDIAN '=' BigOrLittle {
1424 CurModule.CurrentModule->setEndianness($3);
1425 }
1426 | POINTERSIZE '=' EUINT64VAL {
1427 if ($3 == 32)
1428 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1429 else if ($3 == 64)
1430 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1431 else
1432 ThrowException("Invalid pointer size: '" + utostr($3) + "'!");
1433 };
1434
1435
Chris Lattner00950542001-06-06 20:29:01 +00001436//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001437// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001438//===----------------------------------------------------------------------===//
1439
Chris Lattner6c23f572003-08-22 05:42:10 +00001440Name : VAR_ID | STRINGCONSTANT;
1441OptName : Name | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001442
Chris Lattner6c23f572003-08-22 05:42:10 +00001443ArgVal : Types OptName {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001444 if (*$1 == Type::VoidTy)
1445 ThrowException("void typed arguments are invalid!");
Chris Lattner9232b992003-04-22 18:42:41 +00001446 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
Chris Lattner51727be2002-06-04 21:58:56 +00001447};
Chris Lattner00950542001-06-06 20:29:01 +00001448
Chris Lattner69da5cf2002-10-13 20:57:00 +00001449ArgListH : ArgListH ',' ArgVal {
1450 $$ = $1;
1451 $1->push_back(*$3);
1452 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001453 }
1454 | ArgVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001455 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
Chris Lattner69da5cf2002-10-13 20:57:00 +00001456 $$->push_back(*$1);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001457 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001458 };
Chris Lattner00950542001-06-06 20:29:01 +00001459
1460ArgList : ArgListH {
1461 $$ = $1;
1462 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001463 | ArgListH ',' DOTDOTDOT {
1464 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001465 $$->push_back(std::pair<PATypeHolder*,
1466 char*>(new PATypeHolder(Type::VoidTy), 0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001467 }
1468 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001469 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1470 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001471 }
Chris Lattner00950542001-06-06 20:29:01 +00001472 | /* empty */ {
1473 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001474 };
Chris Lattner00950542001-06-06 20:29:01 +00001475
Chris Lattner6c23f572003-08-22 05:42:10 +00001476FunctionHeaderH : TypesV Name '(' ArgList ')' {
Chris Lattner1f862af2003-04-16 18:13:57 +00001477 UnEscapeLexed($2);
Chris Lattner9232b992003-04-22 18:42:41 +00001478 std::string FunctionName($2);
Chris Lattnerdda71962001-11-26 18:54:16 +00001479
Chris Lattnerc282f5a2003-11-21 22:32:23 +00001480 if (!(*$1)->isFirstClassType() && *$1 != Type::VoidTy)
1481 ThrowException("LLVM functions cannot return aggregate types!");
1482
Chris Lattner9232b992003-04-22 18:42:41 +00001483 std::vector<const Type*> ParamTypeList;
Chris Lattner1f862af2003-04-16 18:13:57 +00001484 if ($4) { // If there are arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001485 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001486 I != $4->end(); ++I)
Chris Lattner69da5cf2002-10-13 20:57:00 +00001487 ParamTypeList.push_back(I->first->get());
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001488 }
Chris Lattner00950542001-06-06 20:29:01 +00001489
Chris Lattner2079fde2001-10-13 06:41:08 +00001490 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1491 if (isVarArg) ParamTypeList.pop_back();
1492
Chris Lattner1f862af2003-04-16 18:13:57 +00001493 const FunctionType *FT = FunctionType::get(*$1, ParamTypeList, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001494 const PointerType *PFT = PointerType::get(FT);
Chris Lattner1f862af2003-04-16 18:13:57 +00001495 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001496
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001497 Function *Fn = 0;
1498 // Is the function already in symtab?
1499 if ((Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1500 // Yes it is. If this is the case, either we need to be a forward decl,
1501 // or it needs to be.
Chris Lattner394f2eb2003-10-16 20:12:13 +00001502 if (!CurFun.isDeclare && !Fn->isExternal())
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001503 ThrowException("Redefinition of function '" + FunctionName + "'!");
1504
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001505 // Make sure to strip off any argument names so we can't get conflicts...
1506 for (Function::aiterator AI = Fn->abegin(), AE = Fn->aend(); AI != AE; ++AI)
1507 AI->setName("");
Chris Lattner5659dd12002-07-15 00:10:33 +00001508
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001509 } else { // Not already defined?
Chris Lattner63a22502004-03-08 16:14:19 +00001510 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
1511 CurModule.CurrentModule);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001512 InsertValue(Fn, CurModule.Values);
Chris Lattner1f862af2003-04-16 18:13:57 +00001513 CurModule.DeclareNewGlobalValue(Fn, ValID::create($2));
Chris Lattnere1815642001-07-15 06:35:53 +00001514 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001515 free($2); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001516
Chris Lattner394f2eb2003-10-16 20:12:13 +00001517 CurFun.FunctionStart(Fn);
Chris Lattner00950542001-06-06 20:29:01 +00001518
Chris Lattner7e708292002-06-25 16:13:24 +00001519 // Add all of the arguments we parsed to the function...
Chris Lattner1f862af2003-04-16 18:13:57 +00001520 if ($4) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001521 if (isVarArg) { // Nuke the last entry
Chris Lattner1f862af2003-04-16 18:13:57 +00001522 assert($4->back().first->get() == Type::VoidTy && $4->back().second == 0&&
Chris Lattner69da5cf2002-10-13 20:57:00 +00001523 "Not a varargs marker!");
Chris Lattner1f862af2003-04-16 18:13:57 +00001524 delete $4->back().first;
1525 $4->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00001526 }
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001527 Function::aiterator ArgIt = Fn->abegin();
Chris Lattner9232b992003-04-22 18:42:41 +00001528 for (std::vector<std::pair<PATypeHolder*, char*> >::iterator I =$4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001529 I != $4->end(); ++I, ++ArgIt) {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001530 delete I->first; // Delete the typeholder...
1531
Chris Lattner8ab406d2004-07-13 07:59:27 +00001532 setValueName(ArgIt, I->second); // Insert arg into symtab...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001533 InsertValue(ArgIt);
Chris Lattner00950542001-06-06 20:29:01 +00001534 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001535
Chris Lattner1f862af2003-04-16 18:13:57 +00001536 delete $4; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00001537 }
Chris Lattner51727be2002-06-04 21:58:56 +00001538};
Chris Lattner00950542001-06-06 20:29:01 +00001539
Chris Lattner9b02cc32002-05-03 18:23:48 +00001540BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1541
Chris Lattner4ad02e72003-04-16 20:28:45 +00001542FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00001543 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001544
Chris Lattner4ad02e72003-04-16 20:28:45 +00001545 // Make sure that we keep track of the linkage type even if there was a
1546 // previous "declare".
1547 $$->setLinkage($1);
Chris Lattner1f862af2003-04-16 18:13:57 +00001548
Chris Lattner7e708292002-06-25 16:13:24 +00001549 // Resolve circular types before we parse the body of the function.
Chris Lattner394f2eb2003-10-16 20:12:13 +00001550 ResolveTypes(CurFun.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001551};
Chris Lattner00950542001-06-06 20:29:01 +00001552
Chris Lattner9b02cc32002-05-03 18:23:48 +00001553END : ENDTOK | '}'; // Allow end of '}' to end a function
1554
Chris Lattner79df7c02002-03-26 18:01:55 +00001555Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001556 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001557};
Chris Lattner00950542001-06-06 20:29:01 +00001558
Chris Lattner394f2eb2003-10-16 20:12:13 +00001559FunctionProto : DECLARE { CurFun.isDeclare = true; } FunctionHeaderH {
1560 $$ = CurFun.CurrentFunction;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001561 CurFun.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001562};
Chris Lattner00950542001-06-06 20:29:01 +00001563
1564//===----------------------------------------------------------------------===//
1565// Rules to match Basic Blocks
1566//===----------------------------------------------------------------------===//
1567
1568ConstValueRef : ESINT64VAL { // A reference to a direct constant
1569 $$ = ValID::create($1);
1570 }
1571 | EUINT64VAL {
1572 $$ = ValID::create($1);
1573 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001574 | FPVAL { // Perhaps it's an FP constant?
1575 $$ = ValID::create($1);
1576 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001577 | TRUETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001578 $$ = ValID::create(ConstantBool::True);
Chris Lattner00950542001-06-06 20:29:01 +00001579 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001580 | FALSETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001581 $$ = ValID::create(ConstantBool::False);
Chris Lattner00950542001-06-06 20:29:01 +00001582 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001583 | NULL_TOK {
1584 $$ = ValID::createNull();
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001585 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001586 | ConstExpr {
1587 $$ = ValID::create($1);
1588 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00001589
Chris Lattner2079fde2001-10-13 06:41:08 +00001590// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1591// another value.
1592//
1593SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001594 $$ = ValID::create($1);
1595 }
Chris Lattner6c23f572003-08-22 05:42:10 +00001596 | Name { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001597 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001598 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001599
1600// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001601ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001602
Chris Lattner00950542001-06-06 20:29:01 +00001603
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001604// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1605// type immediately preceeds the value reference, and allows complex constant
1606// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001607ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001608 $$ = getVal(*$1, $2); delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001609 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001610
Chris Lattner00950542001-06-06 20:29:01 +00001611BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001612 $$ = $1;
Chris Lattner00950542001-06-06 20:29:01 +00001613 }
Chris Lattner7e708292002-06-25 16:13:24 +00001614 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
Chris Lattner8ab406d2004-07-13 07:59:27 +00001615 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001616 };
Chris Lattner00950542001-06-06 20:29:01 +00001617
1618
1619// Basic blocks are terminated by branching instructions:
1620// br, br/cc, switch, ret
1621//
Chris Lattner2079fde2001-10-13 06:41:08 +00001622BasicBlock : InstructionList OptAssign BBTerminatorInst {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001623 setValueName($3, $2);
Chris Lattner2079fde2001-10-13 06:41:08 +00001624 InsertValue($3);
1625
1626 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001627 InsertValue($1);
1628 $$ = $1;
1629 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001630 | LABELSTR InstructionList OptAssign BBTerminatorInst {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001631 setValueName($4, $3);
Chris Lattner2079fde2001-10-13 06:41:08 +00001632 InsertValue($4);
1633
1634 $2->getInstList().push_back($4);
Chris Lattner8ab406d2004-07-13 07:59:27 +00001635 setValueName($2, $1);
Chris Lattner00950542001-06-06 20:29:01 +00001636
1637 InsertValue($2);
1638 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001639 };
Chris Lattner00950542001-06-06 20:29:01 +00001640
1641InstructionList : InstructionList Inst {
1642 $1->getInstList().push_back($2);
1643 $$ = $1;
1644 }
1645 | /* empty */ {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001646 $$ = CurBB = new BasicBlock("", CurFun.CurrentFunction);
Chris Lattner51727be2002-06-04 21:58:56 +00001647 };
Chris Lattner00950542001-06-06 20:29:01 +00001648
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001649BBTerminatorInst : RET ResolvedVal { // Return with a result...
1650 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001651 }
1652 | RET VOID { // Return with no result...
1653 $$ = new ReturnInst();
1654 }
1655 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner9636a912001-10-01 16:18:37 +00001656 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
Chris Lattner00950542001-06-06 20:29:01 +00001657 } // Conditional Branch...
1658 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner9636a912001-10-01 16:18:37 +00001659 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1660 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
Chris Lattner00950542001-06-06 20:29:01 +00001661 getVal(Type::BoolTy, $3));
1662 }
1663 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1664 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001665 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
Chris Lattner00950542001-06-06 20:29:01 +00001666 $$ = S;
1667
Chris Lattner9232b992003-04-22 18:42:41 +00001668 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00001669 E = $8->end();
1670 for (; I != E; ++I)
Chris Lattner4354f562003-08-23 23:14:52 +00001671 S->addCase(I->first, I->second);
Chris Lattnerf57a43d2004-04-17 23:49:15 +00001672 delete $8;
Chris Lattner00950542001-06-06 20:29:01 +00001673 }
Chris Lattner196850c2003-05-15 21:30:00 +00001674 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
1675 SwitchInst *S = new SwitchInst(getVal($2, $3),
1676 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
1677 $$ = S;
1678 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001679 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO ResolvedVal
Chris Lattnera58e3a12004-02-08 21:48:25 +00001680 UNWIND ResolvedVal {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001681 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001682 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001683
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001684 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1685 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001686 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001687 std::vector<const Type*> ParamTypes;
Chris Lattner2079fde2001-10-13 06:41:08 +00001688 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001689 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1690 I != E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001691 ParamTypes.push_back((*I)->getType());
1692 }
1693
1694 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1695 if (isVarArg) ParamTypes.pop_back();
1696
Chris Lattner79df7c02002-03-26 18:01:55 +00001697 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001698 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00001699 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001700
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001701 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00001702
1703 BasicBlock *Normal = dyn_cast<BasicBlock>($8);
1704 BasicBlock *Except = dyn_cast<BasicBlock>($10);
1705
1706 if (Normal == 0 || Except == 0)
1707 ThrowException("Invoke instruction without label destinations!");
1708
1709 // Create the call node...
1710 if (!$5) { // Has no arguments?
Chris Lattner9232b992003-04-22 18:42:41 +00001711 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001712 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001713 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00001714 // correctly!
1715 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001716 FunctionType::param_iterator I = Ty->param_begin();
1717 FunctionType::param_iterator E = Ty->param_end();
Chris Lattner9232b992003-04-22 18:42:41 +00001718 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001719
1720 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1721 if ((*ArgI)->getType() != *I)
1722 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001723 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001724
1725 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1726 ThrowException("Invalid number of parameters detected!");
1727
Chris Lattner6cdb0112001-11-26 16:54:11 +00001728 $$ = new InvokeInst(V, Normal, Except, *$5);
Chris Lattner2079fde2001-10-13 06:41:08 +00001729 }
Chris Lattner9cd42572003-12-23 22:18:36 +00001730 delete $2;
Chris Lattner2079fde2001-10-13 06:41:08 +00001731 delete $5;
Chris Lattner36143fc2003-09-08 18:54:55 +00001732 }
1733 | UNWIND {
1734 $$ = new UnwindInst();
Chris Lattner51727be2002-06-04 21:58:56 +00001735 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001736
1737
Chris Lattner00950542001-06-06 20:29:01 +00001738
1739JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1740 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001741 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001742 if (V == 0)
1743 ThrowException("May only switch on a constant pool value!");
1744
Chris Lattner9232b992003-04-22 18:42:41 +00001745 $$->push_back(std::make_pair(V, cast<BasicBlock>(getVal($5, $6))));
Chris Lattner00950542001-06-06 20:29:01 +00001746 }
1747 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00001748 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001749 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001750
1751 if (V == 0)
1752 ThrowException("May only switch on a constant pool value!");
1753
Chris Lattner9232b992003-04-22 18:42:41 +00001754 $$->push_back(std::make_pair(V, cast<BasicBlock>(getVal($4, $5))));
Chris Lattner51727be2002-06-04 21:58:56 +00001755 };
Chris Lattner00950542001-06-06 20:29:01 +00001756
1757Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001758 // Is this definition named?? if so, assign the name...
Chris Lattner8ab406d2004-07-13 07:59:27 +00001759 setValueName($2, $1);
Chris Lattner00950542001-06-06 20:29:01 +00001760 InsertValue($2);
1761 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001762};
Chris Lattner00950542001-06-06 20:29:01 +00001763
Chris Lattnerc24d2082001-06-11 15:04:20 +00001764PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Chris Lattner9232b992003-04-22 18:42:41 +00001765 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
1766 $$->push_back(std::make_pair(getVal(*$1, $3),
1767 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
Chris Lattner30c89792001-09-07 16:35:17 +00001768 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001769 }
1770 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1771 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001772 $1->push_back(std::make_pair(getVal($1->front().first->getType(), $4),
1773 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
Chris Lattner51727be2002-06-04 21:58:56 +00001774 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00001775
1776
Chris Lattner30c89792001-09-07 16:35:17 +00001777ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner9232b992003-04-22 18:42:41 +00001778 $$ = new std::vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001779 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001780 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001781 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001782 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001783 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00001784 };
Chris Lattner00950542001-06-06 20:29:01 +00001785
1786// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00001787ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001788
Chris Lattner4a6482b2002-09-10 19:57:26 +00001789InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1790 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint())
1791 ThrowException("Arithmetic operator requires integer or FP operands!");
1792 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1793 if ($$ == 0)
1794 ThrowException("binary operator returned null!");
1795 delete $2;
1796 }
1797 | LogicalOps Types ValueRef ',' ValueRef {
1798 if (!(*$2)->isIntegral())
1799 ThrowException("Logical operator requires integral operands!");
1800 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1801 if ($$ == 0)
1802 ThrowException("binary operator returned null!");
1803 delete $2;
1804 }
1805 | SetCondOps Types ValueRef ',' ValueRef {
Chris Lattner1cff96a2002-09-10 22:37:46 +00001806 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001807 if ($$ == 0)
1808 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001809 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001810 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00001811 | NOT ResolvedVal {
1812 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
1813 << " Replacing with 'xor'.\n";
1814
1815 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
1816 if (Ones == 0)
1817 ThrowException("Expected integral type for not instruction!");
1818
1819 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00001820 if ($$ == 0)
Chris Lattner699f1eb2002-08-14 17:12:33 +00001821 ThrowException("Could not create a xor instruction!");
Chris Lattner09083092001-07-08 04:57:15 +00001822 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001823 | ShiftOps ResolvedVal ',' ResolvedVal {
1824 if ($4->getType() != Type::UByteTy)
1825 ThrowException("Shift amount must be ubyte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001826 if (!$2->getType()->isInteger())
1827 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001828 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001829 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001830 | CAST ResolvedVal TO Types {
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001831 if (!$4->get()->isFirstClassType())
1832 ThrowException("cast instruction to a non-primitive type: '" +
1833 $4->get()->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001834 $$ = new CastInst($2, *$4);
1835 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001836 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001837 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1838 if ($2->getType() != Type::BoolTy)
1839 ThrowException("select condition must be boolean!");
1840 if ($4->getType() != $6->getType())
1841 ThrowException("select value types should match!");
1842 $$ = new SelectInst($2, $4, $6);
1843 }
Chris Lattner8f77dae2003-05-08 02:44:12 +00001844 | VA_ARG ResolvedVal ',' Types {
Chris Lattner99e7ab72003-10-18 05:53:13 +00001845 // FIXME: This is emulation code for an obsolete syntax. This should be
1846 // removed at some point.
1847 if (!ObsoleteVarArgs) {
1848 std::cerr << "WARNING: this file uses obsolete features. "
1849 << "Assemble and disassemble to update it.\n";
1850 ObsoleteVarArgs = true;
1851 }
1852
1853 // First, load the valist...
1854 Instruction *CurVAList = new LoadInst($2, "");
1855 CurBB->getInstList().push_back(CurVAList);
1856
1857 // Emit the vaarg instruction.
1858 $$ = new VAArgInst(CurVAList, *$4);
1859
1860 // Now we must advance the pointer and update it in memory.
1861 Instruction *TheVANext = new VANextInst(CurVAList, *$4);
1862 CurBB->getInstList().push_back(TheVANext);
1863
1864 CurBB->getInstList().push_back(new StoreInst(TheVANext, $2));
1865 delete $4;
1866 }
1867 | VAARG ResolvedVal ',' Types {
1868 $$ = new VAArgInst($2, *$4);
1869 delete $4;
1870 }
1871 | VANEXT ResolvedVal ',' Types {
1872 $$ = new VANextInst($2, *$4);
Chris Lattner8f77dae2003-05-08 02:44:12 +00001873 delete $4;
1874 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00001875 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001876 const Type *Ty = $2->front().first->getType();
Chris Lattner8ea8f362003-10-30 01:38:18 +00001877 if (!Ty->isFirstClassType())
1878 ThrowException("PHI node operands must be of first class type!");
Chris Lattnerc24d2082001-06-11 15:04:20 +00001879 $$ = new PHINode(Ty);
Chris Lattnerbd2531f2003-10-10 16:34:58 +00001880 $$->op_reserve($2->size()*2);
Chris Lattner00950542001-06-06 20:29:01 +00001881 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001882 if ($2->front().first->getType() != Ty)
1883 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001884 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001885 $2->pop_front();
1886 }
1887 delete $2; // Free the list...
1888 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001889 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001890 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001891 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001892
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001893 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1894 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001895 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001896 std::vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001897 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001898 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1899 I != E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00001900 ParamTypes.push_back((*I)->getType());
1901 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001902
1903 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1904 if (isVarArg) ParamTypes.pop_back();
1905
Chris Lattner79df7c02002-03-26 18:01:55 +00001906 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001907 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001908 }
Chris Lattner00950542001-06-06 20:29:01 +00001909
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001910 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001911
Chris Lattner8b81bf52001-07-25 22:47:46 +00001912 // Create the call node...
1913 if (!$5) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00001914 // Make sure no arguments is a good thing!
1915 if (Ty->getNumParams() != 0)
1916 ThrowException("No arguments passed to a function that "
1917 "expects arguments!");
1918
Chris Lattner9232b992003-04-22 18:42:41 +00001919 $$ = new CallInst(V, std::vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001920 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001921 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00001922 // correctly!
1923 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001924 FunctionType::param_iterator I = Ty->param_begin();
1925 FunctionType::param_iterator E = Ty->param_end();
Chris Lattner9232b992003-04-22 18:42:41 +00001926 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001927
1928 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1929 if ((*ArgI)->getType() != *I)
1930 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001931 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001932
Chris Lattner8b81bf52001-07-25 22:47:46 +00001933 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001934 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001935
Chris Lattner6cdb0112001-11-26 16:54:11 +00001936 $$ = new CallInst(V, *$5);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001937 }
Chris Lattnerf64d57a2003-12-23 20:39:17 +00001938 delete $2;
Chris Lattner8b81bf52001-07-25 22:47:46 +00001939 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001940 }
1941 | MemoryInst {
1942 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001943 };
Chris Lattner00950542001-06-06 20:29:01 +00001944
Chris Lattner6cdb0112001-11-26 16:54:11 +00001945
1946// IndexList - List of indices for GEP based instructions...
1947IndexList : ',' ValueRefList {
Chris Lattner15c9c032003-09-08 18:20:29 +00001948 $$ = $2;
1949 } | /* empty */ {
1950 $$ = new std::vector<Value*>();
1951 };
1952
1953OptVolatile : VOLATILE {
1954 $$ = true;
1955 }
1956 | /* empty */ {
1957 $$ = false;
1958 };
1959
Chris Lattner027dcc52001-07-08 21:10:27 +00001960
Chris Lattner00950542001-06-06 20:29:01 +00001961MemoryInst : MALLOC Types {
Chris Lattner05804b72002-09-13 22:28:45 +00001962 $$ = new MallocInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00001963 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001964 }
1965 | MALLOC Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00001966 $$ = new MallocInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001967 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001968 }
1969 | ALLOCA Types {
Chris Lattner05804b72002-09-13 22:28:45 +00001970 $$ = new AllocaInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00001971 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001972 }
1973 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00001974 $$ = new AllocaInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001975 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001976 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001977 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00001978 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001979 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00001980 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001981 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001982 }
1983
Chris Lattner15c9c032003-09-08 18:20:29 +00001984 | OptVolatile LOAD Types ValueRef {
1985 if (!isa<PointerType>($3->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00001986 ThrowException("Can't load from nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00001987 (*$3)->getDescription());
Chris Lattner79ad13802003-09-08 20:29:46 +00001988 $$ = new LoadInst(getVal(*$3, $4), "", $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00001989 delete $3;
Chris Lattner027dcc52001-07-08 21:10:27 +00001990 }
Chris Lattner15c9c032003-09-08 18:20:29 +00001991 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
1992 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00001993 if (!PT)
Chris Lattner72e00252001-12-14 16:28:42 +00001994 ThrowException("Can't store to a nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00001995 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00001996 const Type *ElTy = PT->getElementType();
Chris Lattner15c9c032003-09-08 18:20:29 +00001997 if (ElTy != $3->getType())
1998 ThrowException("Can't store '" + $3->getType()->getDescription() +
Chris Lattner72e00252001-12-14 16:28:42 +00001999 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner0383cc42002-08-21 23:51:21 +00002000
Chris Lattner79ad13802003-09-08 20:29:46 +00002001 $$ = new StoreInst($3, getVal(*$5, $6), $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002002 delete $5;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002003 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00002004 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00002005 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002006 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner830b6f92004-04-05 01:30:04 +00002007
2008 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
2009 // indices to uint struct indices for compatibility.
2010 generic_gep_type_iterator<std::vector<Value*>::iterator>
2011 GTI = gep_type_begin($2->get(), $4->begin(), $4->end()),
2012 GTE = gep_type_end($2->get(), $4->begin(), $4->end());
2013 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
2014 if (isa<StructType>(*GTI)) // Only change struct indices
2015 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
2016 if (CUI->getType() == Type::UByteTy)
2017 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
2018
Chris Lattner30c89792001-09-07 16:35:17 +00002019 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner830b6f92004-04-05 01:30:04 +00002020 ThrowException("Invalid getelementptr indices for type '" +
2021 (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00002022 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
2023 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00002024 };
Chris Lattner027dcc52001-07-08 21:10:27 +00002025
Brian Gaeked0fde302003-11-11 22:41:34 +00002026
Chris Lattner00950542001-06-06 20:29:01 +00002027%%
Chris Lattner09083092001-07-08 04:57:15 +00002028int yyerror(const char *ErrorMsg) {
Chris Lattner9232b992003-04-22 18:42:41 +00002029 std::string where
2030 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002031 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Chris Lattner9232b992003-04-22 18:42:41 +00002032 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
Chris Lattnerbc280ab2004-03-30 20:58:25 +00002033 if (yychar == YYEMPTY || yychar == 0)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002034 errMsg += "end-of-file.";
2035 else
Chris Lattner9232b992003-04-22 18:42:41 +00002036 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002037 ThrowException(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00002038 return 0;
2039}