blob: a3ff24346ba4636080dfa15bcb2b0c8dc62c4bf1 [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
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000153 std::map<const Type*, ValueList> Values; // Keep track of #'d definitions
Chris Lattner735f2702004-07-08 22:30:50 +0000154 std::map<const Type*, ValueList> LateResolveValues;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000155 bool isDeclare; // Is this function a forward declararation?
156 GlobalValue::LinkageTypes Linkage; // Linkage for forward declaration.
Chris Lattner00950542001-06-06 20:29:01 +0000157
Chris Lattner2e2ed292004-07-14 06:28:35 +0000158 /// BBForwardRefs - When we see forward references to basic blocks, keep
159 /// track of them here.
160 std::map<BasicBlock*, std::pair<ValID, int> > BBForwardRefs;
161 std::vector<BasicBlock*> NumberedBlocks;
162 unsigned NextBBNum;
163
Chris Lattner79df7c02002-03-26 18:01:55 +0000164 inline PerFunctionInfo() {
165 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000166 isDeclare = false;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000167 Linkage = GlobalValue::ExternalLinkage;
Chris Lattner00950542001-06-06 20:29:01 +0000168 }
169
Chris Lattner79df7c02002-03-26 18:01:55 +0000170 inline void FunctionStart(Function *M) {
171 CurrentFunction = M;
Chris Lattner2e2ed292004-07-14 06:28:35 +0000172 NextBBNum = 0;
Chris Lattner00950542001-06-06 20:29:01 +0000173 }
174
Chris Lattner79df7c02002-03-26 18:01:55 +0000175 void FunctionDone() {
Chris Lattner2e2ed292004-07-14 06:28:35 +0000176 NumberedBlocks.clear();
177
178 // Any forward referenced blocks left?
179 if (!BBForwardRefs.empty())
Reid Spencer61c83e02006-08-18 08:43:06 +0000180 GenerateError("Undefined reference to label " +
Chris Lattner83beacd2005-02-24 04:59:49 +0000181 BBForwardRefs.begin()->first->getName());
Chris Lattner2e2ed292004-07-14 06:28:35 +0000182
183 // Resolve all forward references now.
Chris Lattner386a3b72001-10-16 19:54:17 +0000184 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000185
Chris Lattner7e708292002-06-25 16:13:24 +0000186 Values.clear(); // Clear out function local definitions
Chris Lattner79df7c02002-03-26 18:01:55 +0000187 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000188 isDeclare = false;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000189 Linkage = GlobalValue::ExternalLinkage;
Chris Lattner00950542001-06-06 20:29:01 +0000190 }
Chris Lattner394f2eb2003-10-16 20:12:13 +0000191} CurFun; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000192
Chris Lattner394f2eb2003-10-16 20:12:13 +0000193static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000194
Chris Lattner00950542001-06-06 20:29:01 +0000195
196//===----------------------------------------------------------------------===//
197// Code to handle definitions of all the types
198//===----------------------------------------------------------------------===//
199
Chris Lattner735f2702004-07-08 22:30:50 +0000200static int InsertValue(Value *V,
201 std::map<const Type*,ValueList> &ValueTab = CurFun.Values) {
202 if (V->hasName()) return -1; // Is this a numbered definition?
Chris Lattner2079fde2001-10-13 06:41:08 +0000203
204 // Yes, insert the value into the value table...
Chris Lattner735f2702004-07-08 22:30:50 +0000205 ValueList &List = ValueTab[V->getType()];
206 List.push_back(V);
Chris Lattner16af11d2004-02-09 21:03:38 +0000207 return List.size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000208}
209
Chris Lattner30c89792001-09-07 16:35:17 +0000210static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000211 switch (D.Type) {
Chris Lattnera09000d2004-07-14 23:03:46 +0000212 case ValID::NumberVal: // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000213 // Module constants occupy the lowest numbered slots...
Misha Brukman5a2a3822005-05-10 22:02:28 +0000214 if ((unsigned)D.Num < CurModule.Types.size())
Chris Lattnera09000d2004-07-14 23:03:46 +0000215 return CurModule.Types[(unsigned)D.Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000216 break;
Chris Lattnera09000d2004-07-14 23:03:46 +0000217 case ValID::NameVal: // Is it a named definition?
218 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.Name)) {
219 D.destroy(); // Free old strdup'd memory...
220 return N;
Chris Lattner6e6026b2002-11-20 18:36:02 +0000221 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000222 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000223 default:
Reid Spencer61c83e02006-08-18 08:43:06 +0000224 GenerateError("Internal parser error: Invalid symbol type reference!");
Chris Lattner30c89792001-09-07 16:35:17 +0000225 }
226
227 // If we reached here, we referenced either a symbol that we don't know about
228 // or an id number that hasn't been read yet. We may be referencing something
229 // forward, so just create an entry to be resolved later and get to it...
230 //
231 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
232
Chris Lattner355ad1f2005-03-21 06:27:42 +0000233
234 if (inFunctionScope()) {
235 if (D.Type == ValID::NameVal)
Reid Spencer61c83e02006-08-18 08:43:06 +0000236 GenerateError("Reference to an undefined type: '" + D.getName() + "'");
Chris Lattner355ad1f2005-03-21 06:27:42 +0000237 else
Reid Spencer61c83e02006-08-18 08:43:06 +0000238 GenerateError("Reference to an undefined type: #" + itostr(D.Num));
Chris Lattner4a42e902001-10-22 05:56:09 +0000239 }
Chris Lattner30c89792001-09-07 16:35:17 +0000240
Chris Lattner355ad1f2005-03-21 06:27:42 +0000241 std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D);
242 if (I != CurModule.LateResolveTypes.end())
243 return I->second;
244
Chris Lattner82269592001-10-22 06:01:08 +0000245 Type *Typ = OpaqueType::get();
Chris Lattner355ad1f2005-03-21 06:27:42 +0000246 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
Chris Lattner30c89792001-09-07 16:35:17 +0000247 return Typ;
Chris Lattner355ad1f2005-03-21 06:27:42 +0000248 }
Chris Lattner30c89792001-09-07 16:35:17 +0000249
Chris Lattner9232b992003-04-22 18:42:41 +0000250static Value *lookupInSymbolTable(const Type *Ty, const std::string &Name) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000251 SymbolTable &SymTab =
Chris Lattner394f2eb2003-10-16 20:12:13 +0000252 inFunctionScope() ? CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner9705a152002-05-02 19:27:42 +0000253 CurModule.CurrentModule->getSymbolTable();
Chris Lattner6e6026b2002-11-20 18:36:02 +0000254 return SymTab.lookup(Ty, Name);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000255}
256
Chris Lattner2079fde2001-10-13 06:41:08 +0000257// getValNonImprovising - Look up the value specified by the provided type and
258// the provided ValID. If the value exists and has already been defined, return
259// it. Otherwise return null.
260//
261static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000262 if (isa<FunctionType>(Ty))
Reid Spencer61c83e02006-08-18 08:43:06 +0000263 GenerateError("Functions are not values and "
Chris Lattner79df7c02002-03-26 18:01:55 +0000264 "must be referenced as pointers");
Chris Lattner386a3b72001-10-16 19:54:17 +0000265
Chris Lattner30c89792001-09-07 16:35:17 +0000266 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000267 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner00950542001-06-06 20:29:01 +0000268 unsigned Num = (unsigned)D.Num;
269
270 // Module constants occupy the lowest numbered slots...
Chris Lattner735f2702004-07-08 22:30:50 +0000271 std::map<const Type*,ValueList>::iterator VI = CurModule.Values.find(Ty);
Chris Lattner16af11d2004-02-09 21:03:38 +0000272 if (VI != CurModule.Values.end()) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000273 if (Num < VI->second.size())
Chris Lattner16af11d2004-02-09 21:03:38 +0000274 return VI->second[Num];
275 Num -= VI->second.size();
Chris Lattner00950542001-06-06 20:29:01 +0000276 }
277
278 // Make sure that our type is within bounds
Chris Lattner735f2702004-07-08 22:30:50 +0000279 VI = CurFun.Values.find(Ty);
Chris Lattner16af11d2004-02-09 21:03:38 +0000280 if (VI == CurFun.Values.end()) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000281
282 // Check that the number is within bounds...
Chris Lattner16af11d2004-02-09 21:03:38 +0000283 if (VI->second.size() <= Num) return 0;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000284
Chris Lattner16af11d2004-02-09 21:03:38 +0000285 return VI->second[Num];
Chris Lattner00950542001-06-06 20:29:01 +0000286 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000287
Chris Lattner1a1cb112001-09-30 22:46:54 +0000288 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000289 Value *N = lookupInSymbolTable(Ty, std::string(D.Name));
Chris Lattner2079fde2001-10-13 06:41:08 +0000290 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000291
292 D.destroy(); // Free old strdup'd memory...
293 return N;
294 }
295
Misha Brukman5a2a3822005-05-10 22:02:28 +0000296 // Check to make sure that "Ty" is an integral type, and that our
Chris Lattner2079fde2001-10-13 06:41:08 +0000297 // value will fit into the specified type...
298 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattnerd78700d2002-08-16 21:14:40 +0000299 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
Reid Spencer61c83e02006-08-18 08:43:06 +0000300 GenerateError("Signed integral constant '" +
Misha Brukman5a2a3822005-05-10 22:02:28 +0000301 itostr(D.ConstPool64) + "' is invalid for type '" +
Chris Lattnerd78700d2002-08-16 21:14:40 +0000302 Ty->getDescription() + "'!");
303 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000304
305 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000306 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
307 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000308 GenerateError("Integral constant '" + utostr(D.UConstPool64) +
Chris Lattnerf8dff732002-07-18 05:18:37 +0000309 "' is invalid or out of range!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000310 } else { // This is really a signed reference. Transmogrify.
Reid Spencer3271ed52004-07-18 00:08:11 +0000311 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000312 }
313 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000314 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000315 }
316
Chris Lattner2079fde2001-10-13 06:41:08 +0000317 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000318 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Reid Spencer61c83e02006-08-18 08:43:06 +0000319 GenerateError("FP constant invalid for type!!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000320 return ConstantFP::get(Ty, D.ConstPoolFP);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000321
Chris Lattner2079fde2001-10-13 06:41:08 +0000322 case ValID::ConstNullVal: // Is it a null value?
Chris Lattner9b625032002-05-06 16:15:30 +0000323 if (!isa<PointerType>(Ty))
Reid Spencer61c83e02006-08-18 08:43:06 +0000324 GenerateError("Cannot create a a non pointer null!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000325 return ConstantPointerNull::get(cast<PointerType>(Ty));
Misha Brukman5a2a3822005-05-10 22:02:28 +0000326
Chris Lattner16710e92004-10-16 18:17:13 +0000327 case ValID::ConstUndefVal: // Is it an undef value?
328 return UndefValue::get(Ty);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000329
Chris Lattnerf1f03df2005-12-21 17:53:02 +0000330 case ValID::ConstZeroVal: // Is it a zero value?
331 return Constant::getNullValue(Ty);
332
Chris Lattnerd78700d2002-08-16 21:14:40 +0000333 case ValID::ConstantVal: // Fully resolved constant?
334 if (D.ConstantValue->getType() != Ty)
Reid Spencer61c83e02006-08-18 08:43:06 +0000335 GenerateError("Constant expression type different from required type!");
Chris Lattnerd78700d2002-08-16 21:14:40 +0000336 return D.ConstantValue;
337
Chris Lattneraa2c8532006-01-25 22:26:43 +0000338 case ValID::InlineAsmVal: { // Inline asm expression
339 const PointerType *PTy = dyn_cast<PointerType>(Ty);
340 const FunctionType *FTy =
341 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
342 if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints))
Reid Spencer61c83e02006-08-18 08:43:06 +0000343 GenerateError("Invalid type for asm constraint string!");
Chris Lattneraa2c8532006-01-25 22:26:43 +0000344 InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints,
345 D.IAD->HasSideEffects);
346 D.destroy(); // Free InlineAsmDescriptor.
347 return IA;
348 }
Chris Lattner30c89792001-09-07 16:35:17 +0000349 default:
350 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000351 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000352 } // End of switch
353
Chris Lattner2079fde2001-10-13 06:41:08 +0000354 assert(0 && "Unhandled case!");
355 return 0;
356}
357
Chris Lattner2079fde2001-10-13 06:41:08 +0000358// getVal - This function is identical to getValNonImprovising, except that if a
359// value is not already defined, it "improvises" by creating a placeholder var
360// that looks and acts just like the requested variable. When the value is
361// defined later, all uses of the placeholder variable are replaced with the
362// real thing.
363//
Chris Lattner64116f92004-07-14 01:33:11 +0000364static Value *getVal(const Type *Ty, const ValID &ID) {
365 if (Ty == Type::LabelTy)
Reid Spencer61c83e02006-08-18 08:43:06 +0000366 GenerateError("Cannot use a basic block here");
Chris Lattner2079fde2001-10-13 06:41:08 +0000367
Chris Lattner64116f92004-07-14 01:33:11 +0000368 // See if the value has already been defined.
369 Value *V = getValNonImprovising(Ty, ID);
Chris Lattner2079fde2001-10-13 06:41:08 +0000370 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000371
Chris Lattner60cd9552005-03-23 01:29:26 +0000372 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty))
Reid Spencer61c83e02006-08-18 08:43:06 +0000373 GenerateError("Invalid use of a composite type!");
Chris Lattner60cd9552005-03-23 01:29:26 +0000374
Chris Lattner00950542001-06-06 20:29:01 +0000375 // If we reached here, we referenced either a symbol that we don't know about
376 // or an id number that hasn't been read yet. We may be referencing something
377 // forward, so just create an entry to be resolved later and get to it...
378 //
Chris Lattner64116f92004-07-14 01:33:11 +0000379 V = new Argument(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000380
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000381 // Remember where this forward reference came from. FIXME, shouldn't we try
382 // to recycle these things??
Chris Lattner64116f92004-07-14 01:33:11 +0000383 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000384 llvmAsmlineno)));
385
Chris Lattner79df7c02002-03-26 18:01:55 +0000386 if (inFunctionScope())
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000387 InsertValue(V, CurFun.LateResolveValues);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000388 else
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000389 InsertValue(V, CurModule.LateResolveValues);
390 return V;
Chris Lattner00950542001-06-06 20:29:01 +0000391}
392
Chris Lattner2e2ed292004-07-14 06:28:35 +0000393/// getBBVal - This is used for two purposes:
394/// * If isDefinition is true, a new basic block with the specified ID is being
395/// defined.
396/// * If isDefinition is true, this is a reference to a basic block, which may
397/// or may not be a forward reference.
398///
399static BasicBlock *getBBVal(const ValID &ID, bool isDefinition = false) {
Chris Lattner64116f92004-07-14 01:33:11 +0000400 assert(inFunctionScope() && "Can't get basic block at global scope!");
401
Chris Lattner2e2ed292004-07-14 06:28:35 +0000402 std::string Name;
403 BasicBlock *BB = 0;
404 switch (ID.Type) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000405 default: GenerateError("Illegal label reference " + ID.getName());
Chris Lattner2e2ed292004-07-14 06:28:35 +0000406 case ValID::NumberVal: // Is it a numbered definition?
407 if (unsigned(ID.Num) >= CurFun.NumberedBlocks.size())
408 CurFun.NumberedBlocks.resize(ID.Num+1);
409 BB = CurFun.NumberedBlocks[ID.Num];
410 break;
411 case ValID::NameVal: // Is it a named definition?
412 Name = ID.Name;
Chris Lattnera09000d2004-07-14 23:03:46 +0000413 if (Value *N = CurFun.CurrentFunction->
414 getSymbolTable().lookup(Type::LabelTy, Name))
Chris Lattner2e2ed292004-07-14 06:28:35 +0000415 BB = cast<BasicBlock>(N);
416 break;
417 }
Chris Lattner64116f92004-07-14 01:33:11 +0000418
Chris Lattner2e2ed292004-07-14 06:28:35 +0000419 // See if the block has already been defined.
420 if (BB) {
421 // If this is the definition of the block, make sure the existing value was
422 // just a forward reference. If it was a forward reference, there will be
423 // an entry for it in the PlaceHolderInfo map.
424 if (isDefinition && !CurFun.BBForwardRefs.erase(BB))
425 // The existing value was a definition, not a forward reference.
Reid Spencer61c83e02006-08-18 08:43:06 +0000426 GenerateError("Redefinition of label " + ID.getName());
Chris Lattner64116f92004-07-14 01:33:11 +0000427
Chris Lattner2e2ed292004-07-14 06:28:35 +0000428 ID.destroy(); // Free strdup'd memory.
429 return BB;
430 }
431
432 // Otherwise this block has not been seen before.
433 BB = new BasicBlock("", CurFun.CurrentFunction);
434 if (ID.Type == ValID::NameVal) {
435 BB->setName(ID.Name);
436 } else {
437 CurFun.NumberedBlocks[ID.Num] = BB;
438 }
439
440 // If this is not a definition, keep track of it so we can use it as a forward
441 // reference.
442 if (!isDefinition) {
443 // Remember where this forward reference came from.
444 CurFun.BBForwardRefs[BB] = std::make_pair(ID, llvmAsmlineno);
445 } else {
446 // The forward declaration could have been inserted anywhere in the
447 // function: insert it into the correct place now.
448 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
449 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
450 }
Chris Lattnere8343a52004-10-26 18:26:14 +0000451 ID.destroy();
Chris Lattner64116f92004-07-14 01:33:11 +0000452 return BB;
453}
454
Chris Lattner00950542001-06-06 20:29:01 +0000455
456//===----------------------------------------------------------------------===//
457// Code to handle forward references in instructions
458//===----------------------------------------------------------------------===//
459//
460// This code handles the late binding needed with statements that reference
461// values not defined yet... for example, a forward branch, or the PHI node for
462// a loop body.
463//
Chris Lattner394f2eb2003-10-16 20:12:13 +0000464// This keeps a table (CurFun.LateResolveValues) of all such forward references
Chris Lattner00950542001-06-06 20:29:01 +0000465// and back patchs after we are done.
466//
467
Misha Brukman5a2a3822005-05-10 22:02:28 +0000468// ResolveDefinitions - If we could not resolve some defs at parsing
469// time (forward branches, phi functions for loops, etc...) resolve the
Chris Lattner00950542001-06-06 20:29:01 +0000470// defs now...
471//
Misha Brukman5a2a3822005-05-10 22:02:28 +0000472static void
473ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers,
474 std::map<const Type*,ValueList> *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000475 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Chris Lattner735f2702004-07-08 22:30:50 +0000476 for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(),
Chris Lattner16af11d2004-02-09 21:03:38 +0000477 E = LateResolvers.end(); LRI != E; ++LRI) {
478 ValueList &List = LRI->second;
479 while (!List.empty()) {
480 Value *V = List.back();
481 List.pop_back();
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000482
483 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
484 CurModule.PlaceHolderInfo.find(V);
485 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
486
487 ValID &DID = PHI->second.first;
Chris Lattner00950542001-06-06 20:29:01 +0000488
Chris Lattner735f2702004-07-08 22:30:50 +0000489 Value *TheRealValue = getValNonImprovising(LRI->first, DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000490 if (TheRealValue) {
491 V->replaceAllUsesWith(TheRealValue);
492 delete V;
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000493 CurModule.PlaceHolderInfo.erase(PHI);
Chris Lattner386a3b72001-10-16 19:54:17 +0000494 } else if (FutureLateResolvers) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000495 // Functions have their unresolved items forwarded to the module late
Chris Lattner386a3b72001-10-16 19:54:17 +0000496 // resolver table
497 InsertValue(V, *FutureLateResolvers);
498 } else {
Reid Spencer3271ed52004-07-18 00:08:11 +0000499 if (DID.Type == ValID::NameVal)
Reid Spencer61c83e02006-08-18 08:43:06 +0000500 GenerateError("Reference to an invalid definition: '" +DID.getName()+
Reid Spencer3271ed52004-07-18 00:08:11 +0000501 "' of type '" + V->getType()->getDescription() + "'",
502 PHI->second.second);
503 else
Reid Spencer61c83e02006-08-18 08:43:06 +0000504 GenerateError("Reference to an invalid definition: #" +
Misha Brukman5a2a3822005-05-10 22:02:28 +0000505 itostr(DID.Num) + " of type '" +
Reid Spencer3271ed52004-07-18 00:08:11 +0000506 V->getType()->getDescription() + "'",
507 PHI->second.second);
Chris Lattner30c89792001-09-07 16:35:17 +0000508 }
Chris Lattner00950542001-06-06 20:29:01 +0000509 }
510 }
511
512 LateResolvers.clear();
513}
514
Chris Lattner4a42e902001-10-22 05:56:09 +0000515// ResolveTypeTo - A brand new type was just declared. This means that (if
516// name is not null) things referencing Name can be resolved. Otherwise, things
517// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000518//
Chris Lattner4a42e902001-10-22 05:56:09 +0000519static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner355ad1f2005-03-21 06:27:42 +0000520 ValID D;
521 if (Name) D = ValID::create(Name);
522 else D = ValID::create((int)CurModule.Types.size());
Chris Lattner00950542001-06-06 20:29:01 +0000523
Chris Lattner355ad1f2005-03-21 06:27:42 +0000524 std::map<ValID, PATypeHolder>::iterator I =
525 CurModule.LateResolveTypes.find(D);
526 if (I != CurModule.LateResolveTypes.end()) {
527 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
528 CurModule.LateResolveTypes.erase(I);
Chris Lattner30c89792001-09-07 16:35:17 +0000529 }
530}
531
Chris Lattner1781aca2001-09-18 04:00:54 +0000532// setValueName - Set the specified value to the name given. The name may be
533// null potentially, in which case this is a noop. The string passed in is
Chris Lattner8ab406d2004-07-13 07:59:27 +0000534// assumed to be a malloc'd string buffer, and is free'd by this function.
535//
536static void setValueName(Value *V, char *NameStr) {
537 if (NameStr) {
538 std::string Name(NameStr); // Copy string
539 free(NameStr); // Free old string
Chris Lattnerc9aea522004-07-13 08:12:39 +0000540
Misha Brukman5a2a3822005-05-10 22:02:28 +0000541 if (V->getType() == Type::VoidTy)
Reid Spencer61c83e02006-08-18 08:43:06 +0000542 GenerateError("Can't assign name '" + Name+"' to value with void type!");
Misha Brukman5a2a3822005-05-10 22:02:28 +0000543
Chris Lattner8ab406d2004-07-13 07:59:27 +0000544 assert(inFunctionScope() && "Must be in function scope!");
545 SymbolTable &ST = CurFun.CurrentFunction->getSymbolTable();
546 if (ST.lookup(V->getType(), Name))
Reid Spencer61c83e02006-08-18 08:43:06 +0000547 GenerateError("Redefinition of value named '" + Name + "' in the '" +
Chris Lattner8ab406d2004-07-13 07:59:27 +0000548 V->getType()->getDescription() + "' type plane!");
Misha Brukman5a2a3822005-05-10 22:02:28 +0000549
Chris Lattnerc9aea522004-07-13 08:12:39 +0000550 // Set the name.
Chris Lattner262bb9a2005-03-05 19:04:07 +0000551 V->setName(Name);
Chris Lattner8ab406d2004-07-13 07:59:27 +0000552 }
553}
554
Chris Lattnerd88998d2004-07-14 08:23:52 +0000555/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
556/// this is a declaration, otherwise it is a definition.
Chris Lattnerb2b96672005-11-12 18:21:21 +0000557static GlobalVariable *
558ParseGlobalVariable(char *NameStr,GlobalValue::LinkageTypes Linkage,
559 bool isConstantGlobal, const Type *Ty,
560 Constant *Initializer) {
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000561 if (isa<FunctionType>(Ty))
Reid Spencer61c83e02006-08-18 08:43:06 +0000562 GenerateError("Cannot declare global vars of function type!");
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000563
Misha Brukman5a2a3822005-05-10 22:02:28 +0000564 const PointerType *PTy = PointerType::get(Ty);
Chris Lattnera09000d2004-07-14 23:03:46 +0000565
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000566 std::string Name;
567 if (NameStr) {
568 Name = NameStr; // Copy string
569 free(NameStr); // Free old string
Chris Lattnerd88998d2004-07-14 08:23:52 +0000570 }
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000571
Chris Lattner8a327842004-07-14 21:44:00 +0000572 // See if this global value was forward referenced. If so, recycle the
573 // object.
Misha Brukman5a2a3822005-05-10 22:02:28 +0000574 ValID ID;
Chris Lattner8a327842004-07-14 21:44:00 +0000575 if (!Name.empty()) {
576 ID = ValID::create((char*)Name.c_str());
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000577 } else {
Chris Lattner8a327842004-07-14 21:44:00 +0000578 ID = ValID::create((int)CurModule.Values[PTy].size());
579 }
580
581 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000582 // Move the global to the end of the list, from whereever it was
Chris Lattner8a327842004-07-14 21:44:00 +0000583 // previously inserted.
584 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
585 CurModule.CurrentModule->getGlobalList().remove(GV);
586 CurModule.CurrentModule->getGlobalList().push_back(GV);
587 GV->setInitializer(Initializer);
588 GV->setLinkage(Linkage);
589 GV->setConstant(isConstantGlobal);
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000590 InsertValue(GV, CurModule.Values);
Chris Lattnerb2b96672005-11-12 18:21:21 +0000591 return GV;
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000592 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000593
594 // If this global has a name, check to see if there is already a definition
595 // of this global in the module. If so, merge as appropriate. Note that
596 // this is really just a hack around problems in the CFE. :(
597 if (!Name.empty()) {
598 // We are a simple redefinition of a value, check to see if it is defined
599 // the same as the old one.
Misha Brukman5a2a3822005-05-10 22:02:28 +0000600 if (GlobalVariable *EGV =
Chris Lattnera09000d2004-07-14 23:03:46 +0000601 CurModule.CurrentModule->getGlobalVariable(Name, Ty)) {
602 // We are allowed to redefine a global variable in two circumstances:
Misha Brukman5a2a3822005-05-10 22:02:28 +0000603 // 1. If at least one of the globals is uninitialized or
Chris Lattnera09000d2004-07-14 23:03:46 +0000604 // 2. If both initializers have the same value.
605 //
606 if (!EGV->hasInitializer() || !Initializer ||
607 EGV->getInitializer() == Initializer) {
608
609 // Make sure the existing global version gets the initializer! Make
610 // sure that it also gets marked const if the new version is.
611 if (Initializer && !EGV->hasInitializer())
612 EGV->setInitializer(Initializer);
613 if (isConstantGlobal)
614 EGV->setConstant(true);
615 EGV->setLinkage(Linkage);
Chris Lattnerb2b96672005-11-12 18:21:21 +0000616 return EGV;
Chris Lattnera09000d2004-07-14 23:03:46 +0000617 }
618
Reid Spencer61c83e02006-08-18 08:43:06 +0000619 GenerateError("Redefinition of global variable named '" + Name +
Chris Lattnera09000d2004-07-14 23:03:46 +0000620 "' in the '" + Ty->getDescription() + "' type plane!");
621 }
622 }
623
624 // Otherwise there is no existing GV to use, create one now.
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000625 GlobalVariable *GV =
Misha Brukman5a2a3822005-05-10 22:02:28 +0000626 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000627 CurModule.CurrentModule);
628 InsertValue(GV, CurModule.Values);
Chris Lattnerb2b96672005-11-12 18:21:21 +0000629 return GV;
Chris Lattnerd88998d2004-07-14 08:23:52 +0000630}
Chris Lattner8ab406d2004-07-13 07:59:27 +0000631
Reid Spencer75719bf2004-05-26 21:48:31 +0000632// setTypeName - Set the specified type to the name given. The name may be
633// null potentially, in which case this is a noop. The string passed in is
634// assumed to be a malloc'd string buffer, and is freed by this function.
635//
636// This function returns true if the type has already been defined, but is
637// allowed to be redefined in the specified context. If the name is a new name
638// for the type plane, it is inserted and false is returned.
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000639static bool setTypeName(const Type *T, char *NameStr) {
Chris Lattnera09000d2004-07-14 23:03:46 +0000640 assert(!inFunctionScope() && "Can't give types function-local names!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000641 if (NameStr == 0) return false;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000642
Reid Spencer75719bf2004-05-26 21:48:31 +0000643 std::string Name(NameStr); // Copy string
644 free(NameStr); // Free old string
645
646 // We don't allow assigning names to void type
Misha Brukman5a2a3822005-05-10 22:02:28 +0000647 if (T == Type::VoidTy)
Reid Spencer61c83e02006-08-18 08:43:06 +0000648 GenerateError("Can't assign name '" + Name + "' to the void type!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000649
Chris Lattnera09000d2004-07-14 23:03:46 +0000650 // Set the type name, checking for conflicts as we do so.
651 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000652
Chris Lattnera09000d2004-07-14 23:03:46 +0000653 if (AlreadyExists) { // Inserting a name that is already defined???
654 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
655 assert(Existing && "Conflict but no matching type?");
656
Reid Spencer75719bf2004-05-26 21:48:31 +0000657 // There is only one case where this is allowed: when we are refining an
658 // opaque type. In this case, Existing will be an opaque type.
659 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
660 // We ARE replacing an opaque type!
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000661 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000662 return true;
663 }
664
665 // Otherwise, this is an attempt to redefine a type. That's okay if
666 // the redefinition is identical to the original. This will be so if
667 // Existing and T point to the same Type object. In this one case we
668 // allow the equivalent redefinition.
669 if (Existing == T) return true; // Yes, it's equal.
670
671 // Any other kind of (non-equivalent) redefinition is an error.
Reid Spencer61c83e02006-08-18 08:43:06 +0000672 GenerateError("Redefinition of type named '" + Name + "' in the '" +
Reid Spencer3271ed52004-07-18 00:08:11 +0000673 T->getDescription() + "' type plane!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000674 }
675
Reid Spencer75719bf2004-05-26 21:48:31 +0000676 return false;
677}
Chris Lattner8896eda2001-07-09 19:38:36 +0000678
Chris Lattner30c89792001-09-07 16:35:17 +0000679//===----------------------------------------------------------------------===//
680// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000681//
Chris Lattner8896eda2001-07-09 19:38:36 +0000682
Chris Lattner3b073862004-02-09 03:19:29 +0000683// TypeContains - Returns true if Ty directly contains E in it.
Chris Lattner30c89792001-09-07 16:35:17 +0000684//
685static bool TypeContains(const Type *Ty, const Type *E) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000686 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
Chris Lattner6d8dbec2004-12-08 16:13:53 +0000687 E) != Ty->subtype_end();
Chris Lattner3b073862004-02-09 03:19:29 +0000688}
689
690namespace {
691 struct UpRefRecord {
692 // NestingLevel - The number of nesting levels that need to be popped before
693 // this type is resolved.
694 unsigned NestingLevel;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000695
Chris Lattner3b073862004-02-09 03:19:29 +0000696 // LastContainedTy - This is the type at the current binding level for the
697 // type. Every time we reduce the nesting level, this gets updated.
698 const Type *LastContainedTy;
699
700 // UpRefTy - This is the actual opaque type that the upreference is
701 // represented with.
702 OpaqueType *UpRefTy;
703
704 UpRefRecord(unsigned NL, OpaqueType *URTy)
705 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
706 };
Chris Lattner30c89792001-09-07 16:35:17 +0000707}
Chris Lattner698b56e2001-07-20 19:15:08 +0000708
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000709// UpRefs - A list of the outstanding upreferences that need to be resolved.
Chris Lattner3b073862004-02-09 03:19:29 +0000710static std::vector<UpRefRecord> UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000711
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000712/// HandleUpRefs - Every time we finish a new layer of types, this function is
713/// called. It loops through the UpRefs vector, which is a list of the
714/// currently active types. For each type, if the up reference is contained in
715/// the newly completed type, we decrement the level count. When the level
716/// count reaches zero, the upreferenced type is the type that is passed in:
717/// thus we can complete the cycle.
718///
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000719static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner703e92f2006-08-18 17:34:24 +0000720 // If Ty isn't abstract, or if there are no up-references in it, then there is
721 // nothing to resolve here.
722 if (!ty->isAbstract() || UpRefs.empty()) return ty;
723
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000724 PATypeHolder Ty(ty);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000725 UR_OUT("Type '" << Ty->getDescription() <<
Chris Lattner5084d032001-11-02 07:46:26 +0000726 "' newly formed. Resolving upreferences.\n" <<
727 UpRefs.size() << " upreferences active!\n");
Chris Lattner026a8ce2004-02-09 18:53:54 +0000728
729 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
730 // to zero), we resolve them all together before we resolve them to Ty. At
731 // the end of the loop, if there is anything to resolve to Ty, it will be in
732 // this variable.
733 OpaqueType *TypeToResolve = 0;
734
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000735 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000736 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
737 << UpRefs[i].second->getDescription() << ") = "
Reid Spencer3271ed52004-07-18 00:08:11 +0000738 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner3b073862004-02-09 03:19:29 +0000739 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
740 // Decrement level of upreference
741 unsigned Level = --UpRefs[i].NestingLevel;
742 UpRefs[i].LastContainedTy = Ty;
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000743 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Misha Brukman5a2a3822005-05-10 22:02:28 +0000744 if (Level == 0) { // Upreference should be resolved!
Chris Lattner026a8ce2004-02-09 18:53:54 +0000745 if (!TypeToResolve) {
746 TypeToResolve = UpRefs[i].UpRefTy;
747 } else {
748 UR_OUT(" * Resolving upreference for "
749 << UpRefs[i].second->getDescription() << "\n";
750 std::string OldName = UpRefs[i].UpRefTy->getDescription());
751 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
752 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
753 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
754 }
Reid Spencer3271ed52004-07-18 00:08:11 +0000755 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000756 --i; // Do not skip the next element...
Chris Lattner30c89792001-09-07 16:35:17 +0000757 }
758 }
Chris Lattner8896eda2001-07-09 19:38:36 +0000759 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000760
Chris Lattner026a8ce2004-02-09 18:53:54 +0000761 if (TypeToResolve) {
762 UR_OUT(" * Resolving upreference for "
763 << UpRefs[i].second->getDescription() << "\n";
Chris Lattner16af11d2004-02-09 21:03:38 +0000764 std::string OldName = TypeToResolve->getDescription());
Chris Lattner026a8ce2004-02-09 18:53:54 +0000765 TypeToResolve->refineAbstractTypeTo(Ty);
766 }
767
Chris Lattner8896eda2001-07-09 19:38:36 +0000768 return Ty;
769}
770
Chris Lattner30c89792001-09-07 16:35:17 +0000771
Chris Lattner6184feb2005-05-20 03:25:47 +0000772// common code from the two 'RunVMAsmParser' functions
773 static Module * RunParser(Module * M) {
774
775 llvmAsmlineno = 1; // Reset the current line number...
Andrew Lenharth558bc882005-06-18 18:34:52 +0000776 ObsoleteVarArgs = false;
Andrew Lenharthe78f50d2005-06-19 03:53:56 +0000777 NewVarArgs = false;
Chris Lattner6184feb2005-05-20 03:25:47 +0000778
779 CurModule.CurrentModule = M;
780 yyparse(); // Parse the file, potentially throwing exception
Reid Spencer61c83e02006-08-18 08:43:06 +0000781 if (!ParserResult)
782 return 0;
Chris Lattner6184feb2005-05-20 03:25:47 +0000783
784 Module *Result = ParserResult;
785 ParserResult = 0;
786
Andrew Lenharth31c98bf2005-06-20 15:41:37 +0000787 //Not all functions use vaarg, so make a second check for ObsoleteVarArgs
788 {
789 Function* F;
790 if ((F = Result->getNamedFunction("llvm.va_start"))
791 && F->getFunctionType()->getNumParams() == 0)
792 ObsoleteVarArgs = true;
793 if((F = Result->getNamedFunction("llvm.va_copy"))
794 && F->getFunctionType()->getNumParams() == 1)
795 ObsoleteVarArgs = true;
796 }
797
Andrew Lenharthe78f50d2005-06-19 03:53:56 +0000798 if (ObsoleteVarArgs && NewVarArgs)
Reid Spencer61c83e02006-08-18 08:43:06 +0000799 GenerateError("This file is corrupt: it uses both new and old style varargs");
Andrew Lenharthe78f50d2005-06-19 03:53:56 +0000800
Andrew Lenharth558bc882005-06-18 18:34:52 +0000801 if(ObsoleteVarArgs) {
802 if(Function* F = Result->getNamedFunction("llvm.va_start")) {
Andrew Lenharth213e5572005-06-22 21:04:42 +0000803 if (F->arg_size() != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +0000804 GenerateError("Obsolete va_start takes 0 argument!");
Andrew Lenharth558bc882005-06-18 18:34:52 +0000805
806 //foo = va_start()
807 // ->
808 //bar = alloca typeof(foo)
809 //va_start(bar)
810 //foo = load bar
811
812 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
813 const Type* ArgTy = F->getFunctionType()->getReturnType();
814 const Type* ArgTyPtr = PointerType::get(ArgTy);
815 Function* NF = Result->getOrInsertFunction("llvm.va_start",
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000816 RetTy, ArgTyPtr, (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000817
818 while (!F->use_empty()) {
819 CallInst* CI = cast<CallInst>(F->use_back());
820 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vastart.fix.1", CI);
821 new CallInst(NF, bar, "", CI);
822 Value* foo = new LoadInst(bar, "vastart.fix.2", CI);
823 CI->replaceAllUsesWith(foo);
824 CI->getParent()->getInstList().erase(CI);
825 }
826 Result->getFunctionList().erase(F);
827 }
828
829 if(Function* F = Result->getNamedFunction("llvm.va_end")) {
Andrew Lenharth213e5572005-06-22 21:04:42 +0000830 if(F->arg_size() != 1)
Reid Spencer61c83e02006-08-18 08:43:06 +0000831 GenerateError("Obsolete va_end takes 1 argument!");
Andrew Lenharth213e5572005-06-22 21:04:42 +0000832
Andrew Lenharth558bc882005-06-18 18:34:52 +0000833 //vaend foo
834 // ->
835 //bar = alloca 1 of typeof(foo)
836 //vaend bar
837 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
838 const Type* ArgTy = F->getFunctionType()->getParamType(0);
839 const Type* ArgTyPtr = PointerType::get(ArgTy);
840 Function* NF = Result->getOrInsertFunction("llvm.va_end",
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000841 RetTy, ArgTyPtr, (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000842
843 while (!F->use_empty()) {
844 CallInst* CI = cast<CallInst>(F->use_back());
845 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vaend.fix.1", CI);
Andrew Lenharth017fba92005-06-19 14:04:55 +0000846 new StoreInst(CI->getOperand(1), bar, CI);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000847 new CallInst(NF, bar, "", CI);
848 CI->getParent()->getInstList().erase(CI);
849 }
850 Result->getFunctionList().erase(F);
851 }
852
853 if(Function* F = Result->getNamedFunction("llvm.va_copy")) {
Andrew Lenharth213e5572005-06-22 21:04:42 +0000854 if(F->arg_size() != 1)
Reid Spencer61c83e02006-08-18 08:43:06 +0000855 GenerateError("Obsolete va_copy takes 1 argument!");
Andrew Lenharth558bc882005-06-18 18:34:52 +0000856 //foo = vacopy(bar)
857 // ->
858 //a = alloca 1 of typeof(foo)
Andrew Lenharth213e5572005-06-22 21:04:42 +0000859 //b = alloca 1 of typeof(foo)
860 //store bar -> b
861 //vacopy(a, b)
Andrew Lenharth558bc882005-06-18 18:34:52 +0000862 //foo = load a
863
864 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
865 const Type* ArgTy = F->getFunctionType()->getReturnType();
866 const Type* ArgTyPtr = PointerType::get(ArgTy);
867 Function* NF = Result->getOrInsertFunction("llvm.va_copy",
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000868 RetTy, ArgTyPtr, ArgTyPtr,
869 (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000870
871 while (!F->use_empty()) {
872 CallInst* CI = cast<CallInst>(F->use_back());
873 AllocaInst* a = new AllocaInst(ArgTy, 0, "vacopy.fix.1", CI);
Andrew Lenharth213e5572005-06-22 21:04:42 +0000874 AllocaInst* b = new AllocaInst(ArgTy, 0, "vacopy.fix.2", CI);
875 new StoreInst(CI->getOperand(1), b, CI);
876 new CallInst(NF, a, b, "", CI);
877 Value* foo = new LoadInst(a, "vacopy.fix.3", CI);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000878 CI->replaceAllUsesWith(foo);
879 CI->getParent()->getInstList().erase(CI);
880 }
881 Result->getFunctionList().erase(F);
882 }
883 }
884
Chris Lattner6184feb2005-05-20 03:25:47 +0000885 return Result;
886
887 }
888
Chris Lattner00950542001-06-06 20:29:01 +0000889//===----------------------------------------------------------------------===//
890// RunVMAsmParser - Define an interface to this parser
891//===----------------------------------------------------------------------===//
892//
Chris Lattner86427632004-07-14 06:39:48 +0000893Module *llvm::RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner6184feb2005-05-20 03:25:47 +0000894 set_scan_file(F);
895
Chris Lattnera2850432001-07-22 18:36:00 +0000896 CurFilename = Filename;
Chris Lattner6184feb2005-05-20 03:25:47 +0000897 return RunParser(new Module(CurFilename));
898}
Chris Lattner00950542001-06-06 20:29:01 +0000899
Chris Lattner6184feb2005-05-20 03:25:47 +0000900Module *llvm::RunVMAsmParser(const char * AsmString, Module * M) {
901 set_scan_string(AsmString);
Chris Lattner42570982003-11-26 07:24:58 +0000902
Chris Lattner6184feb2005-05-20 03:25:47 +0000903 CurFilename = "from_memory";
904 if (M == NULL) {
905 return RunParser(new Module (CurFilename));
906 } else {
907 return RunParser(M);
908 }
Chris Lattner00950542001-06-06 20:29:01 +0000909}
910
911%}
912
913%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000914 llvm::Module *ModuleVal;
915 llvm::Function *FunctionVal;
916 std::pair<llvm::PATypeHolder*, char*> *ArgVal;
917 llvm::BasicBlock *BasicBlockVal;
918 llvm::TerminatorInst *TermInstVal;
919 llvm::Instruction *InstVal;
920 llvm::Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000921
Brian Gaeked0fde302003-11-11 22:41:34 +0000922 const llvm::Type *PrimType;
923 llvm::PATypeHolder *TypeVal;
924 llvm::Value *ValueVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000925
Brian Gaeked0fde302003-11-11 22:41:34 +0000926 std::vector<std::pair<llvm::PATypeHolder*,char*> > *ArgList;
927 std::vector<llvm::Value*> *ValueList;
928 std::list<llvm::PATypeHolder> *TypeList;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000929 // Represent the RHS of PHI node
Brian Gaeked0fde302003-11-11 22:41:34 +0000930 std::list<std::pair<llvm::Value*,
Misha Brukman5a2a3822005-05-10 22:02:28 +0000931 llvm::BasicBlock*> > *PHIList;
Brian Gaeked0fde302003-11-11 22:41:34 +0000932 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
933 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000934
Brian Gaeked0fde302003-11-11 22:41:34 +0000935 llvm::GlobalValue::LinkageTypes Linkage;
Chris Lattner30c89792001-09-07 16:35:17 +0000936 int64_t SInt64Val;
937 uint64_t UInt64Val;
938 int SIntVal;
939 unsigned UIntVal;
940 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000941 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000942
Chris Lattner30c89792001-09-07 16:35:17 +0000943 char *StrVal; // This memory is strdup'd!
Brian Gaeked0fde302003-11-11 22:41:34 +0000944 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000945
Brian Gaeked0fde302003-11-11 22:41:34 +0000946 llvm::Instruction::BinaryOps BinaryOpVal;
947 llvm::Instruction::TermOps TermOpVal;
948 llvm::Instruction::MemoryOps MemOpVal;
949 llvm::Instruction::OtherOps OtherOpVal;
950 llvm::Module::Endianness Endianness;
Chris Lattner00950542001-06-06 20:29:01 +0000951}
952
Chris Lattner79df7c02002-03-26 18:01:55 +0000953%type <ModuleVal> Module FunctionList
954%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000955%type <BasicBlockVal> BasicBlock InstructionList
956%type <TermInstVal> BBTerminatorInst
957%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000958%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000959%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000960%type <ArgList> ArgList ArgListH
961%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000962%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000963%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000964%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000965%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000966%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +0000967%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner15c9c032003-09-08 18:20:29 +0000968%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattnerddb6db42005-05-06 05:51:46 +0000969%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
Chris Lattneraa2c8532006-01-25 22:26:43 +0000970%type <BoolVal> OptSideEffect // 'sideeffect' or not.
Chris Lattner4ad02e72003-04-16 20:28:45 +0000971%type <Linkage> OptLinkage
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000972%type <Endianness> BigOrLittle
Chris Lattner00950542001-06-06 20:29:01 +0000973
Chris Lattner2079fde2001-10-13 06:41:08 +0000974// ValueRef - Unresolved reference to a definition or BB
975%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000976%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000977// Tokens and types for handling constant integer values
978//
979// ESINT64VAL - A negative number within long long range
980%token <SInt64Val> ESINT64VAL
981
982// EUINT64VAL - A positive number within uns. long long range
983%token <UInt64Val> EUINT64VAL
984%type <SInt64Val> EINT64VAL
985
986%token <SIntVal> SINTVAL // Signed 32 bit ints...
987%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
988%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000989%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000990
991// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000992%type <TypeVal> Types TypesV UpRTypes UpRTypesV
993%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000994%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
995%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000996
Chris Lattner6c23f572003-08-22 05:42:10 +0000997%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
998%type <StrVal> Name OptName OptAssign
Chris Lattner87ac9722005-11-06 06:46:28 +0000999%type <UIntVal> OptAlign OptCAlign
Chris Lattnerb2b96672005-11-12 18:21:21 +00001000%type <StrVal> OptSection SectionString
Chris Lattner00950542001-06-06 20:29:01 +00001001
Chris Lattner91ef4602004-03-31 03:49:47 +00001002%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Chris Lattner164c3782005-11-12 00:11:10 +00001003%token DECLARE GLOBAL CONSTANT SECTION VOLATILE
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001004%token TO DOTDOTDOT NULL_TOK UNDEF CONST INTERNAL LINKONCE WEAK APPENDING
1005%token DLLIMPORT DLLEXPORT EXTERN_WEAK
Nate Begeman14b05292005-11-05 09:21:28 +00001006%token OPAQUE NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG ALIGN
Chris Lattneraa2c8532006-01-25 22:26:43 +00001007%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
Chris Lattner515906d2006-05-19 21:28:34 +00001008%token CC_TOK CCC_TOK CSRETCC_TOK FASTCC_TOK COLDCC_TOK
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001009%token X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
Chris Lattnera8e8f162005-05-06 20:27:19 +00001010%type <UIntVal> OptCallingConv
Chris Lattner00950542001-06-06 20:29:01 +00001011
Misha Brukman5a2a3822005-05-10 22:02:28 +00001012// Basic Block Terminating Operators
Chris Lattner16710e92004-10-16 18:17:13 +00001013%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
Chris Lattner00950542001-06-06 20:29:01 +00001014
Misha Brukman5a2a3822005-05-10 22:02:28 +00001015// Binary Operators
Chris Lattner4a6482b2002-09-10 19:57:26 +00001016%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattner42c9e772001-10-20 09:32:59 +00001017%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +00001018%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +00001019
1020// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001021%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +00001022
Chris Lattner027dcc52001-07-08 21:10:27 +00001023// Other Operators
1024%type <OtherOpVal> ShiftOps
Robert Bocchino2def1b32006-01-17 20:06:25 +00001025%token <OtherOpVal> PHI_TOK CAST SELECT SHL SHR VAARG
Chris Lattner4c949082006-04-08 01:18:35 +00001026%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Andrew Lenharth558bc882005-06-18 18:34:52 +00001027%token VAARG_old VANEXT_old //OBSOLETE
Chris Lattnerddb6db42005-05-06 05:51:46 +00001028
Chris Lattner027dcc52001-07-08 21:10:27 +00001029
Chris Lattner00950542001-06-06 20:29:01 +00001030%start Module
1031%%
1032
1033// Handle constant integer size restriction and conversion...
1034//
Chris Lattner51727be2002-06-04 21:58:56 +00001035INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +00001036INTVAL : UINTVAL {
1037 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
Reid Spencer61c83e02006-08-18 08:43:06 +00001038 GEN_ERROR("Value too large for type!");
Chris Lattner00950542001-06-06 20:29:01 +00001039 $$ = (int32_t)$1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001040 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001041};
Chris Lattner00950542001-06-06 20:29:01 +00001042
1043
Chris Lattner51727be2002-06-04 21:58:56 +00001044EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +00001045EINT64VAL : EUINT64VAL {
1046 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
Reid Spencer61c83e02006-08-18 08:43:06 +00001047 GEN_ERROR("Value too large for type!");
Chris Lattner00950542001-06-06 20:29:01 +00001048 $$ = (int64_t)$1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001049 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001050};
Chris Lattner00950542001-06-06 20:29:01 +00001051
Misha Brukman5a2a3822005-05-10 22:02:28 +00001052// Operations that are notably excluded from this list include:
Chris Lattner00950542001-06-06 20:29:01 +00001053// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1054//
Chris Lattner4a6482b2002-09-10 19:57:26 +00001055ArithmeticOps: ADD | SUB | MUL | DIV | REM;
1056LogicalOps : AND | OR | XOR;
1057SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
Chris Lattner4a6482b2002-09-10 19:57:26 +00001058
Chris Lattner51727be2002-06-04 21:58:56 +00001059ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +00001060
Chris Lattnere98dda62001-07-14 06:10:16 +00001061// These are some types that allow classification if we only want a particular
1062// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +00001063SIntType : LONG | INT | SHORT | SBYTE;
1064UIntType : ULONG | UINT | USHORT | UBYTE;
1065IntType : SIntType | UIntType;
1066FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +00001067
Chris Lattnere98dda62001-07-14 06:10:16 +00001068// OptAssign - Value producing statements have an optional assignment component
Chris Lattner6c23f572003-08-22 05:42:10 +00001069OptAssign : Name '=' {
Chris Lattner00950542001-06-06 20:29:01 +00001070 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001071 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001072 }
Misha Brukman5a2a3822005-05-10 22:02:28 +00001073 | /*empty*/ {
1074 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00001075 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001076 };
Chris Lattner00950542001-06-06 20:29:01 +00001077
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001078OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
1079 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
1080 WEAK { $$ = GlobalValue::WeakLinkage; } |
1081 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
1082 DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; } |
1083 DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; } |
1084 EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; } |
1085 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattner30c89792001-09-07 16:35:17 +00001086
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001087OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
1088 CCC_TOK { $$ = CallingConv::C; } |
1089 CSRETCC_TOK { $$ = CallingConv::CSRet; } |
1090 FASTCC_TOK { $$ = CallingConv::Fast; } |
1091 COLDCC_TOK { $$ = CallingConv::Cold; } |
1092 X86_STDCALLCC_TOK { $$ = CallingConv::X86_StdCall; } |
1093 X86_FASTCALLCC_TOK { $$ = CallingConv::X86_FastCall; } |
1094 CC_TOK EUINT64VAL {
Chris Lattnera8e8f162005-05-06 20:27:19 +00001095 if ((unsigned)$2 != $2)
Reid Spencer61c83e02006-08-18 08:43:06 +00001096 GEN_ERROR("Calling conv too large!");
Chris Lattnera8e8f162005-05-06 20:27:19 +00001097 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001098 CHECK_FOR_ERROR
Chris Lattnera8e8f162005-05-06 20:27:19 +00001099 };
1100
Chris Lattner66db8e42005-11-06 06:34:12 +00001101// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1102// a comma before it.
Chris Lattner87ac9722005-11-06 06:46:28 +00001103OptAlign : /*empty*/ { $$ = 0; } |
Chris Lattnerb2b96672005-11-12 18:21:21 +00001104 ALIGN EUINT64VAL {
1105 $$ = $2;
1106 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencer61c83e02006-08-18 08:43:06 +00001107 GEN_ERROR("Alignment must be a power of two!");
1108 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001109};
Chris Lattner66db8e42005-11-06 06:34:12 +00001110OptCAlign : /*empty*/ { $$ = 0; } |
Chris Lattnerb2b96672005-11-12 18:21:21 +00001111 ',' ALIGN EUINT64VAL {
1112 $$ = $3;
1113 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencer61c83e02006-08-18 08:43:06 +00001114 GEN_ERROR("Alignment must be a power of two!");
1115 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001116};
1117
Chris Lattner66db8e42005-11-06 06:34:12 +00001118
Chris Lattner164c3782005-11-12 00:11:10 +00001119SectionString : SECTION STRINGCONSTANT {
1120 for (unsigned i = 0, e = strlen($2); i != e; ++i)
1121 if ($2[i] == '"' || $2[i] == '\\')
Reid Spencer61c83e02006-08-18 08:43:06 +00001122 GEN_ERROR("Invalid character in section name!");
Chris Lattner164c3782005-11-12 00:11:10 +00001123 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001124 CHECK_FOR_ERROR
Chris Lattner164c3782005-11-12 00:11:10 +00001125};
1126
1127OptSection : /*empty*/ { $$ = 0; } |
1128 SectionString { $$ = $1; };
Chris Lattner164c3782005-11-12 00:11:10 +00001129
Chris Lattnerb2b96672005-11-12 18:21:21 +00001130// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1131// is set to be the global we are processing.
1132//
1133GlobalVarAttributes : /* empty */ {} |
1134 ',' GlobalVarAttribute GlobalVarAttributes {};
1135GlobalVarAttribute : SectionString {
1136 CurGV->setSection($1);
1137 free($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001138 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001139 }
1140 | ALIGN EUINT64VAL {
1141 if ($2 != 0 && !isPowerOf2_32($2))
Reid Spencer61c83e02006-08-18 08:43:06 +00001142 GEN_ERROR("Alignment must be a power of two!");
Chris Lattnerb2b96672005-11-12 18:21:21 +00001143 CurGV->setAlignment($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001144 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001145 };
Chris Lattner164c3782005-11-12 00:11:10 +00001146
Chris Lattner30c89792001-09-07 16:35:17 +00001147//===----------------------------------------------------------------------===//
1148// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +00001149// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +00001150// access to it, a user must explicitly use TypesV.
1151//
1152
1153// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +00001154TypesV : Types | VOID { $$ = new PATypeHolder($1); };
1155UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +00001156
1157Types : UpRTypes {
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001158 if (!UpRefs.empty())
Reid Spencer61c83e02006-08-18 08:43:06 +00001159 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001160 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001161 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001162 };
Chris Lattner30c89792001-09-07 16:35:17 +00001163
1164
1165// Derived types are added later...
1166//
Chris Lattner51727be2002-06-04 21:58:56 +00001167PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
1168PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001169UpRTypes : OPAQUE {
1170 $$ = new PATypeHolder(OpaqueType::get());
Reid Spencer61c83e02006-08-18 08:43:06 +00001171 CHECK_FOR_ERROR
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001172 }
1173 | PrimType {
1174 $$ = new PATypeHolder($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001175 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001176 };
Chris Lattnerd78700d2002-08-16 21:14:40 +00001177UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001178 $$ = new PATypeHolder(getTypeVal($1));
Reid Spencer61c83e02006-08-18 08:43:06 +00001179 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001180};
Chris Lattner30c89792001-09-07 16:35:17 +00001181
Chris Lattner30c89792001-09-07 16:35:17 +00001182// Include derived types in the Types production.
1183//
1184UpRTypes : '\\' EUINT64VAL { // Type UpReference
Reid Spencer61c83e02006-08-18 08:43:06 +00001185 if ($2 > (uint64_t)~0U) GEN_ERROR("Value out of range!");
Chris Lattner30c89792001-09-07 16:35:17 +00001186 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3b073862004-02-09 03:19:29 +00001187 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001188 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +00001189 UR_OUT("New Upreference!\n");
Reid Spencer61c83e02006-08-18 08:43:06 +00001190 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001191 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001192 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner9232b992003-04-22 18:42:41 +00001193 std::vector<const Type*> Params;
Chris Lattnera08976b2005-02-22 23:13:58 +00001194 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1195 E = $3->end(); I != E; ++I)
1196 Params.push_back(*I);
Chris Lattner2079fde2001-10-13 06:41:08 +00001197 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1198 if (isVarArg) Params.pop_back();
1199
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001200 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +00001201 delete $3; // Delete the argument list
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001202 delete $1; // Delete the return type handle
Reid Spencer61c83e02006-08-18 08:43:06 +00001203 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001204 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001205 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001206 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001207 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00001208 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001209 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001210 | '<' EUINT64VAL 'x' UpRTypes '>' { // Packed array type?
1211 const llvm::Type* ElemTy = $4->get();
Chris Lattner9547d7f2005-11-10 01:42:43 +00001212 if ((unsigned)$2 != $2)
Reid Spencer61c83e02006-08-18 08:43:06 +00001213 GEN_ERROR("Unsigned result not equal to signed result");
Chris Lattner9547d7f2005-11-10 01:42:43 +00001214 if (!ElemTy->isPrimitiveType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001215 GEN_ERROR("Elemental type of a PackedType must be primitive");
Chris Lattner9547d7f2005-11-10 01:42:43 +00001216 if (!isPowerOf2_32($2))
Reid Spencer61c83e02006-08-18 08:43:06 +00001217 GEN_ERROR("Vector length should be a power of 2!");
Brian Gaeke715c90b2004-08-20 06:00:58 +00001218 $$ = new PATypeHolder(HandleUpRefs(PackedType::get(*$4, (unsigned)$2)));
1219 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00001220 CHECK_FOR_ERROR
Brian Gaeke715c90b2004-08-20 06:00:58 +00001221 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001222 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001223 std::vector<const Type*> Elements;
Chris Lattnera08976b2005-02-22 23:13:58 +00001224 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
1225 E = $2->end(); I != E; ++I)
1226 Elements.push_back(*I);
Misha Brukman5a2a3822005-05-10 22:02:28 +00001227
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001228 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001229 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001230 CHECK_FOR_ERROR
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001231 }
1232 | '{' '}' { // Empty structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001233 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Reid Spencer61c83e02006-08-18 08:43:06 +00001234 CHECK_FOR_ERROR
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001235 }
1236 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001237 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001238 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001239 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001240 };
Chris Lattner30c89792001-09-07 16:35:17 +00001241
Chris Lattner7e708292002-06-25 16:13:24 +00001242// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +00001243// declaration type lists
1244//
1245TypeListI : UpRTypes {
Chris Lattner9232b992003-04-22 18:42:41 +00001246 $$ = new std::list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +00001247 $$->push_back(*$1); delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001248 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001249 }
1250 | TypeListI ',' UpRTypes {
1251 ($$=$1)->push_back(*$3); delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001252 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001253 };
Chris Lattner30c89792001-09-07 16:35:17 +00001254
Chris Lattner7e708292002-06-25 16:13:24 +00001255// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +00001256ArgTypeListI : TypeListI
1257 | TypeListI ',' DOTDOTDOT {
1258 ($$=$1)->push_back(Type::VoidTy);
Reid Spencer61c83e02006-08-18 08:43:06 +00001259 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001260 }
1261 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001262 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
Reid Spencer61c83e02006-08-18 08:43:06 +00001263 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001264 }
1265 | /*empty*/ {
Chris Lattner9232b992003-04-22 18:42:41 +00001266 $$ = new std::list<PATypeHolder>();
Reid Spencer61c83e02006-08-18 08:43:06 +00001267 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001268 };
Chris Lattner30c89792001-09-07 16:35:17 +00001269
Chris Lattnere98dda62001-07-14 06:10:16 +00001270// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +00001271// production is used ONLY to represent constants that show up AFTER a 'const',
1272// 'constant' or 'global' token at global scope. Constants that can be inlined
1273// into other expressions (such as integers and constexprs) are handled by the
1274// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +00001275//
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001276ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Chris Lattner949a3622003-07-23 15:30:06 +00001277 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001278 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001279 GEN_ERROR("Cannot make array constant with type: '" +
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001280 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001281 const Type *ETy = ATy->getElementType();
1282 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +00001283
Chris Lattner30c89792001-09-07 16:35:17 +00001284 // Verify that we have the correct size...
1285 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001286 GEN_ERROR("Type mismatch: constant sized array initialized with " +
Reid Spencer3271ed52004-07-18 00:08:11 +00001287 utostr($3->size()) + " arguments, but has size of " +
1288 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +00001289
Chris Lattner30c89792001-09-07 16:35:17 +00001290 // Verify all elements are correct type!
1291 for (unsigned i = 0; i < $3->size(); i++) {
1292 if (ETy != (*$3)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001293 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Reid Spencer3271ed52004-07-18 00:08:11 +00001294 ETy->getDescription() +"' as required!\nIt is of type '"+
1295 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +00001296 }
1297
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001298 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001299 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001300 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001301 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001302 | Types '[' ']' {
Chris Lattner949a3622003-07-23 15:30:06 +00001303 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001304 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001305 GEN_ERROR("Cannot make array constant with type: '" +
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001306 (*$1)->getDescription() + "'!");
1307
1308 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +00001309 if (NumElements != -1 && NumElements != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001310 GEN_ERROR("Type mismatch: constant sized array initialized with 0"
Reid Spencer3271ed52004-07-18 00:08:11 +00001311 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattner9232b992003-04-22 18:42:41 +00001312 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +00001313 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001314 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001315 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001316 | Types 'c' STRINGCONSTANT {
Chris Lattner949a3622003-07-23 15:30:06 +00001317 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001318 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001319 GEN_ERROR("Cannot make array constant with type: '" +
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001320 (*$1)->getDescription() + "'!");
1321
Chris Lattner30c89792001-09-07 16:35:17 +00001322 int NumElements = ATy->getNumElements();
1323 const Type *ETy = ATy->getElementType();
1324 char *EndStr = UnEscapeLexed($3, true);
1325 if (NumElements != -1 && NumElements != (EndStr-$3))
Reid Spencer61c83e02006-08-18 08:43:06 +00001326 GEN_ERROR("Can't build string constant of size " +
Reid Spencer3271ed52004-07-18 00:08:11 +00001327 itostr((int)(EndStr-$3)) +
1328 " when array has size " + itostr(NumElements) + "!");
Chris Lattner9232b992003-04-22 18:42:41 +00001329 std::vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +00001330 if (ETy == Type::SByteTy) {
Chris Lattneree454772006-01-23 23:05:15 +00001331 for (signed char *C = (signed char *)$3; C != (signed char *)EndStr; ++C)
Reid Spencer3271ed52004-07-18 00:08:11 +00001332 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +00001333 } else if (ETy == Type::UByteTy) {
Chris Lattneree454772006-01-23 23:05:15 +00001334 for (unsigned char *C = (unsigned char *)$3;
1335 C != (unsigned char*)EndStr; ++C)
1336 Vals.push_back(ConstantUInt::get(ETy, *C));
Chris Lattner93750fa2001-07-28 17:48:55 +00001337 } else {
Chris Lattner30c89792001-09-07 16:35:17 +00001338 free($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00001339 GEN_ERROR("Cannot build string arrays of non byte sized elements!");
Chris Lattner93750fa2001-07-28 17:48:55 +00001340 }
Chris Lattner30c89792001-09-07 16:35:17 +00001341 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001342 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +00001343 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001344 CHECK_FOR_ERROR
Chris Lattner93750fa2001-07-28 17:48:55 +00001345 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001346 | Types '<' ConstVector '>' { // Nonempty unsized arr
1347 const PackedType *PTy = dyn_cast<PackedType>($1->get());
1348 if (PTy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001349 GEN_ERROR("Cannot make packed constant with type: '" +
Brian Gaeke715c90b2004-08-20 06:00:58 +00001350 (*$1)->getDescription() + "'!");
1351 const Type *ETy = PTy->getElementType();
1352 int NumElements = PTy->getNumElements();
1353
1354 // Verify that we have the correct size...
1355 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001356 GEN_ERROR("Type mismatch: constant sized packed initialized with " +
Brian Gaeke715c90b2004-08-20 06:00:58 +00001357 utostr($3->size()) + " arguments, but has size of " +
1358 itostr(NumElements) + "!");
1359
1360 // Verify all elements are correct type!
1361 for (unsigned i = 0; i < $3->size(); i++) {
1362 if (ETy != (*$3)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001363 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Brian Gaeke715c90b2004-08-20 06:00:58 +00001364 ETy->getDescription() +"' as required!\nIt is of type '"+
1365 (*$3)[i]->getType()->getDescription() + "'.");
1366 }
1367
1368 $$ = ConstantPacked::get(PTy, *$3);
1369 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001370 CHECK_FOR_ERROR
Brian Gaeke715c90b2004-08-20 06:00:58 +00001371 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001372 | Types '{' ConstVector '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001373 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001374 if (STy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001375 GEN_ERROR("Cannot make struct constant with type: '" +
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001376 (*$1)->getDescription() + "'!");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001377
Chris Lattnerc6212f12003-05-21 16:06:56 +00001378 if ($3->size() != STy->getNumContainedTypes())
Reid Spencer61c83e02006-08-18 08:43:06 +00001379 GEN_ERROR("Illegal number of initializers for structure type!");
Chris Lattnerc6212f12003-05-21 16:06:56 +00001380
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001381 // Check to ensure that constants are compatible with the type initializer!
1382 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Chris Lattnerd21cd802004-02-09 04:37:31 +00001383 if ((*$3)[i]->getType() != STy->getElementType(i))
Reid Spencer61c83e02006-08-18 08:43:06 +00001384 GEN_ERROR("Expected type '" +
Chris Lattnerd21cd802004-02-09 04:37:31 +00001385 STy->getElementType(i)->getDescription() +
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001386 "' for element #" + utostr(i) +
1387 " of structure initializer!");
1388
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001389 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001390 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001391 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001392 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001393 | Types '{' '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001394 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001395 if (STy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001396 GEN_ERROR("Cannot make struct constant with type: '" +
Chris Lattnerc6212f12003-05-21 16:06:56 +00001397 (*$1)->getDescription() + "'!");
1398
1399 if (STy->getNumContainedTypes() != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001400 GEN_ERROR("Illegal number of initializers for structure type!");
Chris Lattnerc6212f12003-05-21 16:06:56 +00001401
1402 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1403 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001404 CHECK_FOR_ERROR
Chris Lattnerc6212f12003-05-21 16:06:56 +00001405 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001406 | Types NULL_TOK {
Chris Lattner949a3622003-07-23 15:30:06 +00001407 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001408 if (PTy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001409 GEN_ERROR("Cannot make null pointer constant with type: '" +
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001410 (*$1)->getDescription() + "'!");
1411
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001412 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001413 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001414 CHECK_FOR_ERROR
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001415 }
Chris Lattner16710e92004-10-16 18:17:13 +00001416 | Types UNDEF {
1417 $$ = UndefValue::get($1->get());
1418 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001419 CHECK_FOR_ERROR
Chris Lattner16710e92004-10-16 18:17:13 +00001420 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001421 | Types SymbolicValueRef {
Chris Lattner949a3622003-07-23 15:30:06 +00001422 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001423 if (Ty == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001424 GEN_ERROR("Global const reference must be a pointer type!");
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001425
Chris Lattner3101c252002-08-15 17:58:33 +00001426 // ConstExprs can exist in the body of a function, thus creating
Reid Spencer3271ed52004-07-18 00:08:11 +00001427 // GlobalValues whenever they refer to a variable. Because we are in
Chris Lattner3101c252002-08-15 17:58:33 +00001428 // the context of a function, getValNonImprovising will search the functions
1429 // symbol table instead of the module symbol table for the global symbol,
1430 // which throws things all off. To get around this, we just tell
1431 // getValNonImprovising that we are at global scope here.
1432 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001433 Function *SavedCurFn = CurFun.CurrentFunction;
1434 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001435
Chris Lattner2079fde2001-10-13 06:41:08 +00001436 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001437
Chris Lattner394f2eb2003-10-16 20:12:13 +00001438 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001439
Chris Lattner2079fde2001-10-13 06:41:08 +00001440 // If this is an initializer for a constant pointer, which is referencing a
1441 // (currently) undefined variable, create a stub now that shall be replaced
1442 // in the future with the right type of variable.
1443 //
1444 if (V == 0) {
1445 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1446 const PointerType *PT = cast<PointerType>(Ty);
1447
1448 // First check to see if the forward references value is already created!
1449 PerModuleInfo::GlobalRefsType::iterator I =
Reid Spencer3271ed52004-07-18 00:08:11 +00001450 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001451
1452 if (I != CurModule.GlobalRefs.end()) {
Reid Spencer3271ed52004-07-18 00:08:11 +00001453 V = I->second; // Placeholder already exists, use it...
Chris Lattnera989b232003-12-23 20:05:15 +00001454 $2.destroy();
Chris Lattner2079fde2001-10-13 06:41:08 +00001455 } else {
Chris Lattner8a327842004-07-14 21:44:00 +00001456 std::string Name;
Chris Lattnera09000d2004-07-14 23:03:46 +00001457 if ($2.Type == ValID::NameVal) Name = $2.Name;
Chris Lattner8a327842004-07-14 21:44:00 +00001458
Reid Spencer3271ed52004-07-18 00:08:11 +00001459 // Create the forward referenced global.
Chris Lattnera09000d2004-07-14 23:03:46 +00001460 GlobalValue *GV;
1461 if (const FunctionType *FTy =
1462 dyn_cast<FunctionType>(PT->getElementType())) {
1463 GV = new Function(FTy, GlobalValue::ExternalLinkage, Name,
1464 CurModule.CurrentModule);
1465 } else {
Reid Spencer3271ed52004-07-18 00:08:11 +00001466 GV = new GlobalVariable(PT->getElementType(), false,
Chris Lattnera09000d2004-07-14 23:03:46 +00001467 GlobalValue::ExternalLinkage, 0,
1468 Name, CurModule.CurrentModule);
1469 }
Chris Lattner8a327842004-07-14 21:44:00 +00001470
Reid Spencer3271ed52004-07-18 00:08:11 +00001471 // Keep track of the fact that we have a forward ref to recycle it
1472 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1473 V = GV;
Chris Lattner2079fde2001-10-13 06:41:08 +00001474 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001475 }
1476
Reid Spencer3271ed52004-07-18 00:08:11 +00001477 $$ = cast<GlobalValue>(V);
Chris Lattner2079fde2001-10-13 06:41:08 +00001478 delete $1; // Free the type handle
Reid Spencer61c83e02006-08-18 08:43:06 +00001479 CHECK_FOR_ERROR
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001480 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001481 | Types ConstExpr {
1482 if ($1->get() != $2->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001483 GEN_ERROR("Mismatched types for constant expression!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001484 $$ = $2;
1485 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001486 CHECK_FOR_ERROR
Chris Lattnerf4600482003-06-28 20:01:34 +00001487 }
1488 | Types ZEROINITIALIZER {
Chris Lattner78915932004-11-28 16:45:45 +00001489 const Type *Ty = $1->get();
1490 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
Reid Spencer61c83e02006-08-18 08:43:06 +00001491 GEN_ERROR("Cannot create a null initialized value of this type!");
Chris Lattner78915932004-11-28 16:45:45 +00001492 $$ = Constant::getNullValue(Ty);
Chris Lattnerf4600482003-06-28 20:01:34 +00001493 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001494 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001495 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001496
Chris Lattnere43f40b2002-10-09 00:25:32 +00001497ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001498 if (!ConstantSInt::isValueValidForType($1, $2))
Reid Spencer61c83e02006-08-18 08:43:06 +00001499 GEN_ERROR("Constant value doesn't fit in type!");
Chris Lattnerd05e3592002-08-15 18:17:28 +00001500 $$ = ConstantSInt::get($1, $2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001501 CHECK_FOR_ERROR
Chris Lattnere43f40b2002-10-09 00:25:32 +00001502 }
1503 | UIntType EUINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001504 if (!ConstantUInt::isValueValidForType($1, $2))
Reid Spencer61c83e02006-08-18 08:43:06 +00001505 GEN_ERROR("Constant value doesn't fit in type!");
Chris Lattnerd05e3592002-08-15 18:17:28 +00001506 $$ = ConstantUInt::get($1, $2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001507 CHECK_FOR_ERROR
Chris Lattnere43f40b2002-10-09 00:25:32 +00001508 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001509 | BOOL TRUETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001510 $$ = ConstantBool::True;
Reid Spencer61c83e02006-08-18 08:43:06 +00001511 CHECK_FOR_ERROR
Chris Lattnerd05e3592002-08-15 18:17:28 +00001512 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001513 | BOOL FALSETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001514 $$ = ConstantBool::False;
Reid Spencer61c83e02006-08-18 08:43:06 +00001515 CHECK_FOR_ERROR
Chris Lattnerd05e3592002-08-15 18:17:28 +00001516 }
1517 | FPType FPVAL { // Float & Double constants
Reid Spencer9f9b3ac2004-12-06 22:18:25 +00001518 if (!ConstantFP::isValueValidForType($1, $2))
Reid Spencer61c83e02006-08-18 08:43:06 +00001519 GEN_ERROR("Floating point constant invalid for type!!");
Chris Lattnerd05e3592002-08-15 18:17:28 +00001520 $$ = ConstantFP::get($1, $2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001521 CHECK_FOR_ERROR
Chris Lattnerd05e3592002-08-15 18:17:28 +00001522 };
1523
Chris Lattner00950542001-06-06 20:29:01 +00001524
Chris Lattnerd78700d2002-08-16 21:14:40 +00001525ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattnerae711a82003-11-21 20:27:35 +00001526 if (!$3->getType()->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001527 GEN_ERROR("cast constant expression from a non-primitive type: '" +
Chris Lattnerae711a82003-11-21 20:27:35 +00001528 $3->getType()->getDescription() + "'!");
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001529 if (!$5->get()->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001530 GEN_ERROR("cast constant expression to a non-primitive type: '" +
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001531 $5->get()->getDescription() + "'!");
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001532 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001533 delete $5;
Reid Spencer61c83e02006-08-18 08:43:06 +00001534 CHECK_FOR_ERROR
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001535 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001536 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1537 if (!isa<PointerType>($3->getType()))
Reid Spencer61c83e02006-08-18 08:43:06 +00001538 GEN_ERROR("GetElementPtr requires a pointer operand!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001539
Chris Lattner830b6f92004-04-05 01:30:04 +00001540 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
1541 // indices to uint struct indices for compatibility.
1542 generic_gep_type_iterator<std::vector<Value*>::iterator>
1543 GTI = gep_type_begin($3->getType(), $4->begin(), $4->end()),
1544 GTE = gep_type_end($3->getType(), $4->begin(), $4->end());
1545 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
1546 if (isa<StructType>(*GTI)) // Only change struct indices
1547 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
1548 if (CUI->getType() == Type::UByteTy)
1549 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
1550
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001551 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001552 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001553 if (!IdxTy)
Reid Spencer61c83e02006-08-18 08:43:06 +00001554 GEN_ERROR("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001555
Chris Lattner9232b992003-04-22 18:42:41 +00001556 std::vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001557 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1558 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001559 IdxVec.push_back(C);
1560 else
Reid Spencer61c83e02006-08-18 08:43:06 +00001561 GEN_ERROR("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001562
Chris Lattnerd78700d2002-08-16 21:14:40 +00001563 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001564
Chris Lattnerd78700d2002-08-16 21:14:40 +00001565 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Reid Spencer61c83e02006-08-18 08:43:06 +00001566 CHECK_FOR_ERROR
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001567 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001568 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1569 if ($3->getType() != Type::BoolTy)
Reid Spencer61c83e02006-08-18 08:43:06 +00001570 GEN_ERROR("Select condition must be of boolean type!");
Chris Lattner76f0ff32004-03-12 05:51:36 +00001571 if ($5->getType() != $7->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001572 GEN_ERROR("Select operand types must match!");
Chris Lattner76f0ff32004-03-12 05:51:36 +00001573 $$ = ConstantExpr::getSelect($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001574 CHECK_FOR_ERROR
Chris Lattner76f0ff32004-03-12 05:51:36 +00001575 }
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001576 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001577 if ($3->getType() != $5->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001578 GEN_ERROR("Binary operator types must match!");
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001579 // HACK: llvm 1.3 and earlier used to emit invalid pointer constant exprs.
1580 // To retain backward compatibility with these early compilers, we emit a
1581 // cast to the appropriate integer type automatically if we are in the
1582 // broken case. See PR424 for more information.
1583 if (!isa<PointerType>($3->getType())) {
1584 $$ = ConstantExpr::get($1, $3, $5);
1585 } else {
Chris Lattner42db1a02004-08-20 18:07:39 +00001586 const Type *IntPtrTy = 0;
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001587 switch (CurModule.CurrentModule->getPointerSize()) {
1588 case Module::Pointer32: IntPtrTy = Type::IntTy; break;
1589 case Module::Pointer64: IntPtrTy = Type::LongTy; break;
Reid Spencer61c83e02006-08-18 08:43:06 +00001590 default: GEN_ERROR("invalid pointer binary constant expr!");
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001591 }
1592 $$ = ConstantExpr::get($1, ConstantExpr::getCast($3, IntPtrTy),
1593 ConstantExpr::getCast($5, IntPtrTy));
1594 $$ = ConstantExpr::getCast($$, $3->getType());
1595 }
Reid Spencer61c83e02006-08-18 08:43:06 +00001596 CHECK_FOR_ERROR
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001597 }
1598 | LogicalOps '(' ConstVal ',' ConstVal ')' {
1599 if ($3->getType() != $5->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001600 GEN_ERROR("Logical operator types must match!");
Chris Lattner0a017832005-12-21 18:31:29 +00001601 if (!$3->getType()->isIntegral()) {
1602 if (!isa<PackedType>($3->getType()) ||
1603 !cast<PackedType>($3->getType())->getElementType()->isIntegral())
Reid Spencer61c83e02006-08-18 08:43:06 +00001604 GEN_ERROR("Logical operator requires integral operands!");
Chris Lattner0a017832005-12-21 18:31:29 +00001605 }
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001606 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001607 CHECK_FOR_ERROR
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001608 }
1609 | SetCondOps '(' ConstVal ',' ConstVal ')' {
1610 if ($3->getType() != $5->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001611 GEN_ERROR("setcc operand types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001612 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001613 CHECK_FOR_ERROR
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001614 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001615 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001616 if ($5->getType() != Type::UByteTy)
Reid Spencer61c83e02006-08-18 08:43:06 +00001617 GEN_ERROR("Shift count for shift constant must be unsigned byte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001618 if (!$3->getType()->isInteger())
Reid Spencer61c83e02006-08-18 08:43:06 +00001619 GEN_ERROR("Shift constant expression requires integer operand!");
Chris Lattnerb16689b2004-01-12 19:08:43 +00001620 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001621 CHECK_FOR_ERROR
Robert Bocchino9c62b562006-01-10 19:04:32 +00001622 }
1623 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Chris Lattner2d7349a2006-04-08 04:08:32 +00001624 if (!ExtractElementInst::isValidOperands($3, $5))
Reid Spencer61c83e02006-08-18 08:43:06 +00001625 GEN_ERROR("Invalid extractelement operands!");
Robert Bocchino9c62b562006-01-10 19:04:32 +00001626 $$ = ConstantExpr::getExtractElement($3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001627 CHECK_FOR_ERROR
Chris Lattnerca73cd82006-04-08 03:53:34 +00001628 }
1629 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Chris Lattner2d7349a2006-04-08 04:08:32 +00001630 if (!InsertElementInst::isValidOperands($3, $5, $7))
Reid Spencer61c83e02006-08-18 08:43:06 +00001631 GEN_ERROR("Invalid insertelement operands!");
Chris Lattnerca73cd82006-04-08 03:53:34 +00001632 $$ = ConstantExpr::getInsertElement($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001633 CHECK_FOR_ERROR
Chris Lattnerca73cd82006-04-08 03:53:34 +00001634 }
1635 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1636 if (!ShuffleVectorInst::isValidOperands($3, $5, $7))
Reid Spencer61c83e02006-08-18 08:43:06 +00001637 GEN_ERROR("Invalid shufflevector operands!");
Chris Lattnerca73cd82006-04-08 03:53:34 +00001638 $$ = ConstantExpr::getShuffleVector($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001639 CHECK_FOR_ERROR
Chris Lattner699f1eb2002-08-14 17:12:33 +00001640 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001641
Chris Lattnerca73cd82006-04-08 03:53:34 +00001642
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001643// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001644ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001645 ($$ = $1)->push_back($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00001646 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001647 }
1648 | ConstVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001649 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001650 $$->push_back($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001651 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001652 };
Chris Lattner00950542001-06-06 20:29:01 +00001653
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001654
Chris Lattner1781aca2001-09-18 04:00:54 +00001655// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001656GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001657
Chris Lattner00950542001-06-06 20:29:01 +00001658
Chris Lattner0e73ce62002-05-02 19:11:13 +00001659//===----------------------------------------------------------------------===//
1660// Rules to match Modules
1661//===----------------------------------------------------------------------===//
1662
1663// Module rule: Capture the result of parsing the whole file into a result
1664// variable...
1665//
1666Module : FunctionList {
1667 $$ = ParserResult = $1;
1668 CurModule.ModuleDone();
Reid Spencer61c83e02006-08-18 08:43:06 +00001669 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001670};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001671
Chris Lattner7e708292002-06-25 16:13:24 +00001672// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001673//
1674FunctionList : FunctionList Function {
1675 $$ = $1;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001676 CurFun.FunctionDone();
Reid Spencer61c83e02006-08-18 08:43:06 +00001677 CHECK_FOR_ERROR
Chris Lattner0e73ce62002-05-02 19:11:13 +00001678 }
1679 | FunctionList FunctionProto {
1680 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001681 CHECK_FOR_ERROR
Chris Lattner0e73ce62002-05-02 19:11:13 +00001682 }
Chris Lattner71cdba32006-01-24 00:40:17 +00001683 | FunctionList MODULE ASM_TOK AsmBlock {
Chris Lattneree454772006-01-23 23:05:15 +00001684 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001685 CHECK_FOR_ERROR
Chris Lattneree454772006-01-23 23:05:15 +00001686 }
Chris Lattner0e73ce62002-05-02 19:11:13 +00001687 | FunctionList IMPLEMENTATION {
1688 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001689 CHECK_FOR_ERROR
Chris Lattner0e73ce62002-05-02 19:11:13 +00001690 }
1691 | ConstPool {
1692 $$ = CurModule.CurrentModule;
Chris Lattner355ad1f2005-03-21 06:27:42 +00001693 // Emit an error if there are any unresolved types left.
1694 if (!CurModule.LateResolveTypes.empty()) {
1695 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
Reid Spencer61c83e02006-08-18 08:43:06 +00001696 if (DID.Type == ValID::NameVal) {
1697 GEN_ERROR("Reference to an undefined type: '"+DID.getName() + "'");
1698 } else {
1699 GEN_ERROR("Reference to an undefined type: #" + itostr(DID.Num));
1700 }
Chris Lattner355ad1f2005-03-21 06:27:42 +00001701 }
Reid Spencer61c83e02006-08-18 08:43:06 +00001702 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001703 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001704
Chris Lattnere98dda62001-07-14 06:10:16 +00001705// ConstPool - Constants with optional names assigned to them.
Chris Lattner355ad1f2005-03-21 06:27:42 +00001706ConstPool : ConstPool OptAssign TYPE TypesV {
Chris Lattner4a42e902001-10-22 05:56:09 +00001707 // Eagerly resolve types. This is not an optimization, this is a
1708 // requirement that is due to the fact that we could have this:
1709 //
1710 // %list = type { %list * }
1711 // %list = type { %list * } ; repeated type decl
1712 //
1713 // If types are not resolved eagerly, then the two types will not be
1714 // determined to be the same type!
1715 //
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001716 ResolveTypeTo($2, *$4);
Chris Lattner4a42e902001-10-22 05:56:09 +00001717
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001718 if (!setTypeName(*$4, $2) && !$2) {
1719 // If this is a named type that is not a redefinition, add it to the slot
1720 // table.
Chris Lattner355ad1f2005-03-21 06:27:42 +00001721 CurModule.Types.push_back(*$4);
Chris Lattner30c89792001-09-07 16:35:17 +00001722 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001723
1724 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00001725 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001726 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001727 | ConstPool FunctionProto { // Function prototypes can be in const pool
Reid Spencer61c83e02006-08-18 08:43:06 +00001728 CHECK_FOR_ERROR
Chris Lattner93750fa2001-07-28 17:48:55 +00001729 }
Chris Lattner71cdba32006-01-24 00:40:17 +00001730 | ConstPool MODULE ASM_TOK AsmBlock { // Asm blocks can be in the const pool
Reid Spencer61c83e02006-08-18 08:43:06 +00001731 CHECK_FOR_ERROR
Chris Lattneree454772006-01-23 23:05:15 +00001732 }
Chris Lattnerb2b96672005-11-12 18:21:21 +00001733 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Reid Spencer61c83e02006-08-18 08:43:06 +00001734 if ($5 == 0) GEN_ERROR("Global value initializer is not a constant!");
Chris Lattnerb2b96672005-11-12 18:21:21 +00001735 CurGV = ParseGlobalVariable($2, $3, $4, $5->getType(), $5);
1736 } GlobalVarAttributes {
1737 CurGV = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00001738 CHECK_FOR_ERROR
Chris Lattner1781aca2001-09-18 04:00:54 +00001739 }
Chris Lattnerb2b96672005-11-12 18:21:21 +00001740 | ConstPool OptAssign EXTERNAL GlobalType Types {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001741 CurGV = ParseGlobalVariable($2,
1742 GlobalValue::ExternalLinkage, $4, *$5, 0);
1743 delete $5;
1744 } GlobalVarAttributes {
1745 CurGV = 0;
1746 CHECK_FOR_ERROR
1747 }
1748 | ConstPool OptAssign DLLIMPORT GlobalType Types {
1749 CurGV = ParseGlobalVariable($2,
1750 GlobalValue::DLLImportLinkage, $4, *$5, 0);
1751 delete $5;
1752 } GlobalVarAttributes {
1753 CurGV = 0;
1754 CHECK_FOR_ERROR
1755 }
1756 | ConstPool OptAssign EXTERN_WEAK GlobalType Types {
1757 CurGV = ParseGlobalVariable($2,
1758 GlobalValue::ExternalWeakLinkage, $4, *$5, 0);
Chris Lattner1f862af2003-04-16 18:13:57 +00001759 delete $5;
Chris Lattnerb2b96672005-11-12 18:21:21 +00001760 } GlobalVarAttributes {
1761 CurGV = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00001762 CHECK_FOR_ERROR
Chris Lattnere98dda62001-07-14 06:10:16 +00001763 }
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001764 | ConstPool TARGET TargetDefinition {
Reid Spencer61c83e02006-08-18 08:43:06 +00001765 CHECK_FOR_ERROR
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001766 }
Reid Spencer0be13e72004-07-25 17:58:28 +00001767 | ConstPool DEPLIBS '=' LibrariesDefinition {
Reid Spencer61c83e02006-08-18 08:43:06 +00001768 CHECK_FOR_ERROR
Reid Spencer0be13e72004-07-25 17:58:28 +00001769 }
Chris Lattner00950542001-06-06 20:29:01 +00001770 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001771 };
Chris Lattner00950542001-06-06 20:29:01 +00001772
1773
Chris Lattneree454772006-01-23 23:05:15 +00001774AsmBlock : STRINGCONSTANT {
Chris Lattner66316012006-01-24 04:14:29 +00001775 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
Chris Lattneree454772006-01-23 23:05:15 +00001776 char *EndStr = UnEscapeLexed($1, true);
1777 std::string NewAsm($1, EndStr);
1778 free($1);
1779
1780 if (AsmSoFar.empty())
Chris Lattner66316012006-01-24 04:14:29 +00001781 CurModule.CurrentModule->setModuleInlineAsm(NewAsm);
Chris Lattneree454772006-01-23 23:05:15 +00001782 else
Chris Lattner66316012006-01-24 04:14:29 +00001783 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+NewAsm);
Reid Spencer61c83e02006-08-18 08:43:06 +00001784 CHECK_FOR_ERROR
Chris Lattneree454772006-01-23 23:05:15 +00001785};
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001786
1787BigOrLittle : BIG { $$ = Module::BigEndian; };
1788BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1789
1790TargetDefinition : ENDIAN '=' BigOrLittle {
1791 CurModule.CurrentModule->setEndianness($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00001792 CHECK_FOR_ERROR
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001793 }
1794 | POINTERSIZE '=' EUINT64VAL {
1795 if ($3 == 32)
1796 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1797 else if ($3 == 64)
1798 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1799 else
Reid Spencer61c83e02006-08-18 08:43:06 +00001800 GEN_ERROR("Invalid pointer size: '" + utostr($3) + "'!");
1801 CHECK_FOR_ERROR
Reid Spencer0be13e72004-07-25 17:58:28 +00001802 }
1803 | TRIPLE '=' STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001804 CurModule.CurrentModule->setTargetTriple($3);
1805 free($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00001806 CHECK_FOR_ERROR
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001807 };
1808
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001809LibrariesDefinition : '[' LibList ']';
Reid Spencer0be13e72004-07-25 17:58:28 +00001810
1811LibList : LibList ',' STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001812 CurModule.CurrentModule->addLibrary($3);
1813 free($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00001814 CHECK_FOR_ERROR
Reid Spencer0be13e72004-07-25 17:58:28 +00001815 }
1816 | STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001817 CurModule.CurrentModule->addLibrary($1);
1818 free($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001819 CHECK_FOR_ERROR
Reid Spencer0be13e72004-07-25 17:58:28 +00001820 }
1821 | /* empty: end of list */ {
Reid Spencer61c83e02006-08-18 08:43:06 +00001822 CHECK_FOR_ERROR
Reid Spencer0be13e72004-07-25 17:58:28 +00001823 }
1824 ;
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001825
Chris Lattner00950542001-06-06 20:29:01 +00001826//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001827// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001828//===----------------------------------------------------------------------===//
1829
Chris Lattner6c23f572003-08-22 05:42:10 +00001830Name : VAR_ID | STRINGCONSTANT;
1831OptName : Name | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001832
Chris Lattner6c23f572003-08-22 05:42:10 +00001833ArgVal : Types OptName {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001834 if (*$1 == Type::VoidTy)
Reid Spencer61c83e02006-08-18 08:43:06 +00001835 GEN_ERROR("void typed arguments are invalid!");
Chris Lattner9232b992003-04-22 18:42:41 +00001836 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001837 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001838};
Chris Lattner00950542001-06-06 20:29:01 +00001839
Chris Lattner69da5cf2002-10-13 20:57:00 +00001840ArgListH : ArgListH ',' ArgVal {
1841 $$ = $1;
1842 $1->push_back(*$3);
1843 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001844 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001845 }
1846 | ArgVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001847 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
Chris Lattner69da5cf2002-10-13 20:57:00 +00001848 $$->push_back(*$1);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001849 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001850 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001851 };
Chris Lattner00950542001-06-06 20:29:01 +00001852
1853ArgList : ArgListH {
1854 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001855 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001856 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001857 | ArgListH ',' DOTDOTDOT {
1858 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001859 $$->push_back(std::pair<PATypeHolder*,
1860 char*>(new PATypeHolder(Type::VoidTy), 0));
Reid Spencer61c83e02006-08-18 08:43:06 +00001861 CHECK_FOR_ERROR
Chris Lattner69da5cf2002-10-13 20:57:00 +00001862 }
1863 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001864 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1865 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
Reid Spencer61c83e02006-08-18 08:43:06 +00001866 CHECK_FOR_ERROR
Chris Lattner69da5cf2002-10-13 20:57:00 +00001867 }
Chris Lattner00950542001-06-06 20:29:01 +00001868 | /* empty */ {
1869 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00001870 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001871 };
Chris Lattner00950542001-06-06 20:29:01 +00001872
Chris Lattner164c3782005-11-12 00:11:10 +00001873FunctionHeaderH : OptCallingConv TypesV Name '(' ArgList ')'
1874 OptSection OptAlign {
Chris Lattnera8e8f162005-05-06 20:27:19 +00001875 UnEscapeLexed($3);
1876 std::string FunctionName($3);
1877 free($3); // Free strdup'd memory!
Chris Lattnerdda71962001-11-26 18:54:16 +00001878
Chris Lattnera8e8f162005-05-06 20:27:19 +00001879 if (!(*$2)->isFirstClassType() && *$2 != Type::VoidTy)
Reid Spencer61c83e02006-08-18 08:43:06 +00001880 GEN_ERROR("LLVM functions cannot return aggregate types!");
Chris Lattnerc282f5a2003-11-21 22:32:23 +00001881
Chris Lattner9232b992003-04-22 18:42:41 +00001882 std::vector<const Type*> ParamTypeList;
Chris Lattnera8e8f162005-05-06 20:27:19 +00001883 if ($5) { // If there are arguments...
1884 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $5->begin();
1885 I != $5->end(); ++I)
Chris Lattner69da5cf2002-10-13 20:57:00 +00001886 ParamTypeList.push_back(I->first->get());
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001887 }
Chris Lattner00950542001-06-06 20:29:01 +00001888
Chris Lattner2079fde2001-10-13 06:41:08 +00001889 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1890 if (isVarArg) ParamTypeList.pop_back();
1891
Chris Lattnera8e8f162005-05-06 20:27:19 +00001892 const FunctionType *FT = FunctionType::get(*$2, ParamTypeList, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001893 const PointerType *PFT = PointerType::get(FT);
Chris Lattnera8e8f162005-05-06 20:27:19 +00001894 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001895
Chris Lattnera09000d2004-07-14 23:03:46 +00001896 ValID ID;
1897 if (!FunctionName.empty()) {
1898 ID = ValID::create((char*)FunctionName.c_str());
1899 } else {
1900 ID = ValID::create((int)CurModule.Values[PFT].size());
1901 }
1902
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001903 Function *Fn = 0;
Chris Lattnera09000d2004-07-14 23:03:46 +00001904 // See if this function was forward referenced. If so, recycle the object.
1905 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
1906 // Move the function to the end of the list, from whereever it was
1907 // previously inserted.
1908 Fn = cast<Function>(FWRef);
1909 CurModule.CurrentModule->getFunctionList().remove(Fn);
1910 CurModule.CurrentModule->getFunctionList().push_back(Fn);
1911 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
1912 (Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1913 // If this is the case, either we need to be a forward decl, or it needs
1914 // to be.
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001915 if (!CurFun.isDeclare && !Fn->isExternal())
Reid Spencer61c83e02006-08-18 08:43:06 +00001916 GEN_ERROR("Redefinition of function '" + FunctionName + "'!");
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001917
Chris Lattnera09000d2004-07-14 23:03:46 +00001918 // Make sure to strip off any argument names so we can't get conflicts.
1919 if (Fn->isExternal())
Chris Lattnere4d5c442005-03-15 04:54:21 +00001920 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
Chris Lattnera09000d2004-07-14 23:03:46 +00001921 AI != AE; ++AI)
1922 AI->setName("");
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001923 } else { // Not already defined?
1924 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
1925 CurModule.CurrentModule);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001926
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001927 InsertValue(Fn, CurModule.Values);
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001928 }
Chris Lattner00950542001-06-06 20:29:01 +00001929
Chris Lattner394f2eb2003-10-16 20:12:13 +00001930 CurFun.FunctionStart(Fn);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00001931
1932 if (CurFun.isDeclare) {
1933 // If we have declaration, always overwrite linkage. This will allow us to
1934 // correctly handle cases, when pointer to function is passed as argument to
1935 // another function.
1936 Fn->setLinkage(CurFun.Linkage);
1937 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00001938 Fn->setCallingConv($1);
Chris Lattner164c3782005-11-12 00:11:10 +00001939 Fn->setAlignment($8);
1940 if ($7) {
1941 Fn->setSection($7);
1942 free($7);
1943 }
Chris Lattner00950542001-06-06 20:29:01 +00001944
Chris Lattner7e708292002-06-25 16:13:24 +00001945 // Add all of the arguments we parsed to the function...
Chris Lattnera8e8f162005-05-06 20:27:19 +00001946 if ($5) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001947 if (isVarArg) { // Nuke the last entry
Chris Lattnera8e8f162005-05-06 20:27:19 +00001948 assert($5->back().first->get() == Type::VoidTy && $5->back().second == 0&&
Chris Lattner69da5cf2002-10-13 20:57:00 +00001949 "Not a varargs marker!");
Chris Lattnera8e8f162005-05-06 20:27:19 +00001950 delete $5->back().first;
1951 $5->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00001952 }
Chris Lattnere4d5c442005-03-15 04:54:21 +00001953 Function::arg_iterator ArgIt = Fn->arg_begin();
Chris Lattnera8e8f162005-05-06 20:27:19 +00001954 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $5->begin();
1955 I != $5->end(); ++I, ++ArgIt) {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001956 delete I->first; // Delete the typeholder...
1957
Chris Lattner8ab406d2004-07-13 07:59:27 +00001958 setValueName(ArgIt, I->second); // Insert arg into symtab...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001959 InsertValue(ArgIt);
Chris Lattner00950542001-06-06 20:29:01 +00001960 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001961
Chris Lattnera8e8f162005-05-06 20:27:19 +00001962 delete $5; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00001963 }
Reid Spencer61c83e02006-08-18 08:43:06 +00001964 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001965};
Chris Lattner00950542001-06-06 20:29:01 +00001966
Chris Lattner9b02cc32002-05-03 18:23:48 +00001967BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1968
Chris Lattner4ad02e72003-04-16 20:28:45 +00001969FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00001970 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001971
Chris Lattner4ad02e72003-04-16 20:28:45 +00001972 // Make sure that we keep track of the linkage type even if there was a
1973 // previous "declare".
1974 $$->setLinkage($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001975};
Chris Lattner00950542001-06-06 20:29:01 +00001976
Chris Lattner9b02cc32002-05-03 18:23:48 +00001977END : ENDTOK | '}'; // Allow end of '}' to end a function
1978
Chris Lattner79df7c02002-03-26 18:01:55 +00001979Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001980 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001981 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001982};
Chris Lattner00950542001-06-06 20:29:01 +00001983
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001984FnDeclareLinkage: /*default*/ |
1985 DLLIMPORT { CurFun.Linkage = GlobalValue::DLLImportLinkage } |
1986 EXTERN_WEAK { CurFun.Linkage = GlobalValue::DLLImportLinkage };
1987
1988FunctionProto : DECLARE { CurFun.isDeclare = true; } FnDeclareLinkage FunctionHeaderH {
1989 $$ = CurFun.CurrentFunction;
1990 CurFun.FunctionDone();
1991 CHECK_FOR_ERROR
1992 };
Chris Lattner00950542001-06-06 20:29:01 +00001993
1994//===----------------------------------------------------------------------===//
1995// Rules to match Basic Blocks
1996//===----------------------------------------------------------------------===//
1997
Chris Lattneraa2c8532006-01-25 22:26:43 +00001998OptSideEffect : /* empty */ {
1999 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002000 CHECK_FOR_ERROR
Chris Lattneraa2c8532006-01-25 22:26:43 +00002001 }
2002 | SIDEEFFECT {
2003 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002004 CHECK_FOR_ERROR
Chris Lattneraa2c8532006-01-25 22:26:43 +00002005 };
2006
Chris Lattner00950542001-06-06 20:29:01 +00002007ConstValueRef : ESINT64VAL { // A reference to a direct constant
2008 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002009 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002010 }
2011 | EUINT64VAL {
2012 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002013 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002014 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00002015 | FPVAL { // Perhaps it's an FP constant?
2016 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002017 CHECK_FOR_ERROR
Chris Lattner3d52b2f2001-07-15 00:17:01 +00002018 }
Chris Lattner91ef4602004-03-31 03:49:47 +00002019 | TRUETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00002020 $$ = ValID::create(ConstantBool::True);
Reid Spencer61c83e02006-08-18 08:43:06 +00002021 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002022 }
Chris Lattner91ef4602004-03-31 03:49:47 +00002023 | FALSETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00002024 $$ = ValID::create(ConstantBool::False);
Reid Spencer61c83e02006-08-18 08:43:06 +00002025 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002026 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00002027 | NULL_TOK {
2028 $$ = ValID::createNull();
Reid Spencer61c83e02006-08-18 08:43:06 +00002029 CHECK_FOR_ERROR
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002030 }
Chris Lattner16710e92004-10-16 18:17:13 +00002031 | UNDEF {
2032 $$ = ValID::createUndef();
Reid Spencer61c83e02006-08-18 08:43:06 +00002033 CHECK_FOR_ERROR
Chris Lattner16710e92004-10-16 18:17:13 +00002034 }
Chris Lattnerf1f03df2005-12-21 17:53:02 +00002035 | ZEROINITIALIZER { // A vector zero constant.
2036 $$ = ValID::createZeroInit();
Reid Spencer61c83e02006-08-18 08:43:06 +00002037 CHECK_FOR_ERROR
Chris Lattnerf1f03df2005-12-21 17:53:02 +00002038 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00002039 | '<' ConstVector '>' { // Nonempty unsized packed vector
2040 const Type *ETy = (*$2)[0]->getType();
2041 int NumElements = $2->size();
2042
2043 PackedType* pt = PackedType::get(ETy, NumElements);
2044 PATypeHolder* PTy = new PATypeHolder(
2045 HandleUpRefs(
2046 PackedType::get(
2047 ETy,
2048 NumElements)
2049 )
2050 );
2051
2052 // Verify all elements are correct type!
2053 for (unsigned i = 0; i < $2->size(); i++) {
2054 if (ETy != (*$2)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002055 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Brian Gaeke715c90b2004-08-20 06:00:58 +00002056 ETy->getDescription() +"' as required!\nIt is of type '" +
2057 (*$2)[i]->getType()->getDescription() + "'.");
2058 }
2059
2060 $$ = ValID::create(ConstantPacked::get(pt, *$2));
2061 delete PTy; delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002062 CHECK_FOR_ERROR
Brian Gaeke715c90b2004-08-20 06:00:58 +00002063 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00002064 | ConstExpr {
2065 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002066 CHECK_FOR_ERROR
Chris Lattneraa2c8532006-01-25 22:26:43 +00002067 }
2068 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
2069 char *End = UnEscapeLexed($3, true);
2070 std::string AsmStr = std::string($3, End);
2071 End = UnEscapeLexed($5, true);
2072 std::string Constraints = std::string($5, End);
2073 $$ = ValID::createInlineAsm(AsmStr, Constraints, $2);
2074 free($3);
2075 free($5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002076 CHECK_FOR_ERROR
Chris Lattnerd78700d2002-08-16 21:14:40 +00002077 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00002078
Chris Lattner2079fde2001-10-13 06:41:08 +00002079// SymbolicValueRef - Reference to one of two ways of symbolically refering to
2080// another value.
2081//
2082SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00002083 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002084 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002085 }
Chris Lattner6c23f572003-08-22 05:42:10 +00002086 | Name { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00002087 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002088 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002089 };
Chris Lattner2079fde2001-10-13 06:41:08 +00002090
2091// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00002092ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00002093
Chris Lattner00950542001-06-06 20:29:01 +00002094
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002095// ResolvedVal - a <type> <value> pair. This is used only in cases where the
2096// type immediately preceeds the value reference, and allows complex constant
2097// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00002098ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00002099 $$ = getVal(*$1, $2); delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002100 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002101 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00002102
Chris Lattner00950542001-06-06 20:29:01 +00002103BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner8ab406d2004-07-13 07:59:27 +00002104 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002105 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002106 }
Chris Lattner7e708292002-06-25 16:13:24 +00002107 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
Chris Lattner8ab406d2004-07-13 07:59:27 +00002108 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002109 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002110 };
Chris Lattner00950542001-06-06 20:29:01 +00002111
2112
2113// Basic blocks are terminated by branching instructions:
2114// br, br/cc, switch, ret
2115//
Chris Lattner2079fde2001-10-13 06:41:08 +00002116BasicBlock : InstructionList OptAssign BBTerminatorInst {
Chris Lattner8ab406d2004-07-13 07:59:27 +00002117 setValueName($3, $2);
Chris Lattner2079fde2001-10-13 06:41:08 +00002118 InsertValue($3);
2119
2120 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00002121 InsertValue($1);
2122 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002123 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002124 };
Chris Lattner00950542001-06-06 20:29:01 +00002125
2126InstructionList : InstructionList Inst {
2127 $1->getInstList().push_back($2);
2128 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002129 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002130 }
2131 | /* empty */ {
Andrew Lenharth558bc882005-06-18 18:34:52 +00002132 $$ = CurBB = getBBVal(ValID::create((int)CurFun.NextBBNum++), true);
Chris Lattner8d65a062004-07-26 01:40:20 +00002133
2134 // Make sure to move the basic block to the correct location in the
2135 // function, instead of leaving it inserted wherever it was first
2136 // referenced.
Chris Lattnerf924a4c2005-05-06 19:58:35 +00002137 Function::BasicBlockListType &BBL =
2138 CurFun.CurrentFunction->getBasicBlockList();
2139 BBL.splice(BBL.end(), BBL, $$);
Reid Spencer61c83e02006-08-18 08:43:06 +00002140 CHECK_FOR_ERROR
Chris Lattner22ae2322004-07-13 08:39:15 +00002141 }
2142 | LABELSTR {
Andrew Lenharth558bc882005-06-18 18:34:52 +00002143 $$ = CurBB = getBBVal(ValID::create($1), true);
Chris Lattner8d65a062004-07-26 01:40:20 +00002144
2145 // Make sure to move the basic block to the correct location in the
2146 // function, instead of leaving it inserted wherever it was first
2147 // referenced.
Chris Lattnerf924a4c2005-05-06 19:58:35 +00002148 Function::BasicBlockListType &BBL =
2149 CurFun.CurrentFunction->getBasicBlockList();
2150 BBL.splice(BBL.end(), BBL, $$);
Reid Spencer61c83e02006-08-18 08:43:06 +00002151 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002152 };
Chris Lattner00950542001-06-06 20:29:01 +00002153
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002154BBTerminatorInst : RET ResolvedVal { // Return with a result...
2155 $$ = new ReturnInst($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00002156 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002157 }
2158 | RET VOID { // Return with no result...
2159 $$ = new ReturnInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002160 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002161 }
2162 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner64116f92004-07-14 01:33:11 +00002163 $$ = new BranchInst(getBBVal($3));
Reid Spencer61c83e02006-08-18 08:43:06 +00002164 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002165 } // Conditional Branch...
2166 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner64116f92004-07-14 01:33:11 +00002167 $$ = new BranchInst(getBBVal($6), getBBVal($9), getVal(Type::BoolTy, $3));
Reid Spencer61c83e02006-08-18 08:43:06 +00002168 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002169 }
2170 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Chris Lattnerce1df9e2005-01-29 00:35:55 +00002171 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6), $8->size());
Chris Lattner00950542001-06-06 20:29:01 +00002172 $$ = S;
2173
Chris Lattner9232b992003-04-22 18:42:41 +00002174 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00002175 E = $8->end();
Chris Lattner69331f52005-02-24 05:25:17 +00002176 for (; I != E; ++I) {
2177 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
2178 S->addCase(CI, I->second);
2179 else
Reid Spencer61c83e02006-08-18 08:43:06 +00002180 GEN_ERROR("Switch case is constant, but not a simple integer!");
Chris Lattner69331f52005-02-24 05:25:17 +00002181 }
Chris Lattnerf57a43d2004-04-17 23:49:15 +00002182 delete $8;
Reid Spencer61c83e02006-08-18 08:43:06 +00002183 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002184 }
Chris Lattner196850c2003-05-15 21:30:00 +00002185 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Chris Lattnerce1df9e2005-01-29 00:35:55 +00002186 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6), 0);
Chris Lattner196850c2003-05-15 21:30:00 +00002187 $$ = S;
Reid Spencer61c83e02006-08-18 08:43:06 +00002188 CHECK_FOR_ERROR
Chris Lattner196850c2003-05-15 21:30:00 +00002189 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00002190 | INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
2191 TO LABEL ValueRef UNWIND LABEL ValueRef {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002192 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00002193 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00002194
Chris Lattnera8e8f162005-05-06 20:27:19 +00002195 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002196 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00002197 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00002198 std::vector<const Type*> ParamTypes;
Chris Lattnera8e8f162005-05-06 20:27:19 +00002199 if ($6) {
2200 for (std::vector<Value*>::iterator I = $6->begin(), E = $6->end();
Chris Lattner9232b992003-04-22 18:42:41 +00002201 I != E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00002202 ParamTypes.push_back((*I)->getType());
2203 }
2204
2205 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
2206 if (isVarArg) ParamTypes.pop_back();
2207
Chris Lattnera8e8f162005-05-06 20:27:19 +00002208 Ty = FunctionType::get($3->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002209 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00002210 }
Chris Lattner2079fde2001-10-13 06:41:08 +00002211
Chris Lattnera8e8f162005-05-06 20:27:19 +00002212 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00002213
Chris Lattnera8e8f162005-05-06 20:27:19 +00002214 BasicBlock *Normal = getBBVal($10);
2215 BasicBlock *Except = getBBVal($13);
Chris Lattner2079fde2001-10-13 06:41:08 +00002216
2217 // Create the call node...
Chris Lattnera8e8f162005-05-06 20:27:19 +00002218 if (!$6) { // Has no arguments?
Chris Lattner9232b992003-04-22 18:42:41 +00002219 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00002220 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00002221 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00002222 // correctly!
2223 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00002224 FunctionType::param_iterator I = Ty->param_begin();
2225 FunctionType::param_iterator E = Ty->param_end();
Chris Lattnera8e8f162005-05-06 20:27:19 +00002226 std::vector<Value*>::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00002227
2228 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
Reid Spencer3271ed52004-07-18 00:08:11 +00002229 if ((*ArgI)->getType() != *I)
Reid Spencer61c83e02006-08-18 08:43:06 +00002230 GEN_ERROR("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Reid Spencer3271ed52004-07-18 00:08:11 +00002231 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00002232
2233 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002234 GEN_ERROR("Invalid number of parameters detected!");
Chris Lattner2079fde2001-10-13 06:41:08 +00002235
Chris Lattnera8e8f162005-05-06 20:27:19 +00002236 $$ = new InvokeInst(V, Normal, Except, *$6);
Chris Lattner2079fde2001-10-13 06:41:08 +00002237 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00002238 cast<InvokeInst>($$)->setCallingConv($2);
2239
2240 delete $3;
2241 delete $6;
Reid Spencer61c83e02006-08-18 08:43:06 +00002242 CHECK_FOR_ERROR
Chris Lattner36143fc2003-09-08 18:54:55 +00002243 }
2244 | UNWIND {
2245 $$ = new UnwindInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002246 CHECK_FOR_ERROR
Chris Lattner16710e92004-10-16 18:17:13 +00002247 }
2248 | UNREACHABLE {
2249 $$ = new UnreachableInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002250 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002251 };
Chris Lattner2079fde2001-10-13 06:41:08 +00002252
2253
Chris Lattner00950542001-06-06 20:29:01 +00002254
2255JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
2256 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00002257 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00002258 if (V == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002259 GEN_ERROR("May only switch on a constant pool value!");
Chris Lattner00950542001-06-06 20:29:01 +00002260
Chris Lattner64116f92004-07-14 01:33:11 +00002261 $$->push_back(std::make_pair(V, getBBVal($6)));
Reid Spencer61c83e02006-08-18 08:43:06 +00002262 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002263 }
2264 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00002265 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00002266 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00002267
2268 if (V == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002269 GEN_ERROR("May only switch on a constant pool value!");
Chris Lattner00950542001-06-06 20:29:01 +00002270
Chris Lattner64116f92004-07-14 01:33:11 +00002271 $$->push_back(std::make_pair(V, getBBVal($5)));
Reid Spencer61c83e02006-08-18 08:43:06 +00002272 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002273 };
Chris Lattner00950542001-06-06 20:29:01 +00002274
2275Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00002276 // Is this definition named?? if so, assign the name...
Chris Lattner8ab406d2004-07-13 07:59:27 +00002277 setValueName($2, $1);
Chris Lattner00950542001-06-06 20:29:01 +00002278 InsertValue($2);
2279 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002280 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002281};
Chris Lattner00950542001-06-06 20:29:01 +00002282
Chris Lattnerc24d2082001-06-11 15:04:20 +00002283PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Chris Lattner9232b992003-04-22 18:42:41 +00002284 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Chris Lattner64116f92004-07-14 01:33:11 +00002285 $$->push_back(std::make_pair(getVal(*$1, $3), getBBVal($5)));
Chris Lattner30c89792001-09-07 16:35:17 +00002286 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002287 CHECK_FOR_ERROR
Chris Lattnerc24d2082001-06-11 15:04:20 +00002288 }
2289 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
2290 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00002291 $1->push_back(std::make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner64116f92004-07-14 01:33:11 +00002292 getBBVal($6)));
Reid Spencer61c83e02006-08-18 08:43:06 +00002293 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002294 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00002295
2296
Chris Lattner30c89792001-09-07 16:35:17 +00002297ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner9232b992003-04-22 18:42:41 +00002298 $$ = new std::vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002299 $$->push_back($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002300 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002301 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002302 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00002303 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002304 $1->push_back($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002305 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002306 };
Chris Lattner00950542001-06-06 20:29:01 +00002307
2308// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00002309ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00002310
Chris Lattnerddb6db42005-05-06 05:51:46 +00002311OptTailCall : TAIL CALL {
2312 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002313 CHECK_FOR_ERROR
Chris Lattnerddb6db42005-05-06 05:51:46 +00002314 }
2315 | CALL {
2316 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002317 CHECK_FOR_ERROR
Chris Lattnerddb6db42005-05-06 05:51:46 +00002318 };
2319
2320
2321
Chris Lattner4a6482b2002-09-10 19:57:26 +00002322InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Brian Gaeke715c90b2004-08-20 06:00:58 +00002323 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
2324 !isa<PackedType>((*$2).get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002325 GEN_ERROR(
Brian Gaeke715c90b2004-08-20 06:00:58 +00002326 "Arithmetic operator requires integer, FP, or packed operands!");
Misha Brukman5a2a3822005-05-10 22:02:28 +00002327 if (isa<PackedType>((*$2).get()) && $1 == Instruction::Rem)
Reid Spencer61c83e02006-08-18 08:43:06 +00002328 GEN_ERROR("Rem not supported on packed types!");
Chris Lattner4a6482b2002-09-10 19:57:26 +00002329 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
2330 if ($$ == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002331 GEN_ERROR("binary operator returned null!");
Chris Lattner4a6482b2002-09-10 19:57:26 +00002332 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002333 CHECK_FOR_ERROR
Chris Lattner4a6482b2002-09-10 19:57:26 +00002334 }
2335 | LogicalOps Types ValueRef ',' ValueRef {
Chris Lattner0a017832005-12-21 18:31:29 +00002336 if (!(*$2)->isIntegral()) {
2337 if (!isa<PackedType>($2->get()) ||
2338 !cast<PackedType>($2->get())->getElementType()->isIntegral())
Reid Spencer61c83e02006-08-18 08:43:06 +00002339 GEN_ERROR("Logical operator requires integral operands!");
Chris Lattner0a017832005-12-21 18:31:29 +00002340 }
Chris Lattner4a6482b2002-09-10 19:57:26 +00002341 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
2342 if ($$ == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002343 GEN_ERROR("binary operator returned null!");
Chris Lattner4a6482b2002-09-10 19:57:26 +00002344 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002345 CHECK_FOR_ERROR
Chris Lattner4a6482b2002-09-10 19:57:26 +00002346 }
2347 | SetCondOps Types ValueRef ',' ValueRef {
Reid Spencer57b6eec2004-08-21 16:11:02 +00002348 if(isa<PackedType>((*$2).get())) {
Reid Spencer61c83e02006-08-18 08:43:06 +00002349 GEN_ERROR(
Reid Spencer57b6eec2004-08-21 16:11:02 +00002350 "PackedTypes currently not supported in setcc instructions!");
2351 }
Chris Lattner1cff96a2002-09-10 22:37:46 +00002352 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00002353 if ($$ == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002354 GEN_ERROR("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00002355 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002356 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002357 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00002358 | NOT ResolvedVal {
2359 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
2360 << " Replacing with 'xor'.\n";
2361
2362 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
2363 if (Ones == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002364 GEN_ERROR("Expected integral type for not instruction!");
Chris Lattner699f1eb2002-08-14 17:12:33 +00002365
2366 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00002367 if ($$ == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002368 GEN_ERROR("Could not create a xor instruction!");
2369 CHECK_FOR_ERROR
Chris Lattner09083092001-07-08 04:57:15 +00002370 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002371 | ShiftOps ResolvedVal ',' ResolvedVal {
2372 if ($4->getType() != Type::UByteTy)
Reid Spencer61c83e02006-08-18 08:43:06 +00002373 GEN_ERROR("Shift amount must be ubyte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00002374 if (!$2->getType()->isInteger())
Reid Spencer61c83e02006-08-18 08:43:06 +00002375 GEN_ERROR("Shift constant expression requires integer operand!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002376 $$ = new ShiftInst($1, $2, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002377 CHECK_FOR_ERROR
Chris Lattner027dcc52001-07-08 21:10:27 +00002378 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002379 | CAST ResolvedVal TO Types {
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00002380 if (!$4->get()->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002381 GEN_ERROR("cast instruction to a non-primitive type: '" +
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00002382 $4->get()->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00002383 $$ = new CastInst($2, *$4);
2384 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00002385 CHECK_FOR_ERROR
Chris Lattner09083092001-07-08 04:57:15 +00002386 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00002387 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
2388 if ($2->getType() != Type::BoolTy)
Reid Spencer61c83e02006-08-18 08:43:06 +00002389 GEN_ERROR("select condition must be boolean!");
Chris Lattner76f0ff32004-03-12 05:51:36 +00002390 if ($4->getType() != $6->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002391 GEN_ERROR("select value types should match!");
Chris Lattner76f0ff32004-03-12 05:51:36 +00002392 $$ = new SelectInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002393 CHECK_FOR_ERROR
Chris Lattner76f0ff32004-03-12 05:51:36 +00002394 }
Chris Lattner99e7ab72003-10-18 05:53:13 +00002395 | VAARG ResolvedVal ',' Types {
Andrew Lenharthe78f50d2005-06-19 03:53:56 +00002396 NewVarArgs = true;
Chris Lattner99e7ab72003-10-18 05:53:13 +00002397 $$ = new VAArgInst($2, *$4);
2398 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00002399 CHECK_FOR_ERROR
Chris Lattner99e7ab72003-10-18 05:53:13 +00002400 }
Andrew Lenharth558bc882005-06-18 18:34:52 +00002401 | VAARG_old ResolvedVal ',' Types {
2402 ObsoleteVarArgs = true;
2403 const Type* ArgTy = $2->getType();
2404 Function* NF = CurModule.CurrentModule->
Jeff Cohen66c5fd62005-10-23 04:37:20 +00002405 getOrInsertFunction("llvm.va_copy", ArgTy, ArgTy, (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +00002406
2407 //b = vaarg a, t ->
2408 //foo = alloca 1 of t
2409 //bar = vacopy a
2410 //store bar -> foo
2411 //b = vaarg foo, t
2412 AllocaInst* foo = new AllocaInst(ArgTy, 0, "vaarg.fix");
2413 CurBB->getInstList().push_back(foo);
2414 CallInst* bar = new CallInst(NF, $2);
2415 CurBB->getInstList().push_back(bar);
2416 CurBB->getInstList().push_back(new StoreInst(bar, foo));
2417 $$ = new VAArgInst(foo, *$4);
2418 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00002419 CHECK_FOR_ERROR
Andrew Lenharth558bc882005-06-18 18:34:52 +00002420 }
2421 | VANEXT_old ResolvedVal ',' Types {
2422 ObsoleteVarArgs = true;
2423 const Type* ArgTy = $2->getType();
2424 Function* NF = CurModule.CurrentModule->
Jeff Cohen66c5fd62005-10-23 04:37:20 +00002425 getOrInsertFunction("llvm.va_copy", ArgTy, ArgTy, (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +00002426
2427 //b = vanext a, t ->
2428 //foo = alloca 1 of t
2429 //bar = vacopy a
2430 //store bar -> foo
2431 //tmp = vaarg foo, t
2432 //b = load foo
2433 AllocaInst* foo = new AllocaInst(ArgTy, 0, "vanext.fix");
2434 CurBB->getInstList().push_back(foo);
2435 CallInst* bar = new CallInst(NF, $2);
2436 CurBB->getInstList().push_back(bar);
2437 CurBB->getInstList().push_back(new StoreInst(bar, foo));
2438 Instruction* tmp = new VAArgInst(foo, *$4);
2439 CurBB->getInstList().push_back(tmp);
2440 $$ = new LoadInst(foo);
Chris Lattner8f77dae2003-05-08 02:44:12 +00002441 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00002442 CHECK_FOR_ERROR
Chris Lattner8f77dae2003-05-08 02:44:12 +00002443 }
Robert Bocchino9c62b562006-01-10 19:04:32 +00002444 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Chris Lattner2d7349a2006-04-08 04:08:32 +00002445 if (!ExtractElementInst::isValidOperands($2, $4))
Reid Spencer61c83e02006-08-18 08:43:06 +00002446 GEN_ERROR("Invalid extractelement operands!");
Robert Bocchino9c62b562006-01-10 19:04:32 +00002447 $$ = new ExtractElementInst($2, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002448 CHECK_FOR_ERROR
Robert Bocchino9c62b562006-01-10 19:04:32 +00002449 }
Robert Bocchino2def1b32006-01-17 20:06:25 +00002450 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Chris Lattner2d7349a2006-04-08 04:08:32 +00002451 if (!InsertElementInst::isValidOperands($2, $4, $6))
Reid Spencer61c83e02006-08-18 08:43:06 +00002452 GEN_ERROR("Invalid insertelement operands!");
Robert Bocchino2def1b32006-01-17 20:06:25 +00002453 $$ = new InsertElementInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002454 CHECK_FOR_ERROR
Robert Bocchino2def1b32006-01-17 20:06:25 +00002455 }
Chris Lattner4c949082006-04-08 01:18:35 +00002456 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
2457 if (!ShuffleVectorInst::isValidOperands($2, $4, $6))
Reid Spencer61c83e02006-08-18 08:43:06 +00002458 GEN_ERROR("Invalid shufflevector operands!");
Chris Lattner4c949082006-04-08 01:18:35 +00002459 $$ = new ShuffleVectorInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002460 CHECK_FOR_ERROR
Chris Lattner4c949082006-04-08 01:18:35 +00002461 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00002462 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00002463 const Type *Ty = $2->front().first->getType();
Chris Lattner8ea8f362003-10-30 01:38:18 +00002464 if (!Ty->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002465 GEN_ERROR("PHI node operands must be of first class type!");
Chris Lattnerc24d2082001-06-11 15:04:20 +00002466 $$ = new PHINode(Ty);
Chris Lattnerce1df9e2005-01-29 00:35:55 +00002467 ((PHINode*)$$)->reserveOperandSpace($2->size());
Chris Lattner00950542001-06-06 20:29:01 +00002468 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00002469 if ($2->front().first->getType() != Ty)
Reid Spencer61c83e02006-08-18 08:43:06 +00002470 GEN_ERROR("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00002471 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00002472 $2->pop_front();
2473 }
2474 delete $2; // Free the list...
Reid Spencer61c83e02006-08-18 08:43:06 +00002475 CHECK_FOR_ERROR
Chris Lattnerddb6db42005-05-06 05:51:46 +00002476 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00002477 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002478 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00002479 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00002480
Chris Lattnera8e8f162005-05-06 20:27:19 +00002481 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002482 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00002483 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00002484 std::vector<const Type*> ParamTypes;
Chris Lattnera8e8f162005-05-06 20:27:19 +00002485 if ($6) {
2486 for (std::vector<Value*>::iterator I = $6->begin(), E = $6->end();
Chris Lattner9232b992003-04-22 18:42:41 +00002487 I != E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00002488 ParamTypes.push_back((*I)->getType());
2489 }
Chris Lattner2079fde2001-10-13 06:41:08 +00002490
2491 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
2492 if (isVarArg) ParamTypes.pop_back();
2493
Chris Lattnera8e8f162005-05-06 20:27:19 +00002494 if (!(*$3)->isFirstClassType() && *$3 != Type::VoidTy)
Reid Spencer61c83e02006-08-18 08:43:06 +00002495 GEN_ERROR("LLVM functions cannot return aggregate types!");
Chris Lattner595f06c2005-02-01 01:47:42 +00002496
Chris Lattnera8e8f162005-05-06 20:27:19 +00002497 Ty = FunctionType::get($3->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002498 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00002499 }
Chris Lattner00950542001-06-06 20:29:01 +00002500
Chris Lattnera8e8f162005-05-06 20:27:19 +00002501 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00002502
Chris Lattner8b81bf52001-07-25 22:47:46 +00002503 // Create the call node...
Chris Lattnera8e8f162005-05-06 20:27:19 +00002504 if (!$6) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00002505 // Make sure no arguments is a good thing!
2506 if (Ty->getNumParams() != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002507 GEN_ERROR("No arguments passed to a function that "
Chris Lattnera4e25182002-07-25 20:52:56 +00002508 "expects arguments!");
2509
Chris Lattner9232b992003-04-22 18:42:41 +00002510 $$ = new CallInst(V, std::vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00002511 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00002512 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00002513 // correctly!
2514 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00002515 FunctionType::param_iterator I = Ty->param_begin();
2516 FunctionType::param_iterator E = Ty->param_end();
Chris Lattnera8e8f162005-05-06 20:27:19 +00002517 std::vector<Value*>::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00002518
2519 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
Reid Spencer3271ed52004-07-18 00:08:11 +00002520 if ((*ArgI)->getType() != *I)
Reid Spencer61c83e02006-08-18 08:43:06 +00002521 GEN_ERROR("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Reid Spencer3271ed52004-07-18 00:08:11 +00002522 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00002523
Chris Lattner8b81bf52001-07-25 22:47:46 +00002524 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002525 GEN_ERROR("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00002526
Chris Lattnera8e8f162005-05-06 20:27:19 +00002527 $$ = new CallInst(V, *$6);
Chris Lattner8b81bf52001-07-25 22:47:46 +00002528 }
Chris Lattnerddb6db42005-05-06 05:51:46 +00002529 cast<CallInst>($$)->setTailCall($1);
Chris Lattnera8e8f162005-05-06 20:27:19 +00002530 cast<CallInst>($$)->setCallingConv($2);
2531 delete $3;
2532 delete $6;
Reid Spencer61c83e02006-08-18 08:43:06 +00002533 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002534 }
2535 | MemoryInst {
2536 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002537 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002538 };
Chris Lattner00950542001-06-06 20:29:01 +00002539
Chris Lattner6cdb0112001-11-26 16:54:11 +00002540
2541// IndexList - List of indices for GEP based instructions...
2542IndexList : ',' ValueRefList {
Chris Lattner15c9c032003-09-08 18:20:29 +00002543 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002544 CHECK_FOR_ERROR
Chris Lattner15c9c032003-09-08 18:20:29 +00002545 } | /* empty */ {
2546 $$ = new std::vector<Value*>();
Reid Spencer61c83e02006-08-18 08:43:06 +00002547 CHECK_FOR_ERROR
Chris Lattner15c9c032003-09-08 18:20:29 +00002548 };
2549
2550OptVolatile : VOLATILE {
2551 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002552 CHECK_FOR_ERROR
Chris Lattner15c9c032003-09-08 18:20:29 +00002553 }
2554 | /* empty */ {
2555 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002556 CHECK_FOR_ERROR
Chris Lattner15c9c032003-09-08 18:20:29 +00002557 };
2558
Chris Lattner027dcc52001-07-08 21:10:27 +00002559
Chris Lattnerddb6db42005-05-06 05:51:46 +00002560
Chris Lattner66db8e42005-11-06 06:34:12 +00002561MemoryInst : MALLOC Types OptCAlign {
Chris Lattner66db8e42005-11-06 06:34:12 +00002562 $$ = new MallocInst(*$2, 0, $3);
Chris Lattner30c89792001-09-07 16:35:17 +00002563 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002564 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002565 }
Chris Lattner66db8e42005-11-06 06:34:12 +00002566 | MALLOC Types ',' UINT ValueRef OptCAlign {
Chris Lattner66db8e42005-11-06 06:34:12 +00002567 $$ = new MallocInst(*$2, getVal($4, $5), $6);
Nate Begeman14b05292005-11-05 09:21:28 +00002568 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002569 CHECK_FOR_ERROR
Nate Begeman14b05292005-11-05 09:21:28 +00002570 }
Chris Lattner66db8e42005-11-06 06:34:12 +00002571 | ALLOCA Types OptCAlign {
Chris Lattner66db8e42005-11-06 06:34:12 +00002572 $$ = new AllocaInst(*$2, 0, $3);
Nate Begeman14b05292005-11-05 09:21:28 +00002573 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002574 CHECK_FOR_ERROR
Nate Begeman14b05292005-11-05 09:21:28 +00002575 }
Chris Lattner66db8e42005-11-06 06:34:12 +00002576 | ALLOCA Types ',' UINT ValueRef OptCAlign {
Chris Lattner66db8e42005-11-06 06:34:12 +00002577 $$ = new AllocaInst(*$2, getVal($4, $5), $6);
Nate Begeman14b05292005-11-05 09:21:28 +00002578 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002579 CHECK_FOR_ERROR
Nate Begeman14b05292005-11-05 09:21:28 +00002580 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002581 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00002582 if (!isa<PointerType>($2->getType()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002583 GEN_ERROR("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00002584 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002585 $$ = new FreeInst($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00002586 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002587 }
2588
Chris Lattner15c9c032003-09-08 18:20:29 +00002589 | OptVolatile LOAD Types ValueRef {
2590 if (!isa<PointerType>($3->get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002591 GEN_ERROR("Can't load from nonpointer type: " +
Reid Spencer3271ed52004-07-18 00:08:11 +00002592 (*$3)->getDescription());
Chris Lattner1c1bb332004-10-09 02:18:58 +00002593 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002594 GEN_ERROR("Can't load from pointer of non-first-class type: " +
Chris Lattner1c1bb332004-10-09 02:18:58 +00002595 (*$3)->getDescription());
Chris Lattner79ad13802003-09-08 20:29:46 +00002596 $$ = new LoadInst(getVal(*$3, $4), "", $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002597 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002598 CHECK_FOR_ERROR
Chris Lattner027dcc52001-07-08 21:10:27 +00002599 }
Chris Lattner15c9c032003-09-08 18:20:29 +00002600 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
2601 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002602 if (!PT)
Reid Spencer61c83e02006-08-18 08:43:06 +00002603 GEN_ERROR("Can't store to a nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00002604 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002605 const Type *ElTy = PT->getElementType();
Chris Lattner15c9c032003-09-08 18:20:29 +00002606 if (ElTy != $3->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002607 GEN_ERROR("Can't store '" + $3->getType()->getDescription() +
Chris Lattner72e00252001-12-14 16:28:42 +00002608 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner0383cc42002-08-21 23:51:21 +00002609
Chris Lattner79ad13802003-09-08 20:29:46 +00002610 $$ = new StoreInst($3, getVal(*$5, $6), $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002611 delete $5;
Reid Spencer61c83e02006-08-18 08:43:06 +00002612 CHECK_FOR_ERROR
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002613 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00002614 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00002615 if (!isa<PointerType>($2->get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002616 GEN_ERROR("getelementptr insn requires pointer operand!");
Chris Lattner830b6f92004-04-05 01:30:04 +00002617
2618 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
2619 // indices to uint struct indices for compatibility.
2620 generic_gep_type_iterator<std::vector<Value*>::iterator>
2621 GTI = gep_type_begin($2->get(), $4->begin(), $4->end()),
2622 GTE = gep_type_end($2->get(), $4->begin(), $4->end());
2623 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
2624 if (isa<StructType>(*GTI)) // Only change struct indices
2625 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
2626 if (CUI->getType() == Type::UByteTy)
2627 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
2628
Chris Lattner30c89792001-09-07 16:35:17 +00002629 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Reid Spencer61c83e02006-08-18 08:43:06 +00002630 GEN_ERROR("Invalid getelementptr indices for type '" +
Chris Lattner830b6f92004-04-05 01:30:04 +00002631 (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00002632 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
2633 delete $2; delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00002634 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002635 };
Chris Lattner027dcc52001-07-08 21:10:27 +00002636
Brian Gaeked0fde302003-11-11 22:41:34 +00002637
Chris Lattner00950542001-06-06 20:29:01 +00002638%%
Reid Spencer61c83e02006-08-18 08:43:06 +00002639
2640void llvm::GenerateError(const std::string &message, int LineNo) {
2641 if (LineNo == -1) LineNo = llvmAsmlineno;
2642 // TODO: column number in exception
2643 if (TheParseError)
2644 TheParseError->setError(CurFilename, message, LineNo);
2645 TriggerError = 1;
2646}
2647
Chris Lattner09083092001-07-08 04:57:15 +00002648int yyerror(const char *ErrorMsg) {
Chris Lattner9232b992003-04-22 18:42:41 +00002649 std::string where
2650 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002651 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Chris Lattner9232b992003-04-22 18:42:41 +00002652 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
Chris Lattnerbc280ab2004-03-30 20:58:25 +00002653 if (yychar == YYEMPTY || yychar == 0)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002654 errMsg += "end-of-file.";
2655 else
Chris Lattner9232b992003-04-22 18:42:41 +00002656 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Reid Spencer61c83e02006-08-18 08:43:06 +00002657 GenerateError(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00002658 return 0;
2659}