blob: 7d6a9f7af36d55899eb721c5938d049787db52ef [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files --------*- C++ -*-===//
Misha Brukman5a2a3822005-05-10 22:02:28 +00002//
John Criswell856ba762003-10-21 15:17:13 +00003// 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.
Misha Brukman5a2a3822005-05-10 22:02:28 +00007//
John Criswell856ba762003-10-21 15:17:13 +00008//===----------------------------------------------------------------------===//
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 Lattnera8e8f162005-05-06 20:27:19 +000016#include "llvm/CallingConv.h"
Chris Lattneraa2c8532006-01-25 22:26:43 +000017#include "llvm/InlineAsm.h"
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000018#include "llvm/Instructions.h"
Chris Lattner00950542001-06-06 20:29:01 +000019#include "llvm/Module.h"
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000020#include "llvm/SymbolTable.h"
Reid Spencer0b118202006-01-16 21:12:35 +000021#include "llvm/Assembly/AutoUpgrade.h"
Chris Lattner830b6f92004-04-05 01:30:04 +000022#include "llvm/Support/GetElementPtrTypeIterator.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000023#include "llvm/ADT/STLExtras.h"
Chris Lattner87ac9722005-11-06 06:46:28 +000024#include "llvm/Support/MathExtras.h"
Reid Spencer8ce1da72004-07-04 12:19:05 +000025#include <algorithm>
26#include <iostream>
Chris Lattner00950542001-06-06 20:29:01 +000027#include <list>
Chris Lattnerc188eeb2002-07-30 18:54:25 +000028#include <utility>
Chris Lattner00950542001-06-06 20:29:01 +000029
Reid Spencere4f47592006-08-18 17:32:55 +000030// The following is a gross hack. In order to rid the libAsmParser library of
31// exceptions, we have to have a way of getting the yyparse function to go into
32// an error situation. So, whenever we want an error to occur, the GenerateError
33// function (see bottom of file) sets TriggerError. Then, at the end of each
34// production in the grammer we use CHECK_FOR_ERROR which will invoke YYERROR
35// (a goto) to put YACC in error state. Furthermore, several calls to
36// GenerateError are made from inside productions and they must simulate the
37// previous exception behavior by exiting the production immediately. We have
38// replaced these with the GEN_ERROR macro which calls GeneratError and then
39// immediately invokes YYERROR. This would be so much cleaner if it was a
40// recursive descent parser.
Reid Spencer61c83e02006-08-18 08:43:06 +000041static bool TriggerError = false;
42#define CHECK_FOR_ERROR { if (TriggerError) { TriggerError = false; YYERROR; } }
Reid Spencer61c83e02006-08-18 08:43:06 +000043#define GEN_ERROR(msg) { GenerateError(msg); YYERROR; }
44
Chris Lattner386a3b72001-10-16 19:54:17 +000045int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
Chris Lattner09083092001-07-08 04:57:15 +000046int yylex(); // declaration" of xxx warnings.
Chris Lattner00950542001-06-06 20:29:01 +000047int yyparse();
48
Brian Gaeked0fde302003-11-11 22:41:34 +000049namespace llvm {
Chris Lattner86427632004-07-14 06:39:48 +000050 std::string CurFilename;
51}
52using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000053
Chris Lattner00950542001-06-06 20:29:01 +000054static Module *ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +000055
Chris Lattner30c89792001-09-07 16:35:17 +000056// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
57// relating to upreferences in the input stream.
58//
59//#define DEBUG_UPREFS 1
60#ifdef DEBUG_UPREFS
Chris Lattner699f1eb2002-08-14 17:12:33 +000061#define UR_OUT(X) std::cerr << X
Chris Lattner30c89792001-09-07 16:35:17 +000062#else
63#define UR_OUT(X)
64#endif
65
Vikram S. Adved3f7eb02002-07-14 22:59:28 +000066#define YYERROR_VERBOSE 1
67
Andrew Lenharth558bc882005-06-18 18:34:52 +000068static bool ObsoleteVarArgs;
Andrew Lenharthe78f50d2005-06-19 03:53:56 +000069static bool NewVarArgs;
Chris Lattnerb2b96672005-11-12 18:21:21 +000070static BasicBlock *CurBB;
71static GlobalVariable *CurGV;
Andrew Lenharth558bc882005-06-18 18:34:52 +000072
73
Chris Lattner7e708292002-06-25 16:13:24 +000074// This contains info used when building the body of a function. It is
75// destroyed when the function is completed.
Chris Lattner00950542001-06-06 20:29:01 +000076//
Chris Lattner9232b992003-04-22 18:42:41 +000077typedef std::vector<Value *> ValueList; // Numbered defs
Misha Brukman5a2a3822005-05-10 22:02:28 +000078static void
79ResolveDefinitions(std::map<const Type *,ValueList> &LateResolvers,
80 std::map<const Type *,ValueList> *FutureLateResolvers = 0);
Chris Lattner00950542001-06-06 20:29:01 +000081
82static struct PerModuleInfo {
83 Module *CurrentModule;
Chris Lattner735f2702004-07-08 22:30:50 +000084 std::map<const Type *, ValueList> Values; // Module level numbered definitions
85 std::map<const Type *,ValueList> LateResolveValues;
Chris Lattner16af11d2004-02-09 21:03:38 +000086 std::vector<PATypeHolder> Types;
Chris Lattner9232b992003-04-22 18:42:41 +000087 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000088
Chris Lattnerbc721ed2004-07-13 08:28:21 +000089 /// PlaceHolderInfo - When temporary placeholder objects are created, remember
Reid Spencerfd20c0a2006-05-29 02:34:34 +000090 /// how they were referenced and on which line of the input they came from so
Chris Lattner22ae2322004-07-13 08:39:15 +000091 /// that we can resolve them later and print error messages as appropriate.
Chris Lattnerbc721ed2004-07-13 08:28:21 +000092 std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
93
Chris Lattner2079fde2001-10-13 06:41:08 +000094 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
95 // references to global values. Global values may be referenced before they
96 // are defined, and if so, the temporary object that they represent is held
Reid Spencer3271ed52004-07-18 00:08:11 +000097 // here. This is used for forward references of GlobalValues.
Chris Lattner2079fde2001-10-13 06:41:08 +000098 //
Chris Lattner9232b992003-04-22 18:42:41 +000099 typedef std::map<std::pair<const PointerType *,
Chris Lattnerbc207592004-03-08 06:09:57 +0000100 ValID>, GlobalValue*> GlobalRefsType;
Chris Lattner2079fde2001-10-13 06:41:08 +0000101 GlobalRefsType GlobalRefs;
102
Chris Lattner00950542001-06-06 20:29:01 +0000103 void ModuleDone() {
Chris Lattner7e708292002-06-25 16:13:24 +0000104 // If we could not resolve some functions at function compilation time
105 // (calls to functions before they are defined), resolve them now... Types
106 // are resolved when the constant pool has been completely parsed.
Chris Lattner30c89792001-09-07 16:35:17 +0000107 //
Chris Lattner00950542001-06-06 20:29:01 +0000108 ResolveDefinitions(LateResolveValues);
109
Chris Lattner2079fde2001-10-13 06:41:08 +0000110 // Check to make sure that all global value forward references have been
111 // resolved!
112 //
113 if (!GlobalRefs.empty()) {
Chris Lattner9232b992003-04-22 18:42:41 +0000114 std::string UndefinedReferences = "Unresolved global references exist:\n";
Misha Brukman5a2a3822005-05-10 22:02:28 +0000115
Chris Lattner749ce032002-03-11 22:12:39 +0000116 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
117 I != E; ++I) {
118 UndefinedReferences += " " + I->first.first->getDescription() + " " +
119 I->first.second.getName() + "\n";
120 }
Reid Spencer61c83e02006-08-18 08:43:06 +0000121 GenerateError(UndefinedReferences);
Chris Lattner2079fde2001-10-13 06:41:08 +0000122 }
123
Reid Spencere812fb22006-01-19 01:21:04 +0000124 // Look for intrinsic functions and CallInst that need to be upgraded
Chris Lattner7d5c1e12006-03-04 07:53:16 +0000125 for (Module::iterator FI = CurrentModule->begin(),
126 FE = CurrentModule->end(); FI != FE; )
127 UpgradeCallsToIntrinsic(FI++);
Reid Spencer0b118202006-01-16 21:12:35 +0000128
Chris Lattner7e708292002-06-25 16:13:24 +0000129 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000130 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +0000131 CurrentModule = 0;
132 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000133
Chris Lattner8a327842004-07-14 21:44:00 +0000134 // GetForwardRefForGlobal - Check to see if there is a forward reference
135 // for this global. If so, remove it from the GlobalRefs map and return it.
136 // If not, just return null.
137 GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) {
138 // Check to see if there is a forward reference to this global variable...
139 // if there is, eliminate it and patch the reference to use the new def'n.
140 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID));
141 GlobalValue *Ret = 0;
142 if (I != GlobalRefs.end()) {
143 Ret = I->second;
144 GlobalRefs.erase(I);
145 }
146 return Ret;
147 }
Chris Lattner00950542001-06-06 20:29:01 +0000148} CurModule;
149
Chris Lattner79df7c02002-03-26 18:01:55 +0000150static struct PerFunctionInfo {
Chris Lattner7e708292002-06-25 16:13:24 +0000151 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner00950542001-06-06 20:29:01 +0000152
Chris Lattner735f2702004-07-08 22:30:50 +0000153 std::map<const Type*, ValueList> Values; // Keep track of #'d definitions
154 std::map<const Type*, ValueList> LateResolveValues;
Chris Lattner7e708292002-06-25 16:13:24 +0000155 bool isDeclare; // Is this function a forward declararation?
Chris Lattner00950542001-06-06 20:29:01 +0000156
Chris Lattner2e2ed292004-07-14 06:28:35 +0000157 /// BBForwardRefs - When we see forward references to basic blocks, keep
158 /// track of them here.
159 std::map<BasicBlock*, std::pair<ValID, int> > BBForwardRefs;
160 std::vector<BasicBlock*> NumberedBlocks;
161 unsigned NextBBNum;
162
Chris Lattner79df7c02002-03-26 18:01:55 +0000163 inline PerFunctionInfo() {
164 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000165 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000166 }
167
Chris Lattner79df7c02002-03-26 18:01:55 +0000168 inline void FunctionStart(Function *M) {
169 CurrentFunction = M;
Chris Lattner2e2ed292004-07-14 06:28:35 +0000170 NextBBNum = 0;
Chris Lattner00950542001-06-06 20:29:01 +0000171 }
172
Chris Lattner79df7c02002-03-26 18:01:55 +0000173 void FunctionDone() {
Chris Lattner2e2ed292004-07-14 06:28:35 +0000174 NumberedBlocks.clear();
175
176 // Any forward referenced blocks left?
177 if (!BBForwardRefs.empty())
Reid Spencer61c83e02006-08-18 08:43:06 +0000178 GenerateError("Undefined reference to label " +
Chris Lattner83beacd2005-02-24 04:59:49 +0000179 BBForwardRefs.begin()->first->getName());
Chris Lattner2e2ed292004-07-14 06:28:35 +0000180
181 // Resolve all forward references now.
Chris Lattner386a3b72001-10-16 19:54:17 +0000182 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000183
Chris Lattner7e708292002-06-25 16:13:24 +0000184 Values.clear(); // Clear out function local definitions
Chris Lattner79df7c02002-03-26 18:01:55 +0000185 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000186 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000187 }
Chris Lattner394f2eb2003-10-16 20:12:13 +0000188} CurFun; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000189
Chris Lattner394f2eb2003-10-16 20:12:13 +0000190static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000191
Chris Lattner00950542001-06-06 20:29:01 +0000192
193//===----------------------------------------------------------------------===//
194// Code to handle definitions of all the types
195//===----------------------------------------------------------------------===//
196
Chris Lattner735f2702004-07-08 22:30:50 +0000197static int InsertValue(Value *V,
198 std::map<const Type*,ValueList> &ValueTab = CurFun.Values) {
199 if (V->hasName()) return -1; // Is this a numbered definition?
Chris Lattner2079fde2001-10-13 06:41:08 +0000200
201 // Yes, insert the value into the value table...
Chris Lattner735f2702004-07-08 22:30:50 +0000202 ValueList &List = ValueTab[V->getType()];
203 List.push_back(V);
Chris Lattner16af11d2004-02-09 21:03:38 +0000204 return List.size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000205}
206
Chris Lattner30c89792001-09-07 16:35:17 +0000207static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000208 switch (D.Type) {
Chris Lattnera09000d2004-07-14 23:03:46 +0000209 case ValID::NumberVal: // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000210 // Module constants occupy the lowest numbered slots...
Misha Brukman5a2a3822005-05-10 22:02:28 +0000211 if ((unsigned)D.Num < CurModule.Types.size())
Chris Lattnera09000d2004-07-14 23:03:46 +0000212 return CurModule.Types[(unsigned)D.Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000213 break;
Chris Lattnera09000d2004-07-14 23:03:46 +0000214 case ValID::NameVal: // Is it a named definition?
215 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.Name)) {
216 D.destroy(); // Free old strdup'd memory...
217 return N;
Chris Lattner6e6026b2002-11-20 18:36:02 +0000218 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000219 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000220 default:
Reid Spencer61c83e02006-08-18 08:43:06 +0000221 GenerateError("Internal parser error: Invalid symbol type reference!");
Chris Lattner30c89792001-09-07 16:35:17 +0000222 }
223
224 // If we reached here, we referenced either a symbol that we don't know about
225 // or an id number that hasn't been read yet. We may be referencing something
226 // forward, so just create an entry to be resolved later and get to it...
227 //
228 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
229
Chris Lattner355ad1f2005-03-21 06:27:42 +0000230
231 if (inFunctionScope()) {
232 if (D.Type == ValID::NameVal)
Reid Spencer61c83e02006-08-18 08:43:06 +0000233 GenerateError("Reference to an undefined type: '" + D.getName() + "'");
Chris Lattner355ad1f2005-03-21 06:27:42 +0000234 else
Reid Spencer61c83e02006-08-18 08:43:06 +0000235 GenerateError("Reference to an undefined type: #" + itostr(D.Num));
Chris Lattner4a42e902001-10-22 05:56:09 +0000236 }
Chris Lattner30c89792001-09-07 16:35:17 +0000237
Chris Lattner355ad1f2005-03-21 06:27:42 +0000238 std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D);
239 if (I != CurModule.LateResolveTypes.end())
240 return I->second;
241
Chris Lattner82269592001-10-22 06:01:08 +0000242 Type *Typ = OpaqueType::get();
Chris Lattner355ad1f2005-03-21 06:27:42 +0000243 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
Chris Lattner30c89792001-09-07 16:35:17 +0000244 return Typ;
Chris Lattner355ad1f2005-03-21 06:27:42 +0000245 }
Chris Lattner30c89792001-09-07 16:35:17 +0000246
Chris Lattner9232b992003-04-22 18:42:41 +0000247static Value *lookupInSymbolTable(const Type *Ty, const std::string &Name) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000248 SymbolTable &SymTab =
Chris Lattner394f2eb2003-10-16 20:12:13 +0000249 inFunctionScope() ? CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner9705a152002-05-02 19:27:42 +0000250 CurModule.CurrentModule->getSymbolTable();
Chris Lattner6e6026b2002-11-20 18:36:02 +0000251 return SymTab.lookup(Ty, Name);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000252}
253
Chris Lattner2079fde2001-10-13 06:41:08 +0000254// getValNonImprovising - Look up the value specified by the provided type and
255// the provided ValID. If the value exists and has already been defined, return
256// it. Otherwise return null.
257//
258static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000259 if (isa<FunctionType>(Ty))
Reid Spencer61c83e02006-08-18 08:43:06 +0000260 GenerateError("Functions are not values and "
Chris Lattner79df7c02002-03-26 18:01:55 +0000261 "must be referenced as pointers");
Chris Lattner386a3b72001-10-16 19:54:17 +0000262
Chris Lattner30c89792001-09-07 16:35:17 +0000263 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000264 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner00950542001-06-06 20:29:01 +0000265 unsigned Num = (unsigned)D.Num;
266
267 // Module constants occupy the lowest numbered slots...
Chris Lattner735f2702004-07-08 22:30:50 +0000268 std::map<const Type*,ValueList>::iterator VI = CurModule.Values.find(Ty);
Chris Lattner16af11d2004-02-09 21:03:38 +0000269 if (VI != CurModule.Values.end()) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000270 if (Num < VI->second.size())
Chris Lattner16af11d2004-02-09 21:03:38 +0000271 return VI->second[Num];
272 Num -= VI->second.size();
Chris Lattner00950542001-06-06 20:29:01 +0000273 }
274
275 // Make sure that our type is within bounds
Chris Lattner735f2702004-07-08 22:30:50 +0000276 VI = CurFun.Values.find(Ty);
Chris Lattner16af11d2004-02-09 21:03:38 +0000277 if (VI == CurFun.Values.end()) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000278
279 // Check that the number is within bounds...
Chris Lattner16af11d2004-02-09 21:03:38 +0000280 if (VI->second.size() <= Num) return 0;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000281
Chris Lattner16af11d2004-02-09 21:03:38 +0000282 return VI->second[Num];
Chris Lattner00950542001-06-06 20:29:01 +0000283 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000284
Chris Lattner1a1cb112001-09-30 22:46:54 +0000285 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000286 Value *N = lookupInSymbolTable(Ty, std::string(D.Name));
Chris Lattner2079fde2001-10-13 06:41:08 +0000287 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000288
289 D.destroy(); // Free old strdup'd memory...
290 return N;
291 }
292
Misha Brukman5a2a3822005-05-10 22:02:28 +0000293 // Check to make sure that "Ty" is an integral type, and that our
Chris Lattner2079fde2001-10-13 06:41:08 +0000294 // value will fit into the specified type...
295 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattnerd78700d2002-08-16 21:14:40 +0000296 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
Reid Spencer61c83e02006-08-18 08:43:06 +0000297 GenerateError("Signed integral constant '" +
Misha Brukman5a2a3822005-05-10 22:02:28 +0000298 itostr(D.ConstPool64) + "' is invalid for type '" +
Chris Lattnerd78700d2002-08-16 21:14:40 +0000299 Ty->getDescription() + "'!");
300 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000301
302 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000303 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
304 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000305 GenerateError("Integral constant '" + utostr(D.UConstPool64) +
Chris Lattnerf8dff732002-07-18 05:18:37 +0000306 "' is invalid or out of range!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000307 } else { // This is really a signed reference. Transmogrify.
Reid Spencer3271ed52004-07-18 00:08:11 +0000308 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000309 }
310 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000311 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000312 }
313
Chris Lattner2079fde2001-10-13 06:41:08 +0000314 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000315 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Reid Spencer61c83e02006-08-18 08:43:06 +0000316 GenerateError("FP constant invalid for type!!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000317 return ConstantFP::get(Ty, D.ConstPoolFP);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000318
Chris Lattner2079fde2001-10-13 06:41:08 +0000319 case ValID::ConstNullVal: // Is it a null value?
Chris Lattner9b625032002-05-06 16:15:30 +0000320 if (!isa<PointerType>(Ty))
Reid Spencer61c83e02006-08-18 08:43:06 +0000321 GenerateError("Cannot create a a non pointer null!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000322 return ConstantPointerNull::get(cast<PointerType>(Ty));
Misha Brukman5a2a3822005-05-10 22:02:28 +0000323
Chris Lattner16710e92004-10-16 18:17:13 +0000324 case ValID::ConstUndefVal: // Is it an undef value?
325 return UndefValue::get(Ty);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000326
Chris Lattnerf1f03df2005-12-21 17:53:02 +0000327 case ValID::ConstZeroVal: // Is it a zero value?
328 return Constant::getNullValue(Ty);
329
Chris Lattnerd78700d2002-08-16 21:14:40 +0000330 case ValID::ConstantVal: // Fully resolved constant?
331 if (D.ConstantValue->getType() != Ty)
Reid Spencer61c83e02006-08-18 08:43:06 +0000332 GenerateError("Constant expression type different from required type!");
Chris Lattnerd78700d2002-08-16 21:14:40 +0000333 return D.ConstantValue;
334
Chris Lattneraa2c8532006-01-25 22:26:43 +0000335 case ValID::InlineAsmVal: { // Inline asm expression
336 const PointerType *PTy = dyn_cast<PointerType>(Ty);
337 const FunctionType *FTy =
338 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
339 if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints))
Reid Spencer61c83e02006-08-18 08:43:06 +0000340 GenerateError("Invalid type for asm constraint string!");
Chris Lattneraa2c8532006-01-25 22:26:43 +0000341 InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints,
342 D.IAD->HasSideEffects);
343 D.destroy(); // Free InlineAsmDescriptor.
344 return IA;
345 }
Chris Lattner30c89792001-09-07 16:35:17 +0000346 default:
347 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000348 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000349 } // End of switch
350
Chris Lattner2079fde2001-10-13 06:41:08 +0000351 assert(0 && "Unhandled case!");
352 return 0;
353}
354
Chris Lattner2079fde2001-10-13 06:41:08 +0000355// getVal - This function is identical to getValNonImprovising, except that if a
356// value is not already defined, it "improvises" by creating a placeholder var
357// that looks and acts just like the requested variable. When the value is
358// defined later, all uses of the placeholder variable are replaced with the
359// real thing.
360//
Chris Lattner64116f92004-07-14 01:33:11 +0000361static Value *getVal(const Type *Ty, const ValID &ID) {
362 if (Ty == Type::LabelTy)
Reid Spencer61c83e02006-08-18 08:43:06 +0000363 GenerateError("Cannot use a basic block here");
Chris Lattner2079fde2001-10-13 06:41:08 +0000364
Chris Lattner64116f92004-07-14 01:33:11 +0000365 // See if the value has already been defined.
366 Value *V = getValNonImprovising(Ty, ID);
Chris Lattner2079fde2001-10-13 06:41:08 +0000367 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000368
Chris Lattner60cd9552005-03-23 01:29:26 +0000369 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty))
Reid Spencer61c83e02006-08-18 08:43:06 +0000370 GenerateError("Invalid use of a composite type!");
Chris Lattner60cd9552005-03-23 01:29:26 +0000371
Chris Lattner00950542001-06-06 20:29:01 +0000372 // 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 Lattner64116f92004-07-14 01:33:11 +0000376 V = new Argument(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000377
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000378 // Remember where this forward reference came from. FIXME, shouldn't we try
379 // to recycle these things??
Chris Lattner64116f92004-07-14 01:33:11 +0000380 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000381 llvmAsmlineno)));
382
Chris Lattner79df7c02002-03-26 18:01:55 +0000383 if (inFunctionScope())
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000384 InsertValue(V, CurFun.LateResolveValues);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000385 else
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000386 InsertValue(V, CurModule.LateResolveValues);
387 return V;
Chris Lattner00950542001-06-06 20:29:01 +0000388}
389
Chris Lattner2e2ed292004-07-14 06:28:35 +0000390/// getBBVal - This is used for two purposes:
391/// * If isDefinition is true, a new basic block with the specified ID is being
392/// defined.
393/// * If isDefinition is true, this is a reference to a basic block, which may
394/// or may not be a forward reference.
395///
396static BasicBlock *getBBVal(const ValID &ID, bool isDefinition = false) {
Chris Lattner64116f92004-07-14 01:33:11 +0000397 assert(inFunctionScope() && "Can't get basic block at global scope!");
398
Chris Lattner2e2ed292004-07-14 06:28:35 +0000399 std::string Name;
400 BasicBlock *BB = 0;
401 switch (ID.Type) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000402 default: GenerateError("Illegal label reference " + ID.getName());
Chris Lattner2e2ed292004-07-14 06:28:35 +0000403 case ValID::NumberVal: // Is it a numbered definition?
404 if (unsigned(ID.Num) >= CurFun.NumberedBlocks.size())
405 CurFun.NumberedBlocks.resize(ID.Num+1);
406 BB = CurFun.NumberedBlocks[ID.Num];
407 break;
408 case ValID::NameVal: // Is it a named definition?
409 Name = ID.Name;
Chris Lattnera09000d2004-07-14 23:03:46 +0000410 if (Value *N = CurFun.CurrentFunction->
411 getSymbolTable().lookup(Type::LabelTy, Name))
Chris Lattner2e2ed292004-07-14 06:28:35 +0000412 BB = cast<BasicBlock>(N);
413 break;
414 }
Chris Lattner64116f92004-07-14 01:33:11 +0000415
Chris Lattner2e2ed292004-07-14 06:28:35 +0000416 // See if the block has already been defined.
417 if (BB) {
418 // If this is the definition of the block, make sure the existing value was
419 // just a forward reference. If it was a forward reference, there will be
420 // an entry for it in the PlaceHolderInfo map.
421 if (isDefinition && !CurFun.BBForwardRefs.erase(BB))
422 // The existing value was a definition, not a forward reference.
Reid Spencer61c83e02006-08-18 08:43:06 +0000423 GenerateError("Redefinition of label " + ID.getName());
Chris Lattner64116f92004-07-14 01:33:11 +0000424
Chris Lattner2e2ed292004-07-14 06:28:35 +0000425 ID.destroy(); // Free strdup'd memory.
426 return BB;
427 }
428
429 // Otherwise this block has not been seen before.
430 BB = new BasicBlock("", CurFun.CurrentFunction);
431 if (ID.Type == ValID::NameVal) {
432 BB->setName(ID.Name);
433 } else {
434 CurFun.NumberedBlocks[ID.Num] = BB;
435 }
436
437 // If this is not a definition, keep track of it so we can use it as a forward
438 // reference.
439 if (!isDefinition) {
440 // Remember where this forward reference came from.
441 CurFun.BBForwardRefs[BB] = std::make_pair(ID, llvmAsmlineno);
442 } else {
443 // The forward declaration could have been inserted anywhere in the
444 // function: insert it into the correct place now.
445 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
446 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
447 }
Chris Lattnere8343a52004-10-26 18:26:14 +0000448 ID.destroy();
Chris Lattner64116f92004-07-14 01:33:11 +0000449 return BB;
450}
451
Chris Lattner00950542001-06-06 20:29:01 +0000452
453//===----------------------------------------------------------------------===//
454// Code to handle forward references in instructions
455//===----------------------------------------------------------------------===//
456//
457// This code handles the late binding needed with statements that reference
458// values not defined yet... for example, a forward branch, or the PHI node for
459// a loop body.
460//
Chris Lattner394f2eb2003-10-16 20:12:13 +0000461// This keeps a table (CurFun.LateResolveValues) of all such forward references
Chris Lattner00950542001-06-06 20:29:01 +0000462// and back patchs after we are done.
463//
464
Misha Brukman5a2a3822005-05-10 22:02:28 +0000465// ResolveDefinitions - If we could not resolve some defs at parsing
466// time (forward branches, phi functions for loops, etc...) resolve the
Chris Lattner00950542001-06-06 20:29:01 +0000467// defs now...
468//
Misha Brukman5a2a3822005-05-10 22:02:28 +0000469static void
470ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers,
471 std::map<const Type*,ValueList> *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000472 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Chris Lattner735f2702004-07-08 22:30:50 +0000473 for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(),
Chris Lattner16af11d2004-02-09 21:03:38 +0000474 E = LateResolvers.end(); LRI != E; ++LRI) {
475 ValueList &List = LRI->second;
476 while (!List.empty()) {
477 Value *V = List.back();
478 List.pop_back();
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000479
480 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
481 CurModule.PlaceHolderInfo.find(V);
482 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
483
484 ValID &DID = PHI->second.first;
Chris Lattner00950542001-06-06 20:29:01 +0000485
Chris Lattner735f2702004-07-08 22:30:50 +0000486 Value *TheRealValue = getValNonImprovising(LRI->first, DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000487 if (TheRealValue) {
488 V->replaceAllUsesWith(TheRealValue);
489 delete V;
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000490 CurModule.PlaceHolderInfo.erase(PHI);
Chris Lattner386a3b72001-10-16 19:54:17 +0000491 } else if (FutureLateResolvers) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000492 // Functions have their unresolved items forwarded to the module late
Chris Lattner386a3b72001-10-16 19:54:17 +0000493 // resolver table
494 InsertValue(V, *FutureLateResolvers);
495 } else {
Reid Spencer3271ed52004-07-18 00:08:11 +0000496 if (DID.Type == ValID::NameVal)
Reid Spencer61c83e02006-08-18 08:43:06 +0000497 GenerateError("Reference to an invalid definition: '" +DID.getName()+
Reid Spencer3271ed52004-07-18 00:08:11 +0000498 "' of type '" + V->getType()->getDescription() + "'",
499 PHI->second.second);
500 else
Reid Spencer61c83e02006-08-18 08:43:06 +0000501 GenerateError("Reference to an invalid definition: #" +
Misha Brukman5a2a3822005-05-10 22:02:28 +0000502 itostr(DID.Num) + " of type '" +
Reid Spencer3271ed52004-07-18 00:08:11 +0000503 V->getType()->getDescription() + "'",
504 PHI->second.second);
Chris Lattner30c89792001-09-07 16:35:17 +0000505 }
Chris Lattner00950542001-06-06 20:29:01 +0000506 }
507 }
508
509 LateResolvers.clear();
510}
511
Chris Lattner4a42e902001-10-22 05:56:09 +0000512// ResolveTypeTo - A brand new type was just declared. This means that (if
513// name is not null) things referencing Name can be resolved. Otherwise, things
514// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000515//
Chris Lattner4a42e902001-10-22 05:56:09 +0000516static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner355ad1f2005-03-21 06:27:42 +0000517 ValID D;
518 if (Name) D = ValID::create(Name);
519 else D = ValID::create((int)CurModule.Types.size());
Chris Lattner00950542001-06-06 20:29:01 +0000520
Chris Lattner355ad1f2005-03-21 06:27:42 +0000521 std::map<ValID, PATypeHolder>::iterator I =
522 CurModule.LateResolveTypes.find(D);
523 if (I != CurModule.LateResolveTypes.end()) {
524 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
525 CurModule.LateResolveTypes.erase(I);
Chris Lattner30c89792001-09-07 16:35:17 +0000526 }
527}
528
Chris Lattner1781aca2001-09-18 04:00:54 +0000529// setValueName - Set the specified value to the name given. The name may be
530// null potentially, in which case this is a noop. The string passed in is
Chris Lattner8ab406d2004-07-13 07:59:27 +0000531// assumed to be a malloc'd string buffer, and is free'd by this function.
532//
533static void setValueName(Value *V, char *NameStr) {
534 if (NameStr) {
535 std::string Name(NameStr); // Copy string
536 free(NameStr); // Free old string
Chris Lattnerc9aea522004-07-13 08:12:39 +0000537
Misha Brukman5a2a3822005-05-10 22:02:28 +0000538 if (V->getType() == Type::VoidTy)
Reid Spencer61c83e02006-08-18 08:43:06 +0000539 GenerateError("Can't assign name '" + Name+"' to value with void type!");
Misha Brukman5a2a3822005-05-10 22:02:28 +0000540
Chris Lattner8ab406d2004-07-13 07:59:27 +0000541 assert(inFunctionScope() && "Must be in function scope!");
542 SymbolTable &ST = CurFun.CurrentFunction->getSymbolTable();
543 if (ST.lookup(V->getType(), Name))
Reid Spencer61c83e02006-08-18 08:43:06 +0000544 GenerateError("Redefinition of value named '" + Name + "' in the '" +
Chris Lattner8ab406d2004-07-13 07:59:27 +0000545 V->getType()->getDescription() + "' type plane!");
Misha Brukman5a2a3822005-05-10 22:02:28 +0000546
Chris Lattnerc9aea522004-07-13 08:12:39 +0000547 // Set the name.
Chris Lattner262bb9a2005-03-05 19:04:07 +0000548 V->setName(Name);
Chris Lattner8ab406d2004-07-13 07:59:27 +0000549 }
550}
551
Chris Lattnerd88998d2004-07-14 08:23:52 +0000552/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
553/// this is a declaration, otherwise it is a definition.
Chris Lattnerb2b96672005-11-12 18:21:21 +0000554static GlobalVariable *
555ParseGlobalVariable(char *NameStr,GlobalValue::LinkageTypes Linkage,
556 bool isConstantGlobal, const Type *Ty,
557 Constant *Initializer) {
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000558 if (isa<FunctionType>(Ty))
Reid Spencer61c83e02006-08-18 08:43:06 +0000559 GenerateError("Cannot declare global vars of function type!");
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000560
Misha Brukman5a2a3822005-05-10 22:02:28 +0000561 const PointerType *PTy = PointerType::get(Ty);
Chris Lattnera09000d2004-07-14 23:03:46 +0000562
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000563 std::string Name;
564 if (NameStr) {
565 Name = NameStr; // Copy string
566 free(NameStr); // Free old string
Chris Lattnerd88998d2004-07-14 08:23:52 +0000567 }
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000568
Chris Lattner8a327842004-07-14 21:44:00 +0000569 // See if this global value was forward referenced. If so, recycle the
570 // object.
Misha Brukman5a2a3822005-05-10 22:02:28 +0000571 ValID ID;
Chris Lattner8a327842004-07-14 21:44:00 +0000572 if (!Name.empty()) {
573 ID = ValID::create((char*)Name.c_str());
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000574 } else {
Chris Lattner8a327842004-07-14 21:44:00 +0000575 ID = ValID::create((int)CurModule.Values[PTy].size());
576 }
577
578 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000579 // Move the global to the end of the list, from whereever it was
Chris Lattner8a327842004-07-14 21:44:00 +0000580 // previously inserted.
581 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
582 CurModule.CurrentModule->getGlobalList().remove(GV);
583 CurModule.CurrentModule->getGlobalList().push_back(GV);
584 GV->setInitializer(Initializer);
585 GV->setLinkage(Linkage);
586 GV->setConstant(isConstantGlobal);
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000587 InsertValue(GV, CurModule.Values);
Chris Lattnerb2b96672005-11-12 18:21:21 +0000588 return GV;
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000589 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000590
591 // If this global has a name, check to see if there is already a definition
592 // of this global in the module. If so, merge as appropriate. Note that
593 // this is really just a hack around problems in the CFE. :(
594 if (!Name.empty()) {
595 // We are a simple redefinition of a value, check to see if it is defined
596 // the same as the old one.
Misha Brukman5a2a3822005-05-10 22:02:28 +0000597 if (GlobalVariable *EGV =
Chris Lattnera09000d2004-07-14 23:03:46 +0000598 CurModule.CurrentModule->getGlobalVariable(Name, Ty)) {
599 // We are allowed to redefine a global variable in two circumstances:
Misha Brukman5a2a3822005-05-10 22:02:28 +0000600 // 1. If at least one of the globals is uninitialized or
Chris Lattnera09000d2004-07-14 23:03:46 +0000601 // 2. If both initializers have the same value.
602 //
603 if (!EGV->hasInitializer() || !Initializer ||
604 EGV->getInitializer() == Initializer) {
605
606 // Make sure the existing global version gets the initializer! Make
607 // sure that it also gets marked const if the new version is.
608 if (Initializer && !EGV->hasInitializer())
609 EGV->setInitializer(Initializer);
610 if (isConstantGlobal)
611 EGV->setConstant(true);
612 EGV->setLinkage(Linkage);
Chris Lattnerb2b96672005-11-12 18:21:21 +0000613 return EGV;
Chris Lattnera09000d2004-07-14 23:03:46 +0000614 }
615
Reid Spencer61c83e02006-08-18 08:43:06 +0000616 GenerateError("Redefinition of global variable named '" + Name +
Chris Lattnera09000d2004-07-14 23:03:46 +0000617 "' in the '" + Ty->getDescription() + "' type plane!");
618 }
619 }
620
621 // Otherwise there is no existing GV to use, create one now.
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000622 GlobalVariable *GV =
Misha Brukman5a2a3822005-05-10 22:02:28 +0000623 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000624 CurModule.CurrentModule);
625 InsertValue(GV, CurModule.Values);
Chris Lattnerb2b96672005-11-12 18:21:21 +0000626 return GV;
Chris Lattnerd88998d2004-07-14 08:23:52 +0000627}
Chris Lattner8ab406d2004-07-13 07:59:27 +0000628
Reid Spencer75719bf2004-05-26 21:48:31 +0000629// setTypeName - Set the specified type to the name given. The name may be
630// null potentially, in which case this is a noop. The string passed in is
631// assumed to be a malloc'd string buffer, and is freed by this function.
632//
633// This function returns true if the type has already been defined, but is
634// allowed to be redefined in the specified context. If the name is a new name
635// for the type plane, it is inserted and false is returned.
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000636static bool setTypeName(const Type *T, char *NameStr) {
Chris Lattnera09000d2004-07-14 23:03:46 +0000637 assert(!inFunctionScope() && "Can't give types function-local names!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000638 if (NameStr == 0) return false;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000639
Reid Spencer75719bf2004-05-26 21:48:31 +0000640 std::string Name(NameStr); // Copy string
641 free(NameStr); // Free old string
642
643 // We don't allow assigning names to void type
Misha Brukman5a2a3822005-05-10 22:02:28 +0000644 if (T == Type::VoidTy)
Reid Spencer61c83e02006-08-18 08:43:06 +0000645 GenerateError("Can't assign name '" + Name + "' to the void type!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000646
Chris Lattnera09000d2004-07-14 23:03:46 +0000647 // Set the type name, checking for conflicts as we do so.
648 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000649
Chris Lattnera09000d2004-07-14 23:03:46 +0000650 if (AlreadyExists) { // Inserting a name that is already defined???
651 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
652 assert(Existing && "Conflict but no matching type?");
653
Reid Spencer75719bf2004-05-26 21:48:31 +0000654 // There is only one case where this is allowed: when we are refining an
655 // opaque type. In this case, Existing will be an opaque type.
656 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
657 // We ARE replacing an opaque type!
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000658 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000659 return true;
660 }
661
662 // Otherwise, this is an attempt to redefine a type. That's okay if
663 // the redefinition is identical to the original. This will be so if
664 // Existing and T point to the same Type object. In this one case we
665 // allow the equivalent redefinition.
666 if (Existing == T) return true; // Yes, it's equal.
667
668 // Any other kind of (non-equivalent) redefinition is an error.
Reid Spencer61c83e02006-08-18 08:43:06 +0000669 GenerateError("Redefinition of type named '" + Name + "' in the '" +
Reid Spencer3271ed52004-07-18 00:08:11 +0000670 T->getDescription() + "' type plane!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000671 }
672
Reid Spencer75719bf2004-05-26 21:48:31 +0000673 return false;
674}
Chris Lattner8896eda2001-07-09 19:38:36 +0000675
Chris Lattner30c89792001-09-07 16:35:17 +0000676//===----------------------------------------------------------------------===//
677// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000678//
Chris Lattner8896eda2001-07-09 19:38:36 +0000679
Chris Lattner3b073862004-02-09 03:19:29 +0000680// TypeContains - Returns true if Ty directly contains E in it.
Chris Lattner30c89792001-09-07 16:35:17 +0000681//
682static bool TypeContains(const Type *Ty, const Type *E) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000683 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
Chris Lattner6d8dbec2004-12-08 16:13:53 +0000684 E) != Ty->subtype_end();
Chris Lattner3b073862004-02-09 03:19:29 +0000685}
686
687namespace {
688 struct UpRefRecord {
689 // NestingLevel - The number of nesting levels that need to be popped before
690 // this type is resolved.
691 unsigned NestingLevel;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000692
Chris Lattner3b073862004-02-09 03:19:29 +0000693 // LastContainedTy - This is the type at the current binding level for the
694 // type. Every time we reduce the nesting level, this gets updated.
695 const Type *LastContainedTy;
696
697 // UpRefTy - This is the actual opaque type that the upreference is
698 // represented with.
699 OpaqueType *UpRefTy;
700
701 UpRefRecord(unsigned NL, OpaqueType *URTy)
702 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
703 };
Chris Lattner30c89792001-09-07 16:35:17 +0000704}
Chris Lattner698b56e2001-07-20 19:15:08 +0000705
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000706// UpRefs - A list of the outstanding upreferences that need to be resolved.
Chris Lattner3b073862004-02-09 03:19:29 +0000707static std::vector<UpRefRecord> UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000708
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000709/// HandleUpRefs - Every time we finish a new layer of types, this function is
710/// called. It loops through the UpRefs vector, which is a list of the
711/// currently active types. For each type, if the up reference is contained in
712/// the newly completed type, we decrement the level count. When the level
713/// count reaches zero, the upreferenced type is the type that is passed in:
714/// thus we can complete the cycle.
715///
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000716static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner703e92f2006-08-18 17:34:24 +0000717 // If Ty isn't abstract, or if there are no up-references in it, then there is
718 // nothing to resolve here.
719 if (!ty->isAbstract() || UpRefs.empty()) return ty;
720
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000721 PATypeHolder Ty(ty);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000722 UR_OUT("Type '" << Ty->getDescription() <<
Chris Lattner5084d032001-11-02 07:46:26 +0000723 "' newly formed. Resolving upreferences.\n" <<
724 UpRefs.size() << " upreferences active!\n");
Chris Lattner026a8ce2004-02-09 18:53:54 +0000725
726 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
727 // to zero), we resolve them all together before we resolve them to Ty. At
728 // the end of the loop, if there is anything to resolve to Ty, it will be in
729 // this variable.
730 OpaqueType *TypeToResolve = 0;
731
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000732 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000733 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
734 << UpRefs[i].second->getDescription() << ") = "
Reid Spencer3271ed52004-07-18 00:08:11 +0000735 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner3b073862004-02-09 03:19:29 +0000736 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
737 // Decrement level of upreference
738 unsigned Level = --UpRefs[i].NestingLevel;
739 UpRefs[i].LastContainedTy = Ty;
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000740 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Misha Brukman5a2a3822005-05-10 22:02:28 +0000741 if (Level == 0) { // Upreference should be resolved!
Chris Lattner026a8ce2004-02-09 18:53:54 +0000742 if (!TypeToResolve) {
743 TypeToResolve = UpRefs[i].UpRefTy;
744 } else {
745 UR_OUT(" * Resolving upreference for "
746 << UpRefs[i].second->getDescription() << "\n";
747 std::string OldName = UpRefs[i].UpRefTy->getDescription());
748 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
749 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
750 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
751 }
Reid Spencer3271ed52004-07-18 00:08:11 +0000752 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000753 --i; // Do not skip the next element...
Chris Lattner30c89792001-09-07 16:35:17 +0000754 }
755 }
Chris Lattner8896eda2001-07-09 19:38:36 +0000756 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000757
Chris Lattner026a8ce2004-02-09 18:53:54 +0000758 if (TypeToResolve) {
759 UR_OUT(" * Resolving upreference for "
760 << UpRefs[i].second->getDescription() << "\n";
Chris Lattner16af11d2004-02-09 21:03:38 +0000761 std::string OldName = TypeToResolve->getDescription());
Chris Lattner026a8ce2004-02-09 18:53:54 +0000762 TypeToResolve->refineAbstractTypeTo(Ty);
763 }
764
Chris Lattner8896eda2001-07-09 19:38:36 +0000765 return Ty;
766}
767
Chris Lattner30c89792001-09-07 16:35:17 +0000768
Chris Lattner6184feb2005-05-20 03:25:47 +0000769// common code from the two 'RunVMAsmParser' functions
770 static Module * RunParser(Module * M) {
771
772 llvmAsmlineno = 1; // Reset the current line number...
Andrew Lenharth558bc882005-06-18 18:34:52 +0000773 ObsoleteVarArgs = false;
Andrew Lenharthe78f50d2005-06-19 03:53:56 +0000774 NewVarArgs = false;
Chris Lattner6184feb2005-05-20 03:25:47 +0000775
776 CurModule.CurrentModule = M;
777 yyparse(); // Parse the file, potentially throwing exception
Reid Spencer61c83e02006-08-18 08:43:06 +0000778 if (!ParserResult)
779 return 0;
Chris Lattner6184feb2005-05-20 03:25:47 +0000780
781 Module *Result = ParserResult;
782 ParserResult = 0;
783
Andrew Lenharth31c98bf2005-06-20 15:41:37 +0000784 //Not all functions use vaarg, so make a second check for ObsoleteVarArgs
785 {
786 Function* F;
787 if ((F = Result->getNamedFunction("llvm.va_start"))
788 && F->getFunctionType()->getNumParams() == 0)
789 ObsoleteVarArgs = true;
790 if((F = Result->getNamedFunction("llvm.va_copy"))
791 && F->getFunctionType()->getNumParams() == 1)
792 ObsoleteVarArgs = true;
793 }
794
Andrew Lenharthe78f50d2005-06-19 03:53:56 +0000795 if (ObsoleteVarArgs && NewVarArgs)
Reid Spencer61c83e02006-08-18 08:43:06 +0000796 GenerateError("This file is corrupt: it uses both new and old style varargs");
Andrew Lenharthe78f50d2005-06-19 03:53:56 +0000797
Andrew Lenharth558bc882005-06-18 18:34:52 +0000798 if(ObsoleteVarArgs) {
799 if(Function* F = Result->getNamedFunction("llvm.va_start")) {
Andrew Lenharth213e5572005-06-22 21:04:42 +0000800 if (F->arg_size() != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +0000801 GenerateError("Obsolete va_start takes 0 argument!");
Andrew Lenharth558bc882005-06-18 18:34:52 +0000802
803 //foo = va_start()
804 // ->
805 //bar = alloca typeof(foo)
806 //va_start(bar)
807 //foo = load bar
808
809 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
810 const Type* ArgTy = F->getFunctionType()->getReturnType();
811 const Type* ArgTyPtr = PointerType::get(ArgTy);
812 Function* NF = Result->getOrInsertFunction("llvm.va_start",
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000813 RetTy, ArgTyPtr, (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000814
815 while (!F->use_empty()) {
816 CallInst* CI = cast<CallInst>(F->use_back());
817 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vastart.fix.1", CI);
818 new CallInst(NF, bar, "", CI);
819 Value* foo = new LoadInst(bar, "vastart.fix.2", CI);
820 CI->replaceAllUsesWith(foo);
821 CI->getParent()->getInstList().erase(CI);
822 }
823 Result->getFunctionList().erase(F);
824 }
825
826 if(Function* F = Result->getNamedFunction("llvm.va_end")) {
Andrew Lenharth213e5572005-06-22 21:04:42 +0000827 if(F->arg_size() != 1)
Reid Spencer61c83e02006-08-18 08:43:06 +0000828 GenerateError("Obsolete va_end takes 1 argument!");
Andrew Lenharth213e5572005-06-22 21:04:42 +0000829
Andrew Lenharth558bc882005-06-18 18:34:52 +0000830 //vaend foo
831 // ->
832 //bar = alloca 1 of typeof(foo)
833 //vaend bar
834 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
835 const Type* ArgTy = F->getFunctionType()->getParamType(0);
836 const Type* ArgTyPtr = PointerType::get(ArgTy);
837 Function* NF = Result->getOrInsertFunction("llvm.va_end",
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000838 RetTy, ArgTyPtr, (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000839
840 while (!F->use_empty()) {
841 CallInst* CI = cast<CallInst>(F->use_back());
842 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vaend.fix.1", CI);
Andrew Lenharth017fba92005-06-19 14:04:55 +0000843 new StoreInst(CI->getOperand(1), bar, CI);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000844 new CallInst(NF, bar, "", CI);
845 CI->getParent()->getInstList().erase(CI);
846 }
847 Result->getFunctionList().erase(F);
848 }
849
850 if(Function* F = Result->getNamedFunction("llvm.va_copy")) {
Andrew Lenharth213e5572005-06-22 21:04:42 +0000851 if(F->arg_size() != 1)
Reid Spencer61c83e02006-08-18 08:43:06 +0000852 GenerateError("Obsolete va_copy takes 1 argument!");
Andrew Lenharth558bc882005-06-18 18:34:52 +0000853 //foo = vacopy(bar)
854 // ->
855 //a = alloca 1 of typeof(foo)
Andrew Lenharth213e5572005-06-22 21:04:42 +0000856 //b = alloca 1 of typeof(foo)
857 //store bar -> b
858 //vacopy(a, b)
Andrew Lenharth558bc882005-06-18 18:34:52 +0000859 //foo = load a
860
861 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
862 const Type* ArgTy = F->getFunctionType()->getReturnType();
863 const Type* ArgTyPtr = PointerType::get(ArgTy);
864 Function* NF = Result->getOrInsertFunction("llvm.va_copy",
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000865 RetTy, ArgTyPtr, ArgTyPtr,
866 (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000867
868 while (!F->use_empty()) {
869 CallInst* CI = cast<CallInst>(F->use_back());
870 AllocaInst* a = new AllocaInst(ArgTy, 0, "vacopy.fix.1", CI);
Andrew Lenharth213e5572005-06-22 21:04:42 +0000871 AllocaInst* b = new AllocaInst(ArgTy, 0, "vacopy.fix.2", CI);
872 new StoreInst(CI->getOperand(1), b, CI);
873 new CallInst(NF, a, b, "", CI);
874 Value* foo = new LoadInst(a, "vacopy.fix.3", CI);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000875 CI->replaceAllUsesWith(foo);
876 CI->getParent()->getInstList().erase(CI);
877 }
878 Result->getFunctionList().erase(F);
879 }
880 }
881
Chris Lattner6184feb2005-05-20 03:25:47 +0000882 return Result;
883
884 }
885
Chris Lattner00950542001-06-06 20:29:01 +0000886//===----------------------------------------------------------------------===//
887// RunVMAsmParser - Define an interface to this parser
888//===----------------------------------------------------------------------===//
889//
Chris Lattner86427632004-07-14 06:39:48 +0000890Module *llvm::RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner6184feb2005-05-20 03:25:47 +0000891 set_scan_file(F);
892
Chris Lattnera2850432001-07-22 18:36:00 +0000893 CurFilename = Filename;
Chris Lattner6184feb2005-05-20 03:25:47 +0000894 return RunParser(new Module(CurFilename));
895}
Chris Lattner00950542001-06-06 20:29:01 +0000896
Chris Lattner6184feb2005-05-20 03:25:47 +0000897Module *llvm::RunVMAsmParser(const char * AsmString, Module * M) {
898 set_scan_string(AsmString);
Chris Lattner42570982003-11-26 07:24:58 +0000899
Chris Lattner6184feb2005-05-20 03:25:47 +0000900 CurFilename = "from_memory";
901 if (M == NULL) {
902 return RunParser(new Module (CurFilename));
903 } else {
904 return RunParser(M);
905 }
Chris Lattner00950542001-06-06 20:29:01 +0000906}
907
908%}
909
910%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000911 llvm::Module *ModuleVal;
912 llvm::Function *FunctionVal;
913 std::pair<llvm::PATypeHolder*, char*> *ArgVal;
914 llvm::BasicBlock *BasicBlockVal;
915 llvm::TerminatorInst *TermInstVal;
916 llvm::Instruction *InstVal;
917 llvm::Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000918
Brian Gaeked0fde302003-11-11 22:41:34 +0000919 const llvm::Type *PrimType;
920 llvm::PATypeHolder *TypeVal;
921 llvm::Value *ValueVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000922
Brian Gaeked0fde302003-11-11 22:41:34 +0000923 std::vector<std::pair<llvm::PATypeHolder*,char*> > *ArgList;
924 std::vector<llvm::Value*> *ValueList;
925 std::list<llvm::PATypeHolder> *TypeList;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000926 // Represent the RHS of PHI node
Brian Gaeked0fde302003-11-11 22:41:34 +0000927 std::list<std::pair<llvm::Value*,
Misha Brukman5a2a3822005-05-10 22:02:28 +0000928 llvm::BasicBlock*> > *PHIList;
Brian Gaeked0fde302003-11-11 22:41:34 +0000929 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
930 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000931
Brian Gaeked0fde302003-11-11 22:41:34 +0000932 llvm::GlobalValue::LinkageTypes Linkage;
Chris Lattner30c89792001-09-07 16:35:17 +0000933 int64_t SInt64Val;
934 uint64_t UInt64Val;
935 int SIntVal;
936 unsigned UIntVal;
937 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000938 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000939
Chris Lattner30c89792001-09-07 16:35:17 +0000940 char *StrVal; // This memory is strdup'd!
Brian Gaeked0fde302003-11-11 22:41:34 +0000941 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000942
Brian Gaeked0fde302003-11-11 22:41:34 +0000943 llvm::Instruction::BinaryOps BinaryOpVal;
944 llvm::Instruction::TermOps TermOpVal;
945 llvm::Instruction::MemoryOps MemOpVal;
946 llvm::Instruction::OtherOps OtherOpVal;
947 llvm::Module::Endianness Endianness;
Chris Lattner00950542001-06-06 20:29:01 +0000948}
949
Chris Lattner79df7c02002-03-26 18:01:55 +0000950%type <ModuleVal> Module FunctionList
951%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000952%type <BasicBlockVal> BasicBlock InstructionList
953%type <TermInstVal> BBTerminatorInst
954%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000955%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000956%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000957%type <ArgList> ArgList ArgListH
958%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000959%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000960%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000961%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000962%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000963%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +0000964%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner15c9c032003-09-08 18:20:29 +0000965%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattnerddb6db42005-05-06 05:51:46 +0000966%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
Chris Lattneraa2c8532006-01-25 22:26:43 +0000967%type <BoolVal> OptSideEffect // 'sideeffect' or not.
Chris Lattner4ad02e72003-04-16 20:28:45 +0000968%type <Linkage> OptLinkage
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000969%type <Endianness> BigOrLittle
Chris Lattner00950542001-06-06 20:29:01 +0000970
Chris Lattner2079fde2001-10-13 06:41:08 +0000971// ValueRef - Unresolved reference to a definition or BB
972%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000973%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000974// Tokens and types for handling constant integer values
975//
976// ESINT64VAL - A negative number within long long range
977%token <SInt64Val> ESINT64VAL
978
979// EUINT64VAL - A positive number within uns. long long range
980%token <UInt64Val> EUINT64VAL
981%type <SInt64Val> EINT64VAL
982
983%token <SIntVal> SINTVAL // Signed 32 bit ints...
984%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
985%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000986%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000987
988// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000989%type <TypeVal> Types TypesV UpRTypes UpRTypesV
990%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000991%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
992%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000993
Chris Lattner6c23f572003-08-22 05:42:10 +0000994%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
995%type <StrVal> Name OptName OptAssign
Chris Lattner87ac9722005-11-06 06:46:28 +0000996%type <UIntVal> OptAlign OptCAlign
Chris Lattnerb2b96672005-11-12 18:21:21 +0000997%type <StrVal> OptSection SectionString
Chris Lattner00950542001-06-06 20:29:01 +0000998
Chris Lattner91ef4602004-03-31 03:49:47 +0000999%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Chris Lattner164c3782005-11-12 00:11:10 +00001000%token DECLARE GLOBAL CONSTANT SECTION VOLATILE
Chris Lattner16710e92004-10-16 18:17:13 +00001001%token TO DOTDOTDOT NULL_TOK UNDEF CONST INTERNAL LINKONCE WEAK APPENDING
Nate Begeman14b05292005-11-05 09:21:28 +00001002%token OPAQUE NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG ALIGN
Chris Lattneraa2c8532006-01-25 22:26:43 +00001003%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
Chris Lattner515906d2006-05-19 21:28:34 +00001004%token CC_TOK CCC_TOK CSRETCC_TOK FASTCC_TOK COLDCC_TOK
Chris Lattnera8e8f162005-05-06 20:27:19 +00001005%type <UIntVal> OptCallingConv
Chris Lattner00950542001-06-06 20:29:01 +00001006
Misha Brukman5a2a3822005-05-10 22:02:28 +00001007// Basic Block Terminating Operators
Chris Lattner16710e92004-10-16 18:17:13 +00001008%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
Chris Lattner00950542001-06-06 20:29:01 +00001009
Misha Brukman5a2a3822005-05-10 22:02:28 +00001010// Binary Operators
Chris Lattner4a6482b2002-09-10 19:57:26 +00001011%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattner42c9e772001-10-20 09:32:59 +00001012%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +00001013%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +00001014
1015// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001016%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +00001017
Chris Lattner027dcc52001-07-08 21:10:27 +00001018// Other Operators
1019%type <OtherOpVal> ShiftOps
Robert Bocchino2def1b32006-01-17 20:06:25 +00001020%token <OtherOpVal> PHI_TOK CAST SELECT SHL SHR VAARG
Chris Lattner4c949082006-04-08 01:18:35 +00001021%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Andrew Lenharth558bc882005-06-18 18:34:52 +00001022%token VAARG_old VANEXT_old //OBSOLETE
Chris Lattnerddb6db42005-05-06 05:51:46 +00001023
Chris Lattner027dcc52001-07-08 21:10:27 +00001024
Chris Lattner00950542001-06-06 20:29:01 +00001025%start Module
1026%%
1027
1028// Handle constant integer size restriction and conversion...
1029//
Chris Lattner51727be2002-06-04 21:58:56 +00001030INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +00001031INTVAL : UINTVAL {
1032 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
Reid Spencer61c83e02006-08-18 08:43:06 +00001033 GEN_ERROR("Value too large for type!");
Chris Lattner00950542001-06-06 20:29:01 +00001034 $$ = (int32_t)$1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001035 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001036};
Chris Lattner00950542001-06-06 20:29:01 +00001037
1038
Chris Lattner51727be2002-06-04 21:58:56 +00001039EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +00001040EINT64VAL : EUINT64VAL {
1041 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
Reid Spencer61c83e02006-08-18 08:43:06 +00001042 GEN_ERROR("Value too large for type!");
Chris Lattner00950542001-06-06 20:29:01 +00001043 $$ = (int64_t)$1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001044 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001045};
Chris Lattner00950542001-06-06 20:29:01 +00001046
Misha Brukman5a2a3822005-05-10 22:02:28 +00001047// Operations that are notably excluded from this list include:
Chris Lattner00950542001-06-06 20:29:01 +00001048// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1049//
Chris Lattner4a6482b2002-09-10 19:57:26 +00001050ArithmeticOps: ADD | SUB | MUL | DIV | REM;
1051LogicalOps : AND | OR | XOR;
1052SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
Chris Lattner4a6482b2002-09-10 19:57:26 +00001053
Chris Lattner51727be2002-06-04 21:58:56 +00001054ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +00001055
Chris Lattnere98dda62001-07-14 06:10:16 +00001056// These are some types that allow classification if we only want a particular
1057// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +00001058SIntType : LONG | INT | SHORT | SBYTE;
1059UIntType : ULONG | UINT | USHORT | UBYTE;
1060IntType : SIntType | UIntType;
1061FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +00001062
Chris Lattnere98dda62001-07-14 06:10:16 +00001063// OptAssign - Value producing statements have an optional assignment component
Chris Lattner6c23f572003-08-22 05:42:10 +00001064OptAssign : Name '=' {
Chris Lattner00950542001-06-06 20:29:01 +00001065 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001066 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001067 }
Misha Brukman5a2a3822005-05-10 22:02:28 +00001068 | /*empty*/ {
1069 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00001070 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001071 };
Chris Lattner00950542001-06-06 20:29:01 +00001072
Chris Lattner4ad02e72003-04-16 20:28:45 +00001073OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
1074 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
Chris Lattner72ac148d2003-10-16 18:29:00 +00001075 WEAK { $$ = GlobalValue::WeakLinkage; } |
Chris Lattner4ad02e72003-04-16 20:28:45 +00001076 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
1077 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattner30c89792001-09-07 16:35:17 +00001078
Chris Lattnera8e8f162005-05-06 20:27:19 +00001079OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
1080 CCC_TOK { $$ = CallingConv::C; } |
Chris Lattner515906d2006-05-19 21:28:34 +00001081 CSRETCC_TOK { $$ = CallingConv::CSRet; } |
Chris Lattnera8e8f162005-05-06 20:27:19 +00001082 FASTCC_TOK { $$ = CallingConv::Fast; } |
1083 COLDCC_TOK { $$ = CallingConv::Cold; } |
1084 CC_TOK EUINT64VAL {
1085 if ((unsigned)$2 != $2)
Reid Spencer61c83e02006-08-18 08:43:06 +00001086 GEN_ERROR("Calling conv too large!");
Chris Lattnera8e8f162005-05-06 20:27:19 +00001087 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001088 CHECK_FOR_ERROR
Chris Lattnera8e8f162005-05-06 20:27:19 +00001089 };
1090
Chris Lattner66db8e42005-11-06 06:34:12 +00001091// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1092// a comma before it.
Chris Lattner87ac9722005-11-06 06:46:28 +00001093OptAlign : /*empty*/ { $$ = 0; } |
Chris Lattnerb2b96672005-11-12 18:21:21 +00001094 ALIGN EUINT64VAL {
1095 $$ = $2;
1096 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencer61c83e02006-08-18 08:43:06 +00001097 GEN_ERROR("Alignment must be a power of two!");
1098 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001099};
Chris Lattner66db8e42005-11-06 06:34:12 +00001100OptCAlign : /*empty*/ { $$ = 0; } |
Chris Lattnerb2b96672005-11-12 18:21:21 +00001101 ',' ALIGN EUINT64VAL {
1102 $$ = $3;
1103 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencer61c83e02006-08-18 08:43:06 +00001104 GEN_ERROR("Alignment must be a power of two!");
1105 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001106};
1107
Chris Lattner66db8e42005-11-06 06:34:12 +00001108
Chris Lattner164c3782005-11-12 00:11:10 +00001109SectionString : SECTION STRINGCONSTANT {
1110 for (unsigned i = 0, e = strlen($2); i != e; ++i)
1111 if ($2[i] == '"' || $2[i] == '\\')
Reid Spencer61c83e02006-08-18 08:43:06 +00001112 GEN_ERROR("Invalid character in section name!");
Chris Lattner164c3782005-11-12 00:11:10 +00001113 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001114 CHECK_FOR_ERROR
Chris Lattner164c3782005-11-12 00:11:10 +00001115};
1116
1117OptSection : /*empty*/ { $$ = 0; } |
1118 SectionString { $$ = $1; };
Chris Lattner164c3782005-11-12 00:11:10 +00001119
Chris Lattnerb2b96672005-11-12 18:21:21 +00001120// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1121// is set to be the global we are processing.
1122//
1123GlobalVarAttributes : /* empty */ {} |
1124 ',' GlobalVarAttribute GlobalVarAttributes {};
1125GlobalVarAttribute : SectionString {
1126 CurGV->setSection($1);
1127 free($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001128 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001129 }
1130 | ALIGN EUINT64VAL {
1131 if ($2 != 0 && !isPowerOf2_32($2))
Reid Spencer61c83e02006-08-18 08:43:06 +00001132 GEN_ERROR("Alignment must be a power of two!");
Chris Lattnerb2b96672005-11-12 18:21:21 +00001133 CurGV->setAlignment($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001134 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001135 };
Chris Lattner164c3782005-11-12 00:11:10 +00001136
Chris Lattner30c89792001-09-07 16:35:17 +00001137//===----------------------------------------------------------------------===//
1138// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +00001139// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +00001140// access to it, a user must explicitly use TypesV.
1141//
1142
1143// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +00001144TypesV : Types | VOID { $$ = new PATypeHolder($1); };
1145UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +00001146
1147Types : UpRTypes {
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001148 if (!UpRefs.empty())
Reid Spencer61c83e02006-08-18 08:43:06 +00001149 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001150 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001151 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001152 };
Chris Lattner30c89792001-09-07 16:35:17 +00001153
1154
1155// Derived types are added later...
1156//
Chris Lattner51727be2002-06-04 21:58:56 +00001157PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
1158PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001159UpRTypes : OPAQUE {
1160 $$ = new PATypeHolder(OpaqueType::get());
Reid Spencer61c83e02006-08-18 08:43:06 +00001161 CHECK_FOR_ERROR
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001162 }
1163 | PrimType {
1164 $$ = new PATypeHolder($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001165 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001166 };
Chris Lattnerd78700d2002-08-16 21:14:40 +00001167UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001168 $$ = new PATypeHolder(getTypeVal($1));
Reid Spencer61c83e02006-08-18 08:43:06 +00001169 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001170};
Chris Lattner30c89792001-09-07 16:35:17 +00001171
Chris Lattner30c89792001-09-07 16:35:17 +00001172// Include derived types in the Types production.
1173//
1174UpRTypes : '\\' EUINT64VAL { // Type UpReference
Reid Spencer61c83e02006-08-18 08:43:06 +00001175 if ($2 > (uint64_t)~0U) GEN_ERROR("Value out of range!");
Chris Lattner30c89792001-09-07 16:35:17 +00001176 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3b073862004-02-09 03:19:29 +00001177 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001178 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +00001179 UR_OUT("New Upreference!\n");
Reid Spencer61c83e02006-08-18 08:43:06 +00001180 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001181 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001182 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner9232b992003-04-22 18:42:41 +00001183 std::vector<const Type*> Params;
Chris Lattnera08976b2005-02-22 23:13:58 +00001184 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1185 E = $3->end(); I != E; ++I)
1186 Params.push_back(*I);
Chris Lattner2079fde2001-10-13 06:41:08 +00001187 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1188 if (isVarArg) Params.pop_back();
1189
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001190 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +00001191 delete $3; // Delete the argument list
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001192 delete $1; // Delete the return type handle
Reid Spencer61c83e02006-08-18 08:43:06 +00001193 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001194 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001195 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001196 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001197 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00001198 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001199 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001200 | '<' EUINT64VAL 'x' UpRTypes '>' { // Packed array type?
1201 const llvm::Type* ElemTy = $4->get();
Chris Lattner9547d7f2005-11-10 01:42:43 +00001202 if ((unsigned)$2 != $2)
Reid Spencer61c83e02006-08-18 08:43:06 +00001203 GEN_ERROR("Unsigned result not equal to signed result");
Chris Lattner9547d7f2005-11-10 01:42:43 +00001204 if (!ElemTy->isPrimitiveType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001205 GEN_ERROR("Elemental type of a PackedType must be primitive");
Chris Lattner9547d7f2005-11-10 01:42:43 +00001206 if (!isPowerOf2_32($2))
Reid Spencer61c83e02006-08-18 08:43:06 +00001207 GEN_ERROR("Vector length should be a power of 2!");
Brian Gaeke715c90b2004-08-20 06:00:58 +00001208 $$ = new PATypeHolder(HandleUpRefs(PackedType::get(*$4, (unsigned)$2)));
1209 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00001210 CHECK_FOR_ERROR
Brian Gaeke715c90b2004-08-20 06:00:58 +00001211 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001212 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001213 std::vector<const Type*> Elements;
Chris Lattnera08976b2005-02-22 23:13:58 +00001214 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
1215 E = $2->end(); I != E; ++I)
1216 Elements.push_back(*I);
Misha Brukman5a2a3822005-05-10 22:02:28 +00001217
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001218 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001219 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001220 CHECK_FOR_ERROR
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001221 }
1222 | '{' '}' { // Empty structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001223 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Reid Spencer61c83e02006-08-18 08:43:06 +00001224 CHECK_FOR_ERROR
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001225 }
1226 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001227 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001228 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001229 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001230 };
Chris Lattner30c89792001-09-07 16:35:17 +00001231
Chris Lattner7e708292002-06-25 16:13:24 +00001232// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +00001233// declaration type lists
1234//
1235TypeListI : UpRTypes {
Chris Lattner9232b992003-04-22 18:42:41 +00001236 $$ = new std::list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +00001237 $$->push_back(*$1); delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001238 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001239 }
1240 | TypeListI ',' UpRTypes {
1241 ($$=$1)->push_back(*$3); delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001242 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001243 };
Chris Lattner30c89792001-09-07 16:35:17 +00001244
Chris Lattner7e708292002-06-25 16:13:24 +00001245// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +00001246ArgTypeListI : TypeListI
1247 | TypeListI ',' DOTDOTDOT {
1248 ($$=$1)->push_back(Type::VoidTy);
Reid Spencer61c83e02006-08-18 08:43:06 +00001249 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001250 }
1251 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001252 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
Reid Spencer61c83e02006-08-18 08:43:06 +00001253 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001254 }
1255 | /*empty*/ {
Chris Lattner9232b992003-04-22 18:42:41 +00001256 $$ = new std::list<PATypeHolder>();
Reid Spencer61c83e02006-08-18 08:43:06 +00001257 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001258 };
Chris Lattner30c89792001-09-07 16:35:17 +00001259
Chris Lattnere98dda62001-07-14 06:10:16 +00001260// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +00001261// production is used ONLY to represent constants that show up AFTER a 'const',
1262// 'constant' or 'global' token at global scope. Constants that can be inlined
1263// into other expressions (such as integers and constexprs) are handled by the
1264// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +00001265//
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001266ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Chris Lattner949a3622003-07-23 15:30:06 +00001267 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001268 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001269 GEN_ERROR("Cannot make array constant with type: '" +
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001270 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001271 const Type *ETy = ATy->getElementType();
1272 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +00001273
Chris Lattner30c89792001-09-07 16:35:17 +00001274 // Verify that we have the correct size...
1275 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001276 GEN_ERROR("Type mismatch: constant sized array initialized with " +
Reid Spencer3271ed52004-07-18 00:08:11 +00001277 utostr($3->size()) + " arguments, but has size of " +
1278 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +00001279
Chris Lattner30c89792001-09-07 16:35:17 +00001280 // Verify all elements are correct type!
1281 for (unsigned i = 0; i < $3->size(); i++) {
1282 if (ETy != (*$3)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001283 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Reid Spencer3271ed52004-07-18 00:08:11 +00001284 ETy->getDescription() +"' as required!\nIt is of type '"+
1285 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +00001286 }
1287
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001288 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001289 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001290 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001291 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001292 | Types '[' ']' {
Chris Lattner949a3622003-07-23 15:30:06 +00001293 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001294 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001295 GEN_ERROR("Cannot make array constant with type: '" +
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001296 (*$1)->getDescription() + "'!");
1297
1298 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +00001299 if (NumElements != -1 && NumElements != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001300 GEN_ERROR("Type mismatch: constant sized array initialized with 0"
Reid Spencer3271ed52004-07-18 00:08:11 +00001301 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattner9232b992003-04-22 18:42:41 +00001302 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +00001303 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001304 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001305 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001306 | Types 'c' STRINGCONSTANT {
Chris Lattner949a3622003-07-23 15:30:06 +00001307 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001308 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001309 GEN_ERROR("Cannot make array constant with type: '" +
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001310 (*$1)->getDescription() + "'!");
1311
Chris Lattner30c89792001-09-07 16:35:17 +00001312 int NumElements = ATy->getNumElements();
1313 const Type *ETy = ATy->getElementType();
1314 char *EndStr = UnEscapeLexed($3, true);
1315 if (NumElements != -1 && NumElements != (EndStr-$3))
Reid Spencer61c83e02006-08-18 08:43:06 +00001316 GEN_ERROR("Can't build string constant of size " +
Reid Spencer3271ed52004-07-18 00:08:11 +00001317 itostr((int)(EndStr-$3)) +
1318 " when array has size " + itostr(NumElements) + "!");
Chris Lattner9232b992003-04-22 18:42:41 +00001319 std::vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +00001320 if (ETy == Type::SByteTy) {
Chris Lattneree454772006-01-23 23:05:15 +00001321 for (signed char *C = (signed char *)$3; C != (signed char *)EndStr; ++C)
Reid Spencer3271ed52004-07-18 00:08:11 +00001322 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +00001323 } else if (ETy == Type::UByteTy) {
Chris Lattneree454772006-01-23 23:05:15 +00001324 for (unsigned char *C = (unsigned char *)$3;
1325 C != (unsigned char*)EndStr; ++C)
1326 Vals.push_back(ConstantUInt::get(ETy, *C));
Chris Lattner93750fa2001-07-28 17:48:55 +00001327 } else {
Chris Lattner30c89792001-09-07 16:35:17 +00001328 free($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00001329 GEN_ERROR("Cannot build string arrays of non byte sized elements!");
Chris Lattner93750fa2001-07-28 17:48:55 +00001330 }
Chris Lattner30c89792001-09-07 16:35:17 +00001331 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001332 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +00001333 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001334 CHECK_FOR_ERROR
Chris Lattner93750fa2001-07-28 17:48:55 +00001335 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001336 | Types '<' ConstVector '>' { // Nonempty unsized arr
1337 const PackedType *PTy = dyn_cast<PackedType>($1->get());
1338 if (PTy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001339 GEN_ERROR("Cannot make packed constant with type: '" +
Brian Gaeke715c90b2004-08-20 06:00:58 +00001340 (*$1)->getDescription() + "'!");
1341 const Type *ETy = PTy->getElementType();
1342 int NumElements = PTy->getNumElements();
1343
1344 // Verify that we have the correct size...
1345 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001346 GEN_ERROR("Type mismatch: constant sized packed initialized with " +
Brian Gaeke715c90b2004-08-20 06:00:58 +00001347 utostr($3->size()) + " arguments, but has size of " +
1348 itostr(NumElements) + "!");
1349
1350 // Verify all elements are correct type!
1351 for (unsigned i = 0; i < $3->size(); i++) {
1352 if (ETy != (*$3)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001353 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Brian Gaeke715c90b2004-08-20 06:00:58 +00001354 ETy->getDescription() +"' as required!\nIt is of type '"+
1355 (*$3)[i]->getType()->getDescription() + "'.");
1356 }
1357
1358 $$ = ConstantPacked::get(PTy, *$3);
1359 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001360 CHECK_FOR_ERROR
Brian Gaeke715c90b2004-08-20 06:00:58 +00001361 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001362 | Types '{' ConstVector '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001363 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001364 if (STy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001365 GEN_ERROR("Cannot make struct constant with type: '" +
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001366 (*$1)->getDescription() + "'!");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001367
Chris Lattnerc6212f12003-05-21 16:06:56 +00001368 if ($3->size() != STy->getNumContainedTypes())
Reid Spencer61c83e02006-08-18 08:43:06 +00001369 GEN_ERROR("Illegal number of initializers for structure type!");
Chris Lattnerc6212f12003-05-21 16:06:56 +00001370
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001371 // Check to ensure that constants are compatible with the type initializer!
1372 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Chris Lattnerd21cd802004-02-09 04:37:31 +00001373 if ((*$3)[i]->getType() != STy->getElementType(i))
Reid Spencer61c83e02006-08-18 08:43:06 +00001374 GEN_ERROR("Expected type '" +
Chris Lattnerd21cd802004-02-09 04:37:31 +00001375 STy->getElementType(i)->getDescription() +
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001376 "' for element #" + utostr(i) +
1377 " of structure initializer!");
1378
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001379 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001380 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001381 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001382 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001383 | Types '{' '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001384 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001385 if (STy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001386 GEN_ERROR("Cannot make struct constant with type: '" +
Chris Lattnerc6212f12003-05-21 16:06:56 +00001387 (*$1)->getDescription() + "'!");
1388
1389 if (STy->getNumContainedTypes() != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001390 GEN_ERROR("Illegal number of initializers for structure type!");
Chris Lattnerc6212f12003-05-21 16:06:56 +00001391
1392 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1393 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001394 CHECK_FOR_ERROR
Chris Lattnerc6212f12003-05-21 16:06:56 +00001395 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001396 | Types NULL_TOK {
Chris Lattner949a3622003-07-23 15:30:06 +00001397 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001398 if (PTy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001399 GEN_ERROR("Cannot make null pointer constant with type: '" +
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001400 (*$1)->getDescription() + "'!");
1401
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001402 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001403 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001404 CHECK_FOR_ERROR
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001405 }
Chris Lattner16710e92004-10-16 18:17:13 +00001406 | Types UNDEF {
1407 $$ = UndefValue::get($1->get());
1408 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001409 CHECK_FOR_ERROR
Chris Lattner16710e92004-10-16 18:17:13 +00001410 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001411 | Types SymbolicValueRef {
Chris Lattner949a3622003-07-23 15:30:06 +00001412 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001413 if (Ty == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001414 GEN_ERROR("Global const reference must be a pointer type!");
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001415
Chris Lattner3101c252002-08-15 17:58:33 +00001416 // ConstExprs can exist in the body of a function, thus creating
Reid Spencer3271ed52004-07-18 00:08:11 +00001417 // GlobalValues whenever they refer to a variable. Because we are in
Chris Lattner3101c252002-08-15 17:58:33 +00001418 // the context of a function, getValNonImprovising will search the functions
1419 // symbol table instead of the module symbol table for the global symbol,
1420 // which throws things all off. To get around this, we just tell
1421 // getValNonImprovising that we are at global scope here.
1422 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001423 Function *SavedCurFn = CurFun.CurrentFunction;
1424 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001425
Chris Lattner2079fde2001-10-13 06:41:08 +00001426 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001427
Chris Lattner394f2eb2003-10-16 20:12:13 +00001428 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001429
Chris Lattner2079fde2001-10-13 06:41:08 +00001430 // If this is an initializer for a constant pointer, which is referencing a
1431 // (currently) undefined variable, create a stub now that shall be replaced
1432 // in the future with the right type of variable.
1433 //
1434 if (V == 0) {
1435 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1436 const PointerType *PT = cast<PointerType>(Ty);
1437
1438 // First check to see if the forward references value is already created!
1439 PerModuleInfo::GlobalRefsType::iterator I =
Reid Spencer3271ed52004-07-18 00:08:11 +00001440 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001441
1442 if (I != CurModule.GlobalRefs.end()) {
Reid Spencer3271ed52004-07-18 00:08:11 +00001443 V = I->second; // Placeholder already exists, use it...
Chris Lattnera989b232003-12-23 20:05:15 +00001444 $2.destroy();
Chris Lattner2079fde2001-10-13 06:41:08 +00001445 } else {
Chris Lattner8a327842004-07-14 21:44:00 +00001446 std::string Name;
Chris Lattnera09000d2004-07-14 23:03:46 +00001447 if ($2.Type == ValID::NameVal) Name = $2.Name;
Chris Lattner8a327842004-07-14 21:44:00 +00001448
Reid Spencer3271ed52004-07-18 00:08:11 +00001449 // Create the forward referenced global.
Chris Lattnera09000d2004-07-14 23:03:46 +00001450 GlobalValue *GV;
1451 if (const FunctionType *FTy =
1452 dyn_cast<FunctionType>(PT->getElementType())) {
1453 GV = new Function(FTy, GlobalValue::ExternalLinkage, Name,
1454 CurModule.CurrentModule);
1455 } else {
Reid Spencer3271ed52004-07-18 00:08:11 +00001456 GV = new GlobalVariable(PT->getElementType(), false,
Chris Lattnera09000d2004-07-14 23:03:46 +00001457 GlobalValue::ExternalLinkage, 0,
1458 Name, CurModule.CurrentModule);
1459 }
Chris Lattner8a327842004-07-14 21:44:00 +00001460
Reid Spencer3271ed52004-07-18 00:08:11 +00001461 // Keep track of the fact that we have a forward ref to recycle it
1462 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1463 V = GV;
Chris Lattner2079fde2001-10-13 06:41:08 +00001464 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001465 }
1466
Reid Spencer3271ed52004-07-18 00:08:11 +00001467 $$ = cast<GlobalValue>(V);
Chris Lattner2079fde2001-10-13 06:41:08 +00001468 delete $1; // Free the type handle
Reid Spencer61c83e02006-08-18 08:43:06 +00001469 CHECK_FOR_ERROR
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001470 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001471 | Types ConstExpr {
1472 if ($1->get() != $2->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001473 GEN_ERROR("Mismatched types for constant expression!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001474 $$ = $2;
1475 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001476 CHECK_FOR_ERROR
Chris Lattnerf4600482003-06-28 20:01:34 +00001477 }
1478 | Types ZEROINITIALIZER {
Chris Lattner78915932004-11-28 16:45:45 +00001479 const Type *Ty = $1->get();
1480 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
Reid Spencer61c83e02006-08-18 08:43:06 +00001481 GEN_ERROR("Cannot create a null initialized value of this type!");
Chris Lattner78915932004-11-28 16:45:45 +00001482 $$ = Constant::getNullValue(Ty);
Chris Lattnerf4600482003-06-28 20:01:34 +00001483 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001484 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001485 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001486
Chris Lattnere43f40b2002-10-09 00:25:32 +00001487ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001488 if (!ConstantSInt::isValueValidForType($1, $2))
Reid Spencer61c83e02006-08-18 08:43:06 +00001489 GEN_ERROR("Constant value doesn't fit in type!");
Chris Lattnerd05e3592002-08-15 18:17:28 +00001490 $$ = ConstantSInt::get($1, $2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001491 CHECK_FOR_ERROR
Chris Lattnere43f40b2002-10-09 00:25:32 +00001492 }
1493 | UIntType EUINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001494 if (!ConstantUInt::isValueValidForType($1, $2))
Reid Spencer61c83e02006-08-18 08:43:06 +00001495 GEN_ERROR("Constant value doesn't fit in type!");
Chris Lattnerd05e3592002-08-15 18:17:28 +00001496 $$ = ConstantUInt::get($1, $2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001497 CHECK_FOR_ERROR
Chris Lattnere43f40b2002-10-09 00:25:32 +00001498 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001499 | BOOL TRUETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001500 $$ = ConstantBool::True;
Reid Spencer61c83e02006-08-18 08:43:06 +00001501 CHECK_FOR_ERROR
Chris Lattnerd05e3592002-08-15 18:17:28 +00001502 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001503 | BOOL FALSETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001504 $$ = ConstantBool::False;
Reid Spencer61c83e02006-08-18 08:43:06 +00001505 CHECK_FOR_ERROR
Chris Lattnerd05e3592002-08-15 18:17:28 +00001506 }
1507 | FPType FPVAL { // Float & Double constants
Reid Spencer9f9b3ac2004-12-06 22:18:25 +00001508 if (!ConstantFP::isValueValidForType($1, $2))
Reid Spencer61c83e02006-08-18 08:43:06 +00001509 GEN_ERROR("Floating point constant invalid for type!!");
Chris Lattnerd05e3592002-08-15 18:17:28 +00001510 $$ = ConstantFP::get($1, $2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001511 CHECK_FOR_ERROR
Chris Lattnerd05e3592002-08-15 18:17:28 +00001512 };
1513
Chris Lattner00950542001-06-06 20:29:01 +00001514
Chris Lattnerd78700d2002-08-16 21:14:40 +00001515ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattnerae711a82003-11-21 20:27:35 +00001516 if (!$3->getType()->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001517 GEN_ERROR("cast constant expression from a non-primitive type: '" +
Chris Lattnerae711a82003-11-21 20:27:35 +00001518 $3->getType()->getDescription() + "'!");
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001519 if (!$5->get()->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001520 GEN_ERROR("cast constant expression to a non-primitive type: '" +
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001521 $5->get()->getDescription() + "'!");
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001522 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001523 delete $5;
Reid Spencer61c83e02006-08-18 08:43:06 +00001524 CHECK_FOR_ERROR
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001525 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001526 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1527 if (!isa<PointerType>($3->getType()))
Reid Spencer61c83e02006-08-18 08:43:06 +00001528 GEN_ERROR("GetElementPtr requires a pointer operand!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001529
Chris Lattner830b6f92004-04-05 01:30:04 +00001530 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
1531 // indices to uint struct indices for compatibility.
1532 generic_gep_type_iterator<std::vector<Value*>::iterator>
1533 GTI = gep_type_begin($3->getType(), $4->begin(), $4->end()),
1534 GTE = gep_type_end($3->getType(), $4->begin(), $4->end());
1535 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
1536 if (isa<StructType>(*GTI)) // Only change struct indices
1537 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
1538 if (CUI->getType() == Type::UByteTy)
1539 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
1540
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001541 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001542 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001543 if (!IdxTy)
Reid Spencer61c83e02006-08-18 08:43:06 +00001544 GEN_ERROR("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001545
Chris Lattner9232b992003-04-22 18:42:41 +00001546 std::vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001547 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1548 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001549 IdxVec.push_back(C);
1550 else
Reid Spencer61c83e02006-08-18 08:43:06 +00001551 GEN_ERROR("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001552
Chris Lattnerd78700d2002-08-16 21:14:40 +00001553 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001554
Chris Lattnerd78700d2002-08-16 21:14:40 +00001555 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Reid Spencer61c83e02006-08-18 08:43:06 +00001556 CHECK_FOR_ERROR
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001557 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001558 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1559 if ($3->getType() != Type::BoolTy)
Reid Spencer61c83e02006-08-18 08:43:06 +00001560 GEN_ERROR("Select condition must be of boolean type!");
Chris Lattner76f0ff32004-03-12 05:51:36 +00001561 if ($5->getType() != $7->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001562 GEN_ERROR("Select operand types must match!");
Chris Lattner76f0ff32004-03-12 05:51:36 +00001563 $$ = ConstantExpr::getSelect($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001564 CHECK_FOR_ERROR
Chris Lattner76f0ff32004-03-12 05:51:36 +00001565 }
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001566 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001567 if ($3->getType() != $5->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001568 GEN_ERROR("Binary operator types must match!");
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001569 // HACK: llvm 1.3 and earlier used to emit invalid pointer constant exprs.
1570 // To retain backward compatibility with these early compilers, we emit a
1571 // cast to the appropriate integer type automatically if we are in the
1572 // broken case. See PR424 for more information.
1573 if (!isa<PointerType>($3->getType())) {
1574 $$ = ConstantExpr::get($1, $3, $5);
1575 } else {
Chris Lattner42db1a02004-08-20 18:07:39 +00001576 const Type *IntPtrTy = 0;
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001577 switch (CurModule.CurrentModule->getPointerSize()) {
1578 case Module::Pointer32: IntPtrTy = Type::IntTy; break;
1579 case Module::Pointer64: IntPtrTy = Type::LongTy; break;
Reid Spencer61c83e02006-08-18 08:43:06 +00001580 default: GEN_ERROR("invalid pointer binary constant expr!");
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001581 }
1582 $$ = ConstantExpr::get($1, ConstantExpr::getCast($3, IntPtrTy),
1583 ConstantExpr::getCast($5, IntPtrTy));
1584 $$ = ConstantExpr::getCast($$, $3->getType());
1585 }
Reid Spencer61c83e02006-08-18 08:43:06 +00001586 CHECK_FOR_ERROR
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001587 }
1588 | LogicalOps '(' ConstVal ',' ConstVal ')' {
1589 if ($3->getType() != $5->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001590 GEN_ERROR("Logical operator types must match!");
Chris Lattner0a017832005-12-21 18:31:29 +00001591 if (!$3->getType()->isIntegral()) {
1592 if (!isa<PackedType>($3->getType()) ||
1593 !cast<PackedType>($3->getType())->getElementType()->isIntegral())
Reid Spencer61c83e02006-08-18 08:43:06 +00001594 GEN_ERROR("Logical operator requires integral operands!");
Chris Lattner0a017832005-12-21 18:31:29 +00001595 }
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001596 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001597 CHECK_FOR_ERROR
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001598 }
1599 | SetCondOps '(' ConstVal ',' ConstVal ')' {
1600 if ($3->getType() != $5->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001601 GEN_ERROR("setcc operand types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001602 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001603 CHECK_FOR_ERROR
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001604 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001605 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001606 if ($5->getType() != Type::UByteTy)
Reid Spencer61c83e02006-08-18 08:43:06 +00001607 GEN_ERROR("Shift count for shift constant must be unsigned byte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001608 if (!$3->getType()->isInteger())
Reid Spencer61c83e02006-08-18 08:43:06 +00001609 GEN_ERROR("Shift constant expression requires integer operand!");
Chris Lattnerb16689b2004-01-12 19:08:43 +00001610 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001611 CHECK_FOR_ERROR
Robert Bocchino9c62b562006-01-10 19:04:32 +00001612 }
1613 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Chris Lattner2d7349a2006-04-08 04:08:32 +00001614 if (!ExtractElementInst::isValidOperands($3, $5))
Reid Spencer61c83e02006-08-18 08:43:06 +00001615 GEN_ERROR("Invalid extractelement operands!");
Robert Bocchino9c62b562006-01-10 19:04:32 +00001616 $$ = ConstantExpr::getExtractElement($3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001617 CHECK_FOR_ERROR
Chris Lattnerca73cd82006-04-08 03:53:34 +00001618 }
1619 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Chris Lattner2d7349a2006-04-08 04:08:32 +00001620 if (!InsertElementInst::isValidOperands($3, $5, $7))
Reid Spencer61c83e02006-08-18 08:43:06 +00001621 GEN_ERROR("Invalid insertelement operands!");
Chris Lattnerca73cd82006-04-08 03:53:34 +00001622 $$ = ConstantExpr::getInsertElement($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001623 CHECK_FOR_ERROR
Chris Lattnerca73cd82006-04-08 03:53:34 +00001624 }
1625 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1626 if (!ShuffleVectorInst::isValidOperands($3, $5, $7))
Reid Spencer61c83e02006-08-18 08:43:06 +00001627 GEN_ERROR("Invalid shufflevector operands!");
Chris Lattnerca73cd82006-04-08 03:53:34 +00001628 $$ = ConstantExpr::getShuffleVector($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001629 CHECK_FOR_ERROR
Chris Lattner699f1eb2002-08-14 17:12:33 +00001630 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001631
Chris Lattnerca73cd82006-04-08 03:53:34 +00001632
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001633// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001634ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001635 ($$ = $1)->push_back($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00001636 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001637 }
1638 | ConstVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001639 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001640 $$->push_back($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001641 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001642 };
Chris Lattner00950542001-06-06 20:29:01 +00001643
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001644
Chris Lattner1781aca2001-09-18 04:00:54 +00001645// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001646GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001647
Chris Lattner00950542001-06-06 20:29:01 +00001648
Chris Lattner0e73ce62002-05-02 19:11:13 +00001649//===----------------------------------------------------------------------===//
1650// Rules to match Modules
1651//===----------------------------------------------------------------------===//
1652
1653// Module rule: Capture the result of parsing the whole file into a result
1654// variable...
1655//
1656Module : FunctionList {
1657 $$ = ParserResult = $1;
1658 CurModule.ModuleDone();
Reid Spencer61c83e02006-08-18 08:43:06 +00001659 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001660};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001661
Chris Lattner7e708292002-06-25 16:13:24 +00001662// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001663//
1664FunctionList : FunctionList Function {
1665 $$ = $1;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001666 CurFun.FunctionDone();
Reid Spencer61c83e02006-08-18 08:43:06 +00001667 CHECK_FOR_ERROR
Chris Lattner0e73ce62002-05-02 19:11:13 +00001668 }
1669 | FunctionList FunctionProto {
1670 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001671 CHECK_FOR_ERROR
Chris Lattner0e73ce62002-05-02 19:11:13 +00001672 }
Chris Lattner71cdba32006-01-24 00:40:17 +00001673 | FunctionList MODULE ASM_TOK AsmBlock {
Chris Lattneree454772006-01-23 23:05:15 +00001674 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001675 CHECK_FOR_ERROR
Chris Lattneree454772006-01-23 23:05:15 +00001676 }
Chris Lattner0e73ce62002-05-02 19:11:13 +00001677 | FunctionList IMPLEMENTATION {
1678 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001679 CHECK_FOR_ERROR
Chris Lattner0e73ce62002-05-02 19:11:13 +00001680 }
1681 | ConstPool {
1682 $$ = CurModule.CurrentModule;
Chris Lattner355ad1f2005-03-21 06:27:42 +00001683 // Emit an error if there are any unresolved types left.
1684 if (!CurModule.LateResolveTypes.empty()) {
1685 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
Reid Spencer61c83e02006-08-18 08:43:06 +00001686 if (DID.Type == ValID::NameVal) {
1687 GEN_ERROR("Reference to an undefined type: '"+DID.getName() + "'");
1688 } else {
1689 GEN_ERROR("Reference to an undefined type: #" + itostr(DID.Num));
1690 }
Chris Lattner355ad1f2005-03-21 06:27:42 +00001691 }
Reid Spencer61c83e02006-08-18 08:43:06 +00001692 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001693 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001694
Chris Lattnere98dda62001-07-14 06:10:16 +00001695// ConstPool - Constants with optional names assigned to them.
Chris Lattner355ad1f2005-03-21 06:27:42 +00001696ConstPool : ConstPool OptAssign TYPE TypesV {
Chris Lattner4a42e902001-10-22 05:56:09 +00001697 // Eagerly resolve types. This is not an optimization, this is a
1698 // requirement that is due to the fact that we could have this:
1699 //
1700 // %list = type { %list * }
1701 // %list = type { %list * } ; repeated type decl
1702 //
1703 // If types are not resolved eagerly, then the two types will not be
1704 // determined to be the same type!
1705 //
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001706 ResolveTypeTo($2, *$4);
Chris Lattner4a42e902001-10-22 05:56:09 +00001707
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001708 if (!setTypeName(*$4, $2) && !$2) {
1709 // If this is a named type that is not a redefinition, add it to the slot
1710 // table.
Chris Lattner355ad1f2005-03-21 06:27:42 +00001711 CurModule.Types.push_back(*$4);
Chris Lattner30c89792001-09-07 16:35:17 +00001712 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001713
1714 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00001715 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001716 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001717 | ConstPool FunctionProto { // Function prototypes can be in const pool
Reid Spencer61c83e02006-08-18 08:43:06 +00001718 CHECK_FOR_ERROR
Chris Lattner93750fa2001-07-28 17:48:55 +00001719 }
Chris Lattner71cdba32006-01-24 00:40:17 +00001720 | ConstPool MODULE ASM_TOK AsmBlock { // Asm blocks can be in the const pool
Reid Spencer61c83e02006-08-18 08:43:06 +00001721 CHECK_FOR_ERROR
Chris Lattneree454772006-01-23 23:05:15 +00001722 }
Chris Lattnerb2b96672005-11-12 18:21:21 +00001723 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Reid Spencer61c83e02006-08-18 08:43:06 +00001724 if ($5 == 0) GEN_ERROR("Global value initializer is not a constant!");
Chris Lattnerb2b96672005-11-12 18:21:21 +00001725 CurGV = ParseGlobalVariable($2, $3, $4, $5->getType(), $5);
1726 } GlobalVarAttributes {
1727 CurGV = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00001728 CHECK_FOR_ERROR
Chris Lattner1781aca2001-09-18 04:00:54 +00001729 }
Chris Lattnerb2b96672005-11-12 18:21:21 +00001730 | ConstPool OptAssign EXTERNAL GlobalType Types {
1731 CurGV = ParseGlobalVariable($2, GlobalValue::ExternalLinkage,
1732 $4, *$5, 0);
Chris Lattner1f862af2003-04-16 18:13:57 +00001733 delete $5;
Chris Lattnerb2b96672005-11-12 18:21:21 +00001734 } GlobalVarAttributes {
1735 CurGV = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00001736 CHECK_FOR_ERROR
Chris Lattnere98dda62001-07-14 06:10:16 +00001737 }
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001738 | ConstPool TARGET TargetDefinition {
Reid Spencer61c83e02006-08-18 08:43:06 +00001739 CHECK_FOR_ERROR
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001740 }
Reid Spencer0be13e72004-07-25 17:58:28 +00001741 | ConstPool DEPLIBS '=' LibrariesDefinition {
Reid Spencer61c83e02006-08-18 08:43:06 +00001742 CHECK_FOR_ERROR
Reid Spencer0be13e72004-07-25 17:58:28 +00001743 }
Chris Lattner00950542001-06-06 20:29:01 +00001744 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001745 };
Chris Lattner00950542001-06-06 20:29:01 +00001746
1747
Chris Lattneree454772006-01-23 23:05:15 +00001748AsmBlock : STRINGCONSTANT {
Chris Lattner66316012006-01-24 04:14:29 +00001749 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
Chris Lattneree454772006-01-23 23:05:15 +00001750 char *EndStr = UnEscapeLexed($1, true);
1751 std::string NewAsm($1, EndStr);
1752 free($1);
1753
1754 if (AsmSoFar.empty())
Chris Lattner66316012006-01-24 04:14:29 +00001755 CurModule.CurrentModule->setModuleInlineAsm(NewAsm);
Chris Lattneree454772006-01-23 23:05:15 +00001756 else
Chris Lattner66316012006-01-24 04:14:29 +00001757 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+NewAsm);
Reid Spencer61c83e02006-08-18 08:43:06 +00001758 CHECK_FOR_ERROR
Chris Lattneree454772006-01-23 23:05:15 +00001759};
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001760
1761BigOrLittle : BIG { $$ = Module::BigEndian; };
1762BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1763
1764TargetDefinition : ENDIAN '=' BigOrLittle {
1765 CurModule.CurrentModule->setEndianness($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00001766 CHECK_FOR_ERROR
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001767 }
1768 | POINTERSIZE '=' EUINT64VAL {
1769 if ($3 == 32)
1770 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1771 else if ($3 == 64)
1772 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1773 else
Reid Spencer61c83e02006-08-18 08:43:06 +00001774 GEN_ERROR("Invalid pointer size: '" + utostr($3) + "'!");
1775 CHECK_FOR_ERROR
Reid Spencer0be13e72004-07-25 17:58:28 +00001776 }
1777 | TRIPLE '=' STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001778 CurModule.CurrentModule->setTargetTriple($3);
1779 free($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00001780 CHECK_FOR_ERROR
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001781 };
1782
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001783LibrariesDefinition : '[' LibList ']';
Reid Spencer0be13e72004-07-25 17:58:28 +00001784
1785LibList : LibList ',' STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001786 CurModule.CurrentModule->addLibrary($3);
1787 free($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00001788 CHECK_FOR_ERROR
Reid Spencer0be13e72004-07-25 17:58:28 +00001789 }
1790 | STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001791 CurModule.CurrentModule->addLibrary($1);
1792 free($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001793 CHECK_FOR_ERROR
Reid Spencer0be13e72004-07-25 17:58:28 +00001794 }
1795 | /* empty: end of list */ {
Reid Spencer61c83e02006-08-18 08:43:06 +00001796 CHECK_FOR_ERROR
Reid Spencer0be13e72004-07-25 17:58:28 +00001797 }
1798 ;
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001799
Chris Lattner00950542001-06-06 20:29:01 +00001800//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001801// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001802//===----------------------------------------------------------------------===//
1803
Chris Lattner6c23f572003-08-22 05:42:10 +00001804Name : VAR_ID | STRINGCONSTANT;
1805OptName : Name | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001806
Chris Lattner6c23f572003-08-22 05:42:10 +00001807ArgVal : Types OptName {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001808 if (*$1 == Type::VoidTy)
Reid Spencer61c83e02006-08-18 08:43:06 +00001809 GEN_ERROR("void typed arguments are invalid!");
Chris Lattner9232b992003-04-22 18:42:41 +00001810 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001811 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001812};
Chris Lattner00950542001-06-06 20:29:01 +00001813
Chris Lattner69da5cf2002-10-13 20:57:00 +00001814ArgListH : ArgListH ',' ArgVal {
1815 $$ = $1;
1816 $1->push_back(*$3);
1817 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001818 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001819 }
1820 | ArgVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001821 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
Chris Lattner69da5cf2002-10-13 20:57:00 +00001822 $$->push_back(*$1);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001823 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001824 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001825 };
Chris Lattner00950542001-06-06 20:29:01 +00001826
1827ArgList : ArgListH {
1828 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001829 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001830 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001831 | ArgListH ',' DOTDOTDOT {
1832 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001833 $$->push_back(std::pair<PATypeHolder*,
1834 char*>(new PATypeHolder(Type::VoidTy), 0));
Reid Spencer61c83e02006-08-18 08:43:06 +00001835 CHECK_FOR_ERROR
Chris Lattner69da5cf2002-10-13 20:57:00 +00001836 }
1837 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001838 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1839 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
Reid Spencer61c83e02006-08-18 08:43:06 +00001840 CHECK_FOR_ERROR
Chris Lattner69da5cf2002-10-13 20:57:00 +00001841 }
Chris Lattner00950542001-06-06 20:29:01 +00001842 | /* empty */ {
1843 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00001844 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001845 };
Chris Lattner00950542001-06-06 20:29:01 +00001846
Chris Lattner164c3782005-11-12 00:11:10 +00001847FunctionHeaderH : OptCallingConv TypesV Name '(' ArgList ')'
1848 OptSection OptAlign {
Chris Lattnera8e8f162005-05-06 20:27:19 +00001849 UnEscapeLexed($3);
1850 std::string FunctionName($3);
1851 free($3); // Free strdup'd memory!
Chris Lattnerdda71962001-11-26 18:54:16 +00001852
Chris Lattnera8e8f162005-05-06 20:27:19 +00001853 if (!(*$2)->isFirstClassType() && *$2 != Type::VoidTy)
Reid Spencer61c83e02006-08-18 08:43:06 +00001854 GEN_ERROR("LLVM functions cannot return aggregate types!");
Chris Lattnerc282f5a2003-11-21 22:32:23 +00001855
Chris Lattner9232b992003-04-22 18:42:41 +00001856 std::vector<const Type*> ParamTypeList;
Chris Lattnera8e8f162005-05-06 20:27:19 +00001857 if ($5) { // If there are arguments...
1858 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $5->begin();
1859 I != $5->end(); ++I)
Chris Lattner69da5cf2002-10-13 20:57:00 +00001860 ParamTypeList.push_back(I->first->get());
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001861 }
Chris Lattner00950542001-06-06 20:29:01 +00001862
Chris Lattner2079fde2001-10-13 06:41:08 +00001863 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1864 if (isVarArg) ParamTypeList.pop_back();
1865
Chris Lattnera8e8f162005-05-06 20:27:19 +00001866 const FunctionType *FT = FunctionType::get(*$2, ParamTypeList, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001867 const PointerType *PFT = PointerType::get(FT);
Chris Lattnera8e8f162005-05-06 20:27:19 +00001868 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001869
Chris Lattnera09000d2004-07-14 23:03:46 +00001870 ValID ID;
1871 if (!FunctionName.empty()) {
1872 ID = ValID::create((char*)FunctionName.c_str());
1873 } else {
1874 ID = ValID::create((int)CurModule.Values[PFT].size());
1875 }
1876
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001877 Function *Fn = 0;
Chris Lattnera09000d2004-07-14 23:03:46 +00001878 // See if this function was forward referenced. If so, recycle the object.
1879 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
1880 // Move the function to the end of the list, from whereever it was
1881 // previously inserted.
1882 Fn = cast<Function>(FWRef);
1883 CurModule.CurrentModule->getFunctionList().remove(Fn);
1884 CurModule.CurrentModule->getFunctionList().push_back(Fn);
1885 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
1886 (Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1887 // If this is the case, either we need to be a forward decl, or it needs
1888 // to be.
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001889 if (!CurFun.isDeclare && !Fn->isExternal())
Reid Spencer61c83e02006-08-18 08:43:06 +00001890 GEN_ERROR("Redefinition of function '" + FunctionName + "'!");
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001891
Chris Lattnera09000d2004-07-14 23:03:46 +00001892 // Make sure to strip off any argument names so we can't get conflicts.
1893 if (Fn->isExternal())
Chris Lattnere4d5c442005-03-15 04:54:21 +00001894 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
Chris Lattnera09000d2004-07-14 23:03:46 +00001895 AI != AE; ++AI)
1896 AI->setName("");
Chris Lattner5659dd12002-07-15 00:10:33 +00001897
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001898 } else { // Not already defined?
1899 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
1900 CurModule.CurrentModule);
1901 InsertValue(Fn, CurModule.Values);
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001902 }
Chris Lattner00950542001-06-06 20:29:01 +00001903
Chris Lattner394f2eb2003-10-16 20:12:13 +00001904 CurFun.FunctionStart(Fn);
Chris Lattnera8e8f162005-05-06 20:27:19 +00001905 Fn->setCallingConv($1);
Chris Lattner164c3782005-11-12 00:11:10 +00001906 Fn->setAlignment($8);
1907 if ($7) {
1908 Fn->setSection($7);
1909 free($7);
1910 }
Chris Lattner00950542001-06-06 20:29:01 +00001911
Chris Lattner7e708292002-06-25 16:13:24 +00001912 // Add all of the arguments we parsed to the function...
Chris Lattnera8e8f162005-05-06 20:27:19 +00001913 if ($5) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001914 if (isVarArg) { // Nuke the last entry
Chris Lattnera8e8f162005-05-06 20:27:19 +00001915 assert($5->back().first->get() == Type::VoidTy && $5->back().second == 0&&
Chris Lattner69da5cf2002-10-13 20:57:00 +00001916 "Not a varargs marker!");
Chris Lattnera8e8f162005-05-06 20:27:19 +00001917 delete $5->back().first;
1918 $5->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00001919 }
Chris Lattnere4d5c442005-03-15 04:54:21 +00001920 Function::arg_iterator ArgIt = Fn->arg_begin();
Chris Lattnera8e8f162005-05-06 20:27:19 +00001921 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $5->begin();
1922 I != $5->end(); ++I, ++ArgIt) {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001923 delete I->first; // Delete the typeholder...
1924
Chris Lattner8ab406d2004-07-13 07:59:27 +00001925 setValueName(ArgIt, I->second); // Insert arg into symtab...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001926 InsertValue(ArgIt);
Chris Lattner00950542001-06-06 20:29:01 +00001927 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001928
Chris Lattnera8e8f162005-05-06 20:27:19 +00001929 delete $5; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00001930 }
Reid Spencer61c83e02006-08-18 08:43:06 +00001931 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001932};
Chris Lattner00950542001-06-06 20:29:01 +00001933
Chris Lattner9b02cc32002-05-03 18:23:48 +00001934BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1935
Chris Lattner4ad02e72003-04-16 20:28:45 +00001936FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00001937 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001938
Chris Lattner4ad02e72003-04-16 20:28:45 +00001939 // Make sure that we keep track of the linkage type even if there was a
1940 // previous "declare".
1941 $$->setLinkage($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001942};
Chris Lattner00950542001-06-06 20:29:01 +00001943
Chris Lattner9b02cc32002-05-03 18:23:48 +00001944END : ENDTOK | '}'; // Allow end of '}' to end a function
1945
Chris Lattner79df7c02002-03-26 18:01:55 +00001946Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001947 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001948 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001949};
Chris Lattner00950542001-06-06 20:29:01 +00001950
Chris Lattner394f2eb2003-10-16 20:12:13 +00001951FunctionProto : DECLARE { CurFun.isDeclare = true; } FunctionHeaderH {
1952 $$ = CurFun.CurrentFunction;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001953 CurFun.FunctionDone();
Reid Spencer61c83e02006-08-18 08:43:06 +00001954 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001955};
Chris Lattner00950542001-06-06 20:29:01 +00001956
1957//===----------------------------------------------------------------------===//
1958// Rules to match Basic Blocks
1959//===----------------------------------------------------------------------===//
1960
Chris Lattneraa2c8532006-01-25 22:26:43 +00001961OptSideEffect : /* empty */ {
1962 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00001963 CHECK_FOR_ERROR
Chris Lattneraa2c8532006-01-25 22:26:43 +00001964 }
1965 | SIDEEFFECT {
1966 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00001967 CHECK_FOR_ERROR
Chris Lattneraa2c8532006-01-25 22:26:43 +00001968 };
1969
Chris Lattner00950542001-06-06 20:29:01 +00001970ConstValueRef : ESINT64VAL { // A reference to a direct constant
1971 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001972 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001973 }
1974 | EUINT64VAL {
1975 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001976 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001977 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001978 | FPVAL { // Perhaps it's an FP constant?
1979 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001980 CHECK_FOR_ERROR
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001981 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001982 | TRUETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001983 $$ = ValID::create(ConstantBool::True);
Reid Spencer61c83e02006-08-18 08:43:06 +00001984 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001985 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001986 | FALSETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001987 $$ = ValID::create(ConstantBool::False);
Reid Spencer61c83e02006-08-18 08:43:06 +00001988 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001989 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001990 | NULL_TOK {
1991 $$ = ValID::createNull();
Reid Spencer61c83e02006-08-18 08:43:06 +00001992 CHECK_FOR_ERROR
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001993 }
Chris Lattner16710e92004-10-16 18:17:13 +00001994 | UNDEF {
1995 $$ = ValID::createUndef();
Reid Spencer61c83e02006-08-18 08:43:06 +00001996 CHECK_FOR_ERROR
Chris Lattner16710e92004-10-16 18:17:13 +00001997 }
Chris Lattnerf1f03df2005-12-21 17:53:02 +00001998 | ZEROINITIALIZER { // A vector zero constant.
1999 $$ = ValID::createZeroInit();
Reid Spencer61c83e02006-08-18 08:43:06 +00002000 CHECK_FOR_ERROR
Chris Lattnerf1f03df2005-12-21 17:53:02 +00002001 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00002002 | '<' ConstVector '>' { // Nonempty unsized packed vector
2003 const Type *ETy = (*$2)[0]->getType();
2004 int NumElements = $2->size();
2005
2006 PackedType* pt = PackedType::get(ETy, NumElements);
2007 PATypeHolder* PTy = new PATypeHolder(
2008 HandleUpRefs(
2009 PackedType::get(
2010 ETy,
2011 NumElements)
2012 )
2013 );
2014
2015 // Verify all elements are correct type!
2016 for (unsigned i = 0; i < $2->size(); i++) {
2017 if (ETy != (*$2)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002018 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Brian Gaeke715c90b2004-08-20 06:00:58 +00002019 ETy->getDescription() +"' as required!\nIt is of type '" +
2020 (*$2)[i]->getType()->getDescription() + "'.");
2021 }
2022
2023 $$ = ValID::create(ConstantPacked::get(pt, *$2));
2024 delete PTy; delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002025 CHECK_FOR_ERROR
Brian Gaeke715c90b2004-08-20 06:00:58 +00002026 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00002027 | ConstExpr {
2028 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002029 CHECK_FOR_ERROR
Chris Lattneraa2c8532006-01-25 22:26:43 +00002030 }
2031 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
2032 char *End = UnEscapeLexed($3, true);
2033 std::string AsmStr = std::string($3, End);
2034 End = UnEscapeLexed($5, true);
2035 std::string Constraints = std::string($5, End);
2036 $$ = ValID::createInlineAsm(AsmStr, Constraints, $2);
2037 free($3);
2038 free($5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002039 CHECK_FOR_ERROR
Chris Lattnerd78700d2002-08-16 21:14:40 +00002040 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00002041
Chris Lattner2079fde2001-10-13 06:41:08 +00002042// SymbolicValueRef - Reference to one of two ways of symbolically refering to
2043// another value.
2044//
2045SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00002046 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002047 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002048 }
Chris Lattner6c23f572003-08-22 05:42:10 +00002049 | Name { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00002050 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002051 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002052 };
Chris Lattner2079fde2001-10-13 06:41:08 +00002053
2054// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00002055ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00002056
Chris Lattner00950542001-06-06 20:29:01 +00002057
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002058// ResolvedVal - a <type> <value> pair. This is used only in cases where the
2059// type immediately preceeds the value reference, and allows complex constant
2060// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00002061ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00002062 $$ = getVal(*$1, $2); delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002063 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002064 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00002065
Chris Lattner00950542001-06-06 20:29:01 +00002066BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner8ab406d2004-07-13 07:59:27 +00002067 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002068 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002069 }
Chris Lattner7e708292002-06-25 16:13:24 +00002070 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
Chris Lattner8ab406d2004-07-13 07:59:27 +00002071 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002072 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002073 };
Chris Lattner00950542001-06-06 20:29:01 +00002074
2075
2076// Basic blocks are terminated by branching instructions:
2077// br, br/cc, switch, ret
2078//
Chris Lattner2079fde2001-10-13 06:41:08 +00002079BasicBlock : InstructionList OptAssign BBTerminatorInst {
Chris Lattner8ab406d2004-07-13 07:59:27 +00002080 setValueName($3, $2);
Chris Lattner2079fde2001-10-13 06:41:08 +00002081 InsertValue($3);
2082
2083 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00002084 InsertValue($1);
2085 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002086 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002087 };
Chris Lattner00950542001-06-06 20:29:01 +00002088
2089InstructionList : InstructionList Inst {
2090 $1->getInstList().push_back($2);
2091 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002092 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002093 }
2094 | /* empty */ {
Andrew Lenharth558bc882005-06-18 18:34:52 +00002095 $$ = CurBB = getBBVal(ValID::create((int)CurFun.NextBBNum++), true);
Chris Lattner8d65a062004-07-26 01:40:20 +00002096
2097 // Make sure to move the basic block to the correct location in the
2098 // function, instead of leaving it inserted wherever it was first
2099 // referenced.
Chris Lattnerf924a4c2005-05-06 19:58:35 +00002100 Function::BasicBlockListType &BBL =
2101 CurFun.CurrentFunction->getBasicBlockList();
2102 BBL.splice(BBL.end(), BBL, $$);
Reid Spencer61c83e02006-08-18 08:43:06 +00002103 CHECK_FOR_ERROR
Chris Lattner22ae2322004-07-13 08:39:15 +00002104 }
2105 | LABELSTR {
Andrew Lenharth558bc882005-06-18 18:34:52 +00002106 $$ = CurBB = getBBVal(ValID::create($1), true);
Chris Lattner8d65a062004-07-26 01:40:20 +00002107
2108 // Make sure to move the basic block to the correct location in the
2109 // function, instead of leaving it inserted wherever it was first
2110 // referenced.
Chris Lattnerf924a4c2005-05-06 19:58:35 +00002111 Function::BasicBlockListType &BBL =
2112 CurFun.CurrentFunction->getBasicBlockList();
2113 BBL.splice(BBL.end(), BBL, $$);
Reid Spencer61c83e02006-08-18 08:43:06 +00002114 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002115 };
Chris Lattner00950542001-06-06 20:29:01 +00002116
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002117BBTerminatorInst : RET ResolvedVal { // Return with a result...
2118 $$ = new ReturnInst($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00002119 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002120 }
2121 | RET VOID { // Return with no result...
2122 $$ = new ReturnInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002123 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002124 }
2125 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner64116f92004-07-14 01:33:11 +00002126 $$ = new BranchInst(getBBVal($3));
Reid Spencer61c83e02006-08-18 08:43:06 +00002127 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002128 } // Conditional Branch...
2129 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner64116f92004-07-14 01:33:11 +00002130 $$ = new BranchInst(getBBVal($6), getBBVal($9), getVal(Type::BoolTy, $3));
Reid Spencer61c83e02006-08-18 08:43:06 +00002131 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002132 }
2133 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Chris Lattnerce1df9e2005-01-29 00:35:55 +00002134 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6), $8->size());
Chris Lattner00950542001-06-06 20:29:01 +00002135 $$ = S;
2136
Chris Lattner9232b992003-04-22 18:42:41 +00002137 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00002138 E = $8->end();
Chris Lattner69331f52005-02-24 05:25:17 +00002139 for (; I != E; ++I) {
2140 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
2141 S->addCase(CI, I->second);
2142 else
Reid Spencer61c83e02006-08-18 08:43:06 +00002143 GEN_ERROR("Switch case is constant, but not a simple integer!");
Chris Lattner69331f52005-02-24 05:25:17 +00002144 }
Chris Lattnerf57a43d2004-04-17 23:49:15 +00002145 delete $8;
Reid Spencer61c83e02006-08-18 08:43:06 +00002146 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002147 }
Chris Lattner196850c2003-05-15 21:30:00 +00002148 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Chris Lattnerce1df9e2005-01-29 00:35:55 +00002149 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6), 0);
Chris Lattner196850c2003-05-15 21:30:00 +00002150 $$ = S;
Reid Spencer61c83e02006-08-18 08:43:06 +00002151 CHECK_FOR_ERROR
Chris Lattner196850c2003-05-15 21:30:00 +00002152 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00002153 | INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
2154 TO LABEL ValueRef UNWIND LABEL ValueRef {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002155 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00002156 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00002157
Chris Lattnera8e8f162005-05-06 20:27:19 +00002158 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002159 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00002160 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00002161 std::vector<const Type*> ParamTypes;
Chris Lattnera8e8f162005-05-06 20:27:19 +00002162 if ($6) {
2163 for (std::vector<Value*>::iterator I = $6->begin(), E = $6->end();
Chris Lattner9232b992003-04-22 18:42:41 +00002164 I != E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00002165 ParamTypes.push_back((*I)->getType());
2166 }
2167
2168 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
2169 if (isVarArg) ParamTypes.pop_back();
2170
Chris Lattnera8e8f162005-05-06 20:27:19 +00002171 Ty = FunctionType::get($3->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002172 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00002173 }
Chris Lattner2079fde2001-10-13 06:41:08 +00002174
Chris Lattnera8e8f162005-05-06 20:27:19 +00002175 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00002176
Chris Lattnera8e8f162005-05-06 20:27:19 +00002177 BasicBlock *Normal = getBBVal($10);
2178 BasicBlock *Except = getBBVal($13);
Chris Lattner2079fde2001-10-13 06:41:08 +00002179
2180 // Create the call node...
Chris Lattnera8e8f162005-05-06 20:27:19 +00002181 if (!$6) { // Has no arguments?
Chris Lattner9232b992003-04-22 18:42:41 +00002182 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00002183 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00002184 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00002185 // correctly!
2186 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00002187 FunctionType::param_iterator I = Ty->param_begin();
2188 FunctionType::param_iterator E = Ty->param_end();
Chris Lattnera8e8f162005-05-06 20:27:19 +00002189 std::vector<Value*>::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00002190
2191 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
Reid Spencer3271ed52004-07-18 00:08:11 +00002192 if ((*ArgI)->getType() != *I)
Reid Spencer61c83e02006-08-18 08:43:06 +00002193 GEN_ERROR("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Reid Spencer3271ed52004-07-18 00:08:11 +00002194 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00002195
2196 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002197 GEN_ERROR("Invalid number of parameters detected!");
Chris Lattner2079fde2001-10-13 06:41:08 +00002198
Chris Lattnera8e8f162005-05-06 20:27:19 +00002199 $$ = new InvokeInst(V, Normal, Except, *$6);
Chris Lattner2079fde2001-10-13 06:41:08 +00002200 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00002201 cast<InvokeInst>($$)->setCallingConv($2);
2202
2203 delete $3;
2204 delete $6;
Reid Spencer61c83e02006-08-18 08:43:06 +00002205 CHECK_FOR_ERROR
Chris Lattner36143fc2003-09-08 18:54:55 +00002206 }
2207 | UNWIND {
2208 $$ = new UnwindInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002209 CHECK_FOR_ERROR
Chris Lattner16710e92004-10-16 18:17:13 +00002210 }
2211 | UNREACHABLE {
2212 $$ = new UnreachableInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002213 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002214 };
Chris Lattner2079fde2001-10-13 06:41:08 +00002215
2216
Chris Lattner00950542001-06-06 20:29:01 +00002217
2218JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
2219 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00002220 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00002221 if (V == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002222 GEN_ERROR("May only switch on a constant pool value!");
Chris Lattner00950542001-06-06 20:29:01 +00002223
Chris Lattner64116f92004-07-14 01:33:11 +00002224 $$->push_back(std::make_pair(V, getBBVal($6)));
Reid Spencer61c83e02006-08-18 08:43:06 +00002225 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002226 }
2227 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00002228 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00002229 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00002230
2231 if (V == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002232 GEN_ERROR("May only switch on a constant pool value!");
Chris Lattner00950542001-06-06 20:29:01 +00002233
Chris Lattner64116f92004-07-14 01:33:11 +00002234 $$->push_back(std::make_pair(V, getBBVal($5)));
Reid Spencer61c83e02006-08-18 08:43:06 +00002235 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002236 };
Chris Lattner00950542001-06-06 20:29:01 +00002237
2238Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00002239 // Is this definition named?? if so, assign the name...
Chris Lattner8ab406d2004-07-13 07:59:27 +00002240 setValueName($2, $1);
Chris Lattner00950542001-06-06 20:29:01 +00002241 InsertValue($2);
2242 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002243 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002244};
Chris Lattner00950542001-06-06 20:29:01 +00002245
Chris Lattnerc24d2082001-06-11 15:04:20 +00002246PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Chris Lattner9232b992003-04-22 18:42:41 +00002247 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Chris Lattner64116f92004-07-14 01:33:11 +00002248 $$->push_back(std::make_pair(getVal(*$1, $3), getBBVal($5)));
Chris Lattner30c89792001-09-07 16:35:17 +00002249 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002250 CHECK_FOR_ERROR
Chris Lattnerc24d2082001-06-11 15:04:20 +00002251 }
2252 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
2253 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00002254 $1->push_back(std::make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner64116f92004-07-14 01:33:11 +00002255 getBBVal($6)));
Reid Spencer61c83e02006-08-18 08:43:06 +00002256 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002257 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00002258
2259
Chris Lattner30c89792001-09-07 16:35:17 +00002260ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner9232b992003-04-22 18:42:41 +00002261 $$ = new std::vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002262 $$->push_back($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002263 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002264 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002265 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00002266 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002267 $1->push_back($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002268 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002269 };
Chris Lattner00950542001-06-06 20:29:01 +00002270
2271// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00002272ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00002273
Chris Lattnerddb6db42005-05-06 05:51:46 +00002274OptTailCall : TAIL CALL {
2275 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002276 CHECK_FOR_ERROR
Chris Lattnerddb6db42005-05-06 05:51:46 +00002277 }
2278 | CALL {
2279 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002280 CHECK_FOR_ERROR
Chris Lattnerddb6db42005-05-06 05:51:46 +00002281 };
2282
2283
2284
Chris Lattner4a6482b2002-09-10 19:57:26 +00002285InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Brian Gaeke715c90b2004-08-20 06:00:58 +00002286 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
2287 !isa<PackedType>((*$2).get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002288 GEN_ERROR(
Brian Gaeke715c90b2004-08-20 06:00:58 +00002289 "Arithmetic operator requires integer, FP, or packed operands!");
Misha Brukman5a2a3822005-05-10 22:02:28 +00002290 if (isa<PackedType>((*$2).get()) && $1 == Instruction::Rem)
Reid Spencer61c83e02006-08-18 08:43:06 +00002291 GEN_ERROR("Rem not supported on packed types!");
Chris Lattner4a6482b2002-09-10 19:57:26 +00002292 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
2293 if ($$ == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002294 GEN_ERROR("binary operator returned null!");
Chris Lattner4a6482b2002-09-10 19:57:26 +00002295 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002296 CHECK_FOR_ERROR
Chris Lattner4a6482b2002-09-10 19:57:26 +00002297 }
2298 | LogicalOps Types ValueRef ',' ValueRef {
Chris Lattner0a017832005-12-21 18:31:29 +00002299 if (!(*$2)->isIntegral()) {
2300 if (!isa<PackedType>($2->get()) ||
2301 !cast<PackedType>($2->get())->getElementType()->isIntegral())
Reid Spencer61c83e02006-08-18 08:43:06 +00002302 GEN_ERROR("Logical operator requires integral operands!");
Chris Lattner0a017832005-12-21 18:31:29 +00002303 }
Chris Lattner4a6482b2002-09-10 19:57:26 +00002304 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
2305 if ($$ == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002306 GEN_ERROR("binary operator returned null!");
Chris Lattner4a6482b2002-09-10 19:57:26 +00002307 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002308 CHECK_FOR_ERROR
Chris Lattner4a6482b2002-09-10 19:57:26 +00002309 }
2310 | SetCondOps Types ValueRef ',' ValueRef {
Reid Spencer57b6eec2004-08-21 16:11:02 +00002311 if(isa<PackedType>((*$2).get())) {
Reid Spencer61c83e02006-08-18 08:43:06 +00002312 GEN_ERROR(
Reid Spencer57b6eec2004-08-21 16:11:02 +00002313 "PackedTypes currently not supported in setcc instructions!");
2314 }
Chris Lattner1cff96a2002-09-10 22:37:46 +00002315 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00002316 if ($$ == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002317 GEN_ERROR("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00002318 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002319 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002320 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00002321 | NOT ResolvedVal {
2322 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
2323 << " Replacing with 'xor'.\n";
2324
2325 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
2326 if (Ones == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002327 GEN_ERROR("Expected integral type for not instruction!");
Chris Lattner699f1eb2002-08-14 17:12:33 +00002328
2329 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00002330 if ($$ == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002331 GEN_ERROR("Could not create a xor instruction!");
2332 CHECK_FOR_ERROR
Chris Lattner09083092001-07-08 04:57:15 +00002333 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002334 | ShiftOps ResolvedVal ',' ResolvedVal {
2335 if ($4->getType() != Type::UByteTy)
Reid Spencer61c83e02006-08-18 08:43:06 +00002336 GEN_ERROR("Shift amount must be ubyte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00002337 if (!$2->getType()->isInteger())
Reid Spencer61c83e02006-08-18 08:43:06 +00002338 GEN_ERROR("Shift constant expression requires integer operand!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002339 $$ = new ShiftInst($1, $2, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002340 CHECK_FOR_ERROR
Chris Lattner027dcc52001-07-08 21:10:27 +00002341 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002342 | CAST ResolvedVal TO Types {
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00002343 if (!$4->get()->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002344 GEN_ERROR("cast instruction to a non-primitive type: '" +
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00002345 $4->get()->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00002346 $$ = new CastInst($2, *$4);
2347 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00002348 CHECK_FOR_ERROR
Chris Lattner09083092001-07-08 04:57:15 +00002349 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00002350 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
2351 if ($2->getType() != Type::BoolTy)
Reid Spencer61c83e02006-08-18 08:43:06 +00002352 GEN_ERROR("select condition must be boolean!");
Chris Lattner76f0ff32004-03-12 05:51:36 +00002353 if ($4->getType() != $6->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002354 GEN_ERROR("select value types should match!");
Chris Lattner76f0ff32004-03-12 05:51:36 +00002355 $$ = new SelectInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002356 CHECK_FOR_ERROR
Chris Lattner76f0ff32004-03-12 05:51:36 +00002357 }
Chris Lattner99e7ab72003-10-18 05:53:13 +00002358 | VAARG ResolvedVal ',' Types {
Andrew Lenharthe78f50d2005-06-19 03:53:56 +00002359 NewVarArgs = true;
Chris Lattner99e7ab72003-10-18 05:53:13 +00002360 $$ = new VAArgInst($2, *$4);
2361 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00002362 CHECK_FOR_ERROR
Chris Lattner99e7ab72003-10-18 05:53:13 +00002363 }
Andrew Lenharth558bc882005-06-18 18:34:52 +00002364 | VAARG_old ResolvedVal ',' Types {
2365 ObsoleteVarArgs = true;
2366 const Type* ArgTy = $2->getType();
2367 Function* NF = CurModule.CurrentModule->
Jeff Cohen66c5fd62005-10-23 04:37:20 +00002368 getOrInsertFunction("llvm.va_copy", ArgTy, ArgTy, (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +00002369
2370 //b = vaarg a, t ->
2371 //foo = alloca 1 of t
2372 //bar = vacopy a
2373 //store bar -> foo
2374 //b = vaarg foo, t
2375 AllocaInst* foo = new AllocaInst(ArgTy, 0, "vaarg.fix");
2376 CurBB->getInstList().push_back(foo);
2377 CallInst* bar = new CallInst(NF, $2);
2378 CurBB->getInstList().push_back(bar);
2379 CurBB->getInstList().push_back(new StoreInst(bar, foo));
2380 $$ = new VAArgInst(foo, *$4);
2381 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00002382 CHECK_FOR_ERROR
Andrew Lenharth558bc882005-06-18 18:34:52 +00002383 }
2384 | VANEXT_old ResolvedVal ',' Types {
2385 ObsoleteVarArgs = true;
2386 const Type* ArgTy = $2->getType();
2387 Function* NF = CurModule.CurrentModule->
Jeff Cohen66c5fd62005-10-23 04:37:20 +00002388 getOrInsertFunction("llvm.va_copy", ArgTy, ArgTy, (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +00002389
2390 //b = vanext a, t ->
2391 //foo = alloca 1 of t
2392 //bar = vacopy a
2393 //store bar -> foo
2394 //tmp = vaarg foo, t
2395 //b = load foo
2396 AllocaInst* foo = new AllocaInst(ArgTy, 0, "vanext.fix");
2397 CurBB->getInstList().push_back(foo);
2398 CallInst* bar = new CallInst(NF, $2);
2399 CurBB->getInstList().push_back(bar);
2400 CurBB->getInstList().push_back(new StoreInst(bar, foo));
2401 Instruction* tmp = new VAArgInst(foo, *$4);
2402 CurBB->getInstList().push_back(tmp);
2403 $$ = new LoadInst(foo);
Chris Lattner8f77dae2003-05-08 02:44:12 +00002404 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00002405 CHECK_FOR_ERROR
Chris Lattner8f77dae2003-05-08 02:44:12 +00002406 }
Robert Bocchino9c62b562006-01-10 19:04:32 +00002407 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Chris Lattner2d7349a2006-04-08 04:08:32 +00002408 if (!ExtractElementInst::isValidOperands($2, $4))
Reid Spencer61c83e02006-08-18 08:43:06 +00002409 GEN_ERROR("Invalid extractelement operands!");
Robert Bocchino9c62b562006-01-10 19:04:32 +00002410 $$ = new ExtractElementInst($2, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002411 CHECK_FOR_ERROR
Robert Bocchino9c62b562006-01-10 19:04:32 +00002412 }
Robert Bocchino2def1b32006-01-17 20:06:25 +00002413 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Chris Lattner2d7349a2006-04-08 04:08:32 +00002414 if (!InsertElementInst::isValidOperands($2, $4, $6))
Reid Spencer61c83e02006-08-18 08:43:06 +00002415 GEN_ERROR("Invalid insertelement operands!");
Robert Bocchino2def1b32006-01-17 20:06:25 +00002416 $$ = new InsertElementInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002417 CHECK_FOR_ERROR
Robert Bocchino2def1b32006-01-17 20:06:25 +00002418 }
Chris Lattner4c949082006-04-08 01:18:35 +00002419 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
2420 if (!ShuffleVectorInst::isValidOperands($2, $4, $6))
Reid Spencer61c83e02006-08-18 08:43:06 +00002421 GEN_ERROR("Invalid shufflevector operands!");
Chris Lattner4c949082006-04-08 01:18:35 +00002422 $$ = new ShuffleVectorInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002423 CHECK_FOR_ERROR
Chris Lattner4c949082006-04-08 01:18:35 +00002424 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00002425 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00002426 const Type *Ty = $2->front().first->getType();
Chris Lattner8ea8f362003-10-30 01:38:18 +00002427 if (!Ty->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002428 GEN_ERROR("PHI node operands must be of first class type!");
Chris Lattnerc24d2082001-06-11 15:04:20 +00002429 $$ = new PHINode(Ty);
Chris Lattnerce1df9e2005-01-29 00:35:55 +00002430 ((PHINode*)$$)->reserveOperandSpace($2->size());
Chris Lattner00950542001-06-06 20:29:01 +00002431 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00002432 if ($2->front().first->getType() != Ty)
Reid Spencer61c83e02006-08-18 08:43:06 +00002433 GEN_ERROR("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00002434 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00002435 $2->pop_front();
2436 }
2437 delete $2; // Free the list...
Reid Spencer61c83e02006-08-18 08:43:06 +00002438 CHECK_FOR_ERROR
Chris Lattnerddb6db42005-05-06 05:51:46 +00002439 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00002440 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002441 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00002442 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00002443
Chris Lattnera8e8f162005-05-06 20:27:19 +00002444 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002445 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00002446 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00002447 std::vector<const Type*> ParamTypes;
Chris Lattnera8e8f162005-05-06 20:27:19 +00002448 if ($6) {
2449 for (std::vector<Value*>::iterator I = $6->begin(), E = $6->end();
Chris Lattner9232b992003-04-22 18:42:41 +00002450 I != E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00002451 ParamTypes.push_back((*I)->getType());
2452 }
Chris Lattner2079fde2001-10-13 06:41:08 +00002453
2454 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
2455 if (isVarArg) ParamTypes.pop_back();
2456
Chris Lattnera8e8f162005-05-06 20:27:19 +00002457 if (!(*$3)->isFirstClassType() && *$3 != Type::VoidTy)
Reid Spencer61c83e02006-08-18 08:43:06 +00002458 GEN_ERROR("LLVM functions cannot return aggregate types!");
Chris Lattner595f06c2005-02-01 01:47:42 +00002459
Chris Lattnera8e8f162005-05-06 20:27:19 +00002460 Ty = FunctionType::get($3->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002461 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00002462 }
Chris Lattner00950542001-06-06 20:29:01 +00002463
Chris Lattnera8e8f162005-05-06 20:27:19 +00002464 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00002465
Chris Lattner8b81bf52001-07-25 22:47:46 +00002466 // Create the call node...
Chris Lattnera8e8f162005-05-06 20:27:19 +00002467 if (!$6) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00002468 // Make sure no arguments is a good thing!
2469 if (Ty->getNumParams() != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002470 GEN_ERROR("No arguments passed to a function that "
Chris Lattnera4e25182002-07-25 20:52:56 +00002471 "expects arguments!");
2472
Chris Lattner9232b992003-04-22 18:42:41 +00002473 $$ = new CallInst(V, std::vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00002474 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00002475 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00002476 // correctly!
2477 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00002478 FunctionType::param_iterator I = Ty->param_begin();
2479 FunctionType::param_iterator E = Ty->param_end();
Chris Lattnera8e8f162005-05-06 20:27:19 +00002480 std::vector<Value*>::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00002481
2482 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
Reid Spencer3271ed52004-07-18 00:08:11 +00002483 if ((*ArgI)->getType() != *I)
Reid Spencer61c83e02006-08-18 08:43:06 +00002484 GEN_ERROR("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Reid Spencer3271ed52004-07-18 00:08:11 +00002485 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00002486
Chris Lattner8b81bf52001-07-25 22:47:46 +00002487 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002488 GEN_ERROR("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00002489
Chris Lattnera8e8f162005-05-06 20:27:19 +00002490 $$ = new CallInst(V, *$6);
Chris Lattner8b81bf52001-07-25 22:47:46 +00002491 }
Chris Lattnerddb6db42005-05-06 05:51:46 +00002492 cast<CallInst>($$)->setTailCall($1);
Chris Lattnera8e8f162005-05-06 20:27:19 +00002493 cast<CallInst>($$)->setCallingConv($2);
2494 delete $3;
2495 delete $6;
Reid Spencer61c83e02006-08-18 08:43:06 +00002496 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002497 }
2498 | MemoryInst {
2499 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002500 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002501 };
Chris Lattner00950542001-06-06 20:29:01 +00002502
Chris Lattner6cdb0112001-11-26 16:54:11 +00002503
2504// IndexList - List of indices for GEP based instructions...
2505IndexList : ',' ValueRefList {
Chris Lattner15c9c032003-09-08 18:20:29 +00002506 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002507 CHECK_FOR_ERROR
Chris Lattner15c9c032003-09-08 18:20:29 +00002508 } | /* empty */ {
2509 $$ = new std::vector<Value*>();
Reid Spencer61c83e02006-08-18 08:43:06 +00002510 CHECK_FOR_ERROR
Chris Lattner15c9c032003-09-08 18:20:29 +00002511 };
2512
2513OptVolatile : VOLATILE {
2514 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002515 CHECK_FOR_ERROR
Chris Lattner15c9c032003-09-08 18:20:29 +00002516 }
2517 | /* empty */ {
2518 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002519 CHECK_FOR_ERROR
Chris Lattner15c9c032003-09-08 18:20:29 +00002520 };
2521
Chris Lattner027dcc52001-07-08 21:10:27 +00002522
Chris Lattnerddb6db42005-05-06 05:51:46 +00002523
Chris Lattner66db8e42005-11-06 06:34:12 +00002524MemoryInst : MALLOC Types OptCAlign {
Chris Lattner66db8e42005-11-06 06:34:12 +00002525 $$ = new MallocInst(*$2, 0, $3);
Chris Lattner30c89792001-09-07 16:35:17 +00002526 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002527 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002528 }
Chris Lattner66db8e42005-11-06 06:34:12 +00002529 | MALLOC Types ',' UINT ValueRef OptCAlign {
Chris Lattner66db8e42005-11-06 06:34:12 +00002530 $$ = new MallocInst(*$2, getVal($4, $5), $6);
Nate Begeman14b05292005-11-05 09:21:28 +00002531 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002532 CHECK_FOR_ERROR
Nate Begeman14b05292005-11-05 09:21:28 +00002533 }
Chris Lattner66db8e42005-11-06 06:34:12 +00002534 | ALLOCA Types OptCAlign {
Chris Lattner66db8e42005-11-06 06:34:12 +00002535 $$ = new AllocaInst(*$2, 0, $3);
Nate Begeman14b05292005-11-05 09:21:28 +00002536 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002537 CHECK_FOR_ERROR
Nate Begeman14b05292005-11-05 09:21:28 +00002538 }
Chris Lattner66db8e42005-11-06 06:34:12 +00002539 | ALLOCA Types ',' UINT ValueRef OptCAlign {
Chris Lattner66db8e42005-11-06 06:34:12 +00002540 $$ = new AllocaInst(*$2, getVal($4, $5), $6);
Nate Begeman14b05292005-11-05 09:21:28 +00002541 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002542 CHECK_FOR_ERROR
Nate Begeman14b05292005-11-05 09:21:28 +00002543 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002544 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00002545 if (!isa<PointerType>($2->getType()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002546 GEN_ERROR("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00002547 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002548 $$ = new FreeInst($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00002549 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002550 }
2551
Chris Lattner15c9c032003-09-08 18:20:29 +00002552 | OptVolatile LOAD Types ValueRef {
2553 if (!isa<PointerType>($3->get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002554 GEN_ERROR("Can't load from nonpointer type: " +
Reid Spencer3271ed52004-07-18 00:08:11 +00002555 (*$3)->getDescription());
Chris Lattner1c1bb332004-10-09 02:18:58 +00002556 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002557 GEN_ERROR("Can't load from pointer of non-first-class type: " +
Chris Lattner1c1bb332004-10-09 02:18:58 +00002558 (*$3)->getDescription());
Chris Lattner79ad13802003-09-08 20:29:46 +00002559 $$ = new LoadInst(getVal(*$3, $4), "", $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002560 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002561 CHECK_FOR_ERROR
Chris Lattner027dcc52001-07-08 21:10:27 +00002562 }
Chris Lattner15c9c032003-09-08 18:20:29 +00002563 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
2564 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002565 if (!PT)
Reid Spencer61c83e02006-08-18 08:43:06 +00002566 GEN_ERROR("Can't store to a nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00002567 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002568 const Type *ElTy = PT->getElementType();
Chris Lattner15c9c032003-09-08 18:20:29 +00002569 if (ElTy != $3->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002570 GEN_ERROR("Can't store '" + $3->getType()->getDescription() +
Chris Lattner72e00252001-12-14 16:28:42 +00002571 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner0383cc42002-08-21 23:51:21 +00002572
Chris Lattner79ad13802003-09-08 20:29:46 +00002573 $$ = new StoreInst($3, getVal(*$5, $6), $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002574 delete $5;
Reid Spencer61c83e02006-08-18 08:43:06 +00002575 CHECK_FOR_ERROR
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002576 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00002577 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00002578 if (!isa<PointerType>($2->get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002579 GEN_ERROR("getelementptr insn requires pointer operand!");
Chris Lattner830b6f92004-04-05 01:30:04 +00002580
2581 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
2582 // indices to uint struct indices for compatibility.
2583 generic_gep_type_iterator<std::vector<Value*>::iterator>
2584 GTI = gep_type_begin($2->get(), $4->begin(), $4->end()),
2585 GTE = gep_type_end($2->get(), $4->begin(), $4->end());
2586 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
2587 if (isa<StructType>(*GTI)) // Only change struct indices
2588 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
2589 if (CUI->getType() == Type::UByteTy)
2590 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
2591
Chris Lattner30c89792001-09-07 16:35:17 +00002592 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Reid Spencer61c83e02006-08-18 08:43:06 +00002593 GEN_ERROR("Invalid getelementptr indices for type '" +
Chris Lattner830b6f92004-04-05 01:30:04 +00002594 (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00002595 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
2596 delete $2; delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00002597 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002598 };
Chris Lattner027dcc52001-07-08 21:10:27 +00002599
Brian Gaeked0fde302003-11-11 22:41:34 +00002600
Chris Lattner00950542001-06-06 20:29:01 +00002601%%
Reid Spencer61c83e02006-08-18 08:43:06 +00002602
2603void llvm::GenerateError(const std::string &message, int LineNo) {
2604 if (LineNo == -1) LineNo = llvmAsmlineno;
2605 // TODO: column number in exception
2606 if (TheParseError)
2607 TheParseError->setError(CurFilename, message, LineNo);
2608 TriggerError = 1;
2609}
2610
Chris Lattner09083092001-07-08 04:57:15 +00002611int yyerror(const char *ErrorMsg) {
Chris Lattner9232b992003-04-22 18:42:41 +00002612 std::string where
2613 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002614 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Chris Lattner9232b992003-04-22 18:42:41 +00002615 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
Chris Lattnerbc280ab2004-03-30 20:58:25 +00002616 if (yychar == YYEMPTY || yychar == 0)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002617 errMsg += "end-of-file.";
2618 else
Chris Lattner9232b992003-04-22 18:42:41 +00002619 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Reid Spencer61c83e02006-08-18 08:43:06 +00002620 GenerateError(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00002621 return 0;
2622}