blob: f09b99a5f0383de7414a2c170855069712eca95a [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 Spencer61c83e02006-08-18 08:43:06 +000030static bool TriggerError = false;
31#define CHECK_FOR_ERROR { if (TriggerError) { TriggerError = false; YYERROR; } }
32
33#define GEN_ERROR(msg) { GenerateError(msg); YYERROR; }
34
Chris Lattner386a3b72001-10-16 19:54:17 +000035int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
Chris Lattner09083092001-07-08 04:57:15 +000036int yylex(); // declaration" of xxx warnings.
Chris Lattner00950542001-06-06 20:29:01 +000037int yyparse();
38
Brian Gaeked0fde302003-11-11 22:41:34 +000039namespace llvm {
Chris Lattner86427632004-07-14 06:39:48 +000040 std::string CurFilename;
41}
42using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000043
Chris Lattner00950542001-06-06 20:29:01 +000044static Module *ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +000045
Chris Lattner30c89792001-09-07 16:35:17 +000046// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
47// relating to upreferences in the input stream.
48//
49//#define DEBUG_UPREFS 1
50#ifdef DEBUG_UPREFS
Chris Lattner699f1eb2002-08-14 17:12:33 +000051#define UR_OUT(X) std::cerr << X
Chris Lattner30c89792001-09-07 16:35:17 +000052#else
53#define UR_OUT(X)
54#endif
55
Vikram S. Adved3f7eb02002-07-14 22:59:28 +000056#define YYERROR_VERBOSE 1
57
Andrew Lenharth558bc882005-06-18 18:34:52 +000058static bool ObsoleteVarArgs;
Andrew Lenharthe78f50d2005-06-19 03:53:56 +000059static bool NewVarArgs;
Chris Lattnerb2b96672005-11-12 18:21:21 +000060static BasicBlock *CurBB;
61static GlobalVariable *CurGV;
Andrew Lenharth558bc882005-06-18 18:34:52 +000062
63
Chris Lattner7e708292002-06-25 16:13:24 +000064// This contains info used when building the body of a function. It is
65// destroyed when the function is completed.
Chris Lattner00950542001-06-06 20:29:01 +000066//
Chris Lattner9232b992003-04-22 18:42:41 +000067typedef std::vector<Value *> ValueList; // Numbered defs
Misha Brukman5a2a3822005-05-10 22:02:28 +000068static void
69ResolveDefinitions(std::map<const Type *,ValueList> &LateResolvers,
70 std::map<const Type *,ValueList> *FutureLateResolvers = 0);
Chris Lattner00950542001-06-06 20:29:01 +000071
72static struct PerModuleInfo {
73 Module *CurrentModule;
Chris Lattner735f2702004-07-08 22:30:50 +000074 std::map<const Type *, ValueList> Values; // Module level numbered definitions
75 std::map<const Type *,ValueList> LateResolveValues;
Chris Lattner16af11d2004-02-09 21:03:38 +000076 std::vector<PATypeHolder> Types;
Chris Lattner9232b992003-04-22 18:42:41 +000077 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000078
Chris Lattnerbc721ed2004-07-13 08:28:21 +000079 /// PlaceHolderInfo - When temporary placeholder objects are created, remember
Reid Spencerfd20c0a2006-05-29 02:34:34 +000080 /// how they were referenced and on which line of the input they came from so
Chris Lattner22ae2322004-07-13 08:39:15 +000081 /// that we can resolve them later and print error messages as appropriate.
Chris Lattnerbc721ed2004-07-13 08:28:21 +000082 std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
83
Chris Lattner2079fde2001-10-13 06:41:08 +000084 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
85 // references to global values. Global values may be referenced before they
86 // are defined, and if so, the temporary object that they represent is held
Reid Spencer3271ed52004-07-18 00:08:11 +000087 // here. This is used for forward references of GlobalValues.
Chris Lattner2079fde2001-10-13 06:41:08 +000088 //
Chris Lattner9232b992003-04-22 18:42:41 +000089 typedef std::map<std::pair<const PointerType *,
Chris Lattnerbc207592004-03-08 06:09:57 +000090 ValID>, GlobalValue*> GlobalRefsType;
Chris Lattner2079fde2001-10-13 06:41:08 +000091 GlobalRefsType GlobalRefs;
92
Chris Lattner00950542001-06-06 20:29:01 +000093 void ModuleDone() {
Chris Lattner7e708292002-06-25 16:13:24 +000094 // If we could not resolve some functions at function compilation time
95 // (calls to functions before they are defined), resolve them now... Types
96 // are resolved when the constant pool has been completely parsed.
Chris Lattner30c89792001-09-07 16:35:17 +000097 //
Chris Lattner00950542001-06-06 20:29:01 +000098 ResolveDefinitions(LateResolveValues);
99
Chris Lattner2079fde2001-10-13 06:41:08 +0000100 // Check to make sure that all global value forward references have been
101 // resolved!
102 //
103 if (!GlobalRefs.empty()) {
Chris Lattner9232b992003-04-22 18:42:41 +0000104 std::string UndefinedReferences = "Unresolved global references exist:\n";
Misha Brukman5a2a3822005-05-10 22:02:28 +0000105
Chris Lattner749ce032002-03-11 22:12:39 +0000106 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
107 I != E; ++I) {
108 UndefinedReferences += " " + I->first.first->getDescription() + " " +
109 I->first.second.getName() + "\n";
110 }
Reid Spencer61c83e02006-08-18 08:43:06 +0000111 GenerateError(UndefinedReferences);
Chris Lattner2079fde2001-10-13 06:41:08 +0000112 }
113
Reid Spencere812fb22006-01-19 01:21:04 +0000114 // Look for intrinsic functions and CallInst that need to be upgraded
Chris Lattner7d5c1e12006-03-04 07:53:16 +0000115 for (Module::iterator FI = CurrentModule->begin(),
116 FE = CurrentModule->end(); FI != FE; )
117 UpgradeCallsToIntrinsic(FI++);
Reid Spencer0b118202006-01-16 21:12:35 +0000118
Chris Lattner7e708292002-06-25 16:13:24 +0000119 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000120 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +0000121 CurrentModule = 0;
122 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000123
Chris Lattner8a327842004-07-14 21:44:00 +0000124 // GetForwardRefForGlobal - Check to see if there is a forward reference
125 // for this global. If so, remove it from the GlobalRefs map and return it.
126 // If not, just return null.
127 GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) {
128 // Check to see if there is a forward reference to this global variable...
129 // if there is, eliminate it and patch the reference to use the new def'n.
130 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID));
131 GlobalValue *Ret = 0;
132 if (I != GlobalRefs.end()) {
133 Ret = I->second;
134 GlobalRefs.erase(I);
135 }
136 return Ret;
137 }
Chris Lattner00950542001-06-06 20:29:01 +0000138} CurModule;
139
Chris Lattner79df7c02002-03-26 18:01:55 +0000140static struct PerFunctionInfo {
Chris Lattner7e708292002-06-25 16:13:24 +0000141 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner00950542001-06-06 20:29:01 +0000142
Chris Lattner735f2702004-07-08 22:30:50 +0000143 std::map<const Type*, ValueList> Values; // Keep track of #'d definitions
144 std::map<const Type*, ValueList> LateResolveValues;
Chris Lattner7e708292002-06-25 16:13:24 +0000145 bool isDeclare; // Is this function a forward declararation?
Chris Lattner00950542001-06-06 20:29:01 +0000146
Chris Lattner2e2ed292004-07-14 06:28:35 +0000147 /// BBForwardRefs - When we see forward references to basic blocks, keep
148 /// track of them here.
149 std::map<BasicBlock*, std::pair<ValID, int> > BBForwardRefs;
150 std::vector<BasicBlock*> NumberedBlocks;
151 unsigned NextBBNum;
152
Chris Lattner79df7c02002-03-26 18:01:55 +0000153 inline PerFunctionInfo() {
154 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000155 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000156 }
157
Chris Lattner79df7c02002-03-26 18:01:55 +0000158 inline void FunctionStart(Function *M) {
159 CurrentFunction = M;
Chris Lattner2e2ed292004-07-14 06:28:35 +0000160 NextBBNum = 0;
Chris Lattner00950542001-06-06 20:29:01 +0000161 }
162
Chris Lattner79df7c02002-03-26 18:01:55 +0000163 void FunctionDone() {
Chris Lattner2e2ed292004-07-14 06:28:35 +0000164 NumberedBlocks.clear();
165
166 // Any forward referenced blocks left?
167 if (!BBForwardRefs.empty())
Reid Spencer61c83e02006-08-18 08:43:06 +0000168 GenerateError("Undefined reference to label " +
Chris Lattner83beacd2005-02-24 04:59:49 +0000169 BBForwardRefs.begin()->first->getName());
Chris Lattner2e2ed292004-07-14 06:28:35 +0000170
171 // Resolve all forward references now.
Chris Lattner386a3b72001-10-16 19:54:17 +0000172 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000173
Chris Lattner7e708292002-06-25 16:13:24 +0000174 Values.clear(); // Clear out function local definitions
Chris Lattner79df7c02002-03-26 18:01:55 +0000175 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000176 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000177 }
Chris Lattner394f2eb2003-10-16 20:12:13 +0000178} CurFun; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000179
Chris Lattner394f2eb2003-10-16 20:12:13 +0000180static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000181
Chris Lattner00950542001-06-06 20:29:01 +0000182
183//===----------------------------------------------------------------------===//
184// Code to handle definitions of all the types
185//===----------------------------------------------------------------------===//
186
Chris Lattner735f2702004-07-08 22:30:50 +0000187static int InsertValue(Value *V,
188 std::map<const Type*,ValueList> &ValueTab = CurFun.Values) {
189 if (V->hasName()) return -1; // Is this a numbered definition?
Chris Lattner2079fde2001-10-13 06:41:08 +0000190
191 // Yes, insert the value into the value table...
Chris Lattner735f2702004-07-08 22:30:50 +0000192 ValueList &List = ValueTab[V->getType()];
193 List.push_back(V);
Chris Lattner16af11d2004-02-09 21:03:38 +0000194 return List.size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000195}
196
Chris Lattner30c89792001-09-07 16:35:17 +0000197static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000198 switch (D.Type) {
Chris Lattnera09000d2004-07-14 23:03:46 +0000199 case ValID::NumberVal: // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000200 // Module constants occupy the lowest numbered slots...
Misha Brukman5a2a3822005-05-10 22:02:28 +0000201 if ((unsigned)D.Num < CurModule.Types.size())
Chris Lattnera09000d2004-07-14 23:03:46 +0000202 return CurModule.Types[(unsigned)D.Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000203 break;
Chris Lattnera09000d2004-07-14 23:03:46 +0000204 case ValID::NameVal: // Is it a named definition?
205 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.Name)) {
206 D.destroy(); // Free old strdup'd memory...
207 return N;
Chris Lattner6e6026b2002-11-20 18:36:02 +0000208 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000209 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000210 default:
Reid Spencer61c83e02006-08-18 08:43:06 +0000211 GenerateError("Internal parser error: Invalid symbol type reference!");
Chris Lattner30c89792001-09-07 16:35:17 +0000212 }
213
214 // If we reached here, we referenced either a symbol that we don't know about
215 // or an id number that hasn't been read yet. We may be referencing something
216 // forward, so just create an entry to be resolved later and get to it...
217 //
218 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
219
Chris Lattner355ad1f2005-03-21 06:27:42 +0000220
221 if (inFunctionScope()) {
222 if (D.Type == ValID::NameVal)
Reid Spencer61c83e02006-08-18 08:43:06 +0000223 GenerateError("Reference to an undefined type: '" + D.getName() + "'");
Chris Lattner355ad1f2005-03-21 06:27:42 +0000224 else
Reid Spencer61c83e02006-08-18 08:43:06 +0000225 GenerateError("Reference to an undefined type: #" + itostr(D.Num));
Chris Lattner4a42e902001-10-22 05:56:09 +0000226 }
Chris Lattner30c89792001-09-07 16:35:17 +0000227
Chris Lattner355ad1f2005-03-21 06:27:42 +0000228 std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D);
229 if (I != CurModule.LateResolveTypes.end())
230 return I->second;
231
Chris Lattner82269592001-10-22 06:01:08 +0000232 Type *Typ = OpaqueType::get();
Chris Lattner355ad1f2005-03-21 06:27:42 +0000233 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
Chris Lattner30c89792001-09-07 16:35:17 +0000234 return Typ;
Chris Lattner355ad1f2005-03-21 06:27:42 +0000235 }
Chris Lattner30c89792001-09-07 16:35:17 +0000236
Chris Lattner9232b992003-04-22 18:42:41 +0000237static Value *lookupInSymbolTable(const Type *Ty, const std::string &Name) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000238 SymbolTable &SymTab =
Chris Lattner394f2eb2003-10-16 20:12:13 +0000239 inFunctionScope() ? CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner9705a152002-05-02 19:27:42 +0000240 CurModule.CurrentModule->getSymbolTable();
Chris Lattner6e6026b2002-11-20 18:36:02 +0000241 return SymTab.lookup(Ty, Name);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000242}
243
Chris Lattner2079fde2001-10-13 06:41:08 +0000244// getValNonImprovising - Look up the value specified by the provided type and
245// the provided ValID. If the value exists and has already been defined, return
246// it. Otherwise return null.
247//
248static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000249 if (isa<FunctionType>(Ty))
Reid Spencer61c83e02006-08-18 08:43:06 +0000250 GenerateError("Functions are not values and "
Chris Lattner79df7c02002-03-26 18:01:55 +0000251 "must be referenced as pointers");
Chris Lattner386a3b72001-10-16 19:54:17 +0000252
Chris Lattner30c89792001-09-07 16:35:17 +0000253 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000254 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner00950542001-06-06 20:29:01 +0000255 unsigned Num = (unsigned)D.Num;
256
257 // Module constants occupy the lowest numbered slots...
Chris Lattner735f2702004-07-08 22:30:50 +0000258 std::map<const Type*,ValueList>::iterator VI = CurModule.Values.find(Ty);
Chris Lattner16af11d2004-02-09 21:03:38 +0000259 if (VI != CurModule.Values.end()) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000260 if (Num < VI->second.size())
Chris Lattner16af11d2004-02-09 21:03:38 +0000261 return VI->second[Num];
262 Num -= VI->second.size();
Chris Lattner00950542001-06-06 20:29:01 +0000263 }
264
265 // Make sure that our type is within bounds
Chris Lattner735f2702004-07-08 22:30:50 +0000266 VI = CurFun.Values.find(Ty);
Chris Lattner16af11d2004-02-09 21:03:38 +0000267 if (VI == CurFun.Values.end()) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000268
269 // Check that the number is within bounds...
Chris Lattner16af11d2004-02-09 21:03:38 +0000270 if (VI->second.size() <= Num) return 0;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000271
Chris Lattner16af11d2004-02-09 21:03:38 +0000272 return VI->second[Num];
Chris Lattner00950542001-06-06 20:29:01 +0000273 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000274
Chris Lattner1a1cb112001-09-30 22:46:54 +0000275 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000276 Value *N = lookupInSymbolTable(Ty, std::string(D.Name));
Chris Lattner2079fde2001-10-13 06:41:08 +0000277 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000278
279 D.destroy(); // Free old strdup'd memory...
280 return N;
281 }
282
Misha Brukman5a2a3822005-05-10 22:02:28 +0000283 // Check to make sure that "Ty" is an integral type, and that our
Chris Lattner2079fde2001-10-13 06:41:08 +0000284 // value will fit into the specified type...
285 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattnerd78700d2002-08-16 21:14:40 +0000286 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
Reid Spencer61c83e02006-08-18 08:43:06 +0000287 GenerateError("Signed integral constant '" +
Misha Brukman5a2a3822005-05-10 22:02:28 +0000288 itostr(D.ConstPool64) + "' is invalid for type '" +
Chris Lattnerd78700d2002-08-16 21:14:40 +0000289 Ty->getDescription() + "'!");
290 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000291
292 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000293 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
294 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000295 GenerateError("Integral constant '" + utostr(D.UConstPool64) +
Chris Lattnerf8dff732002-07-18 05:18:37 +0000296 "' is invalid or out of range!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000297 } else { // This is really a signed reference. Transmogrify.
Reid Spencer3271ed52004-07-18 00:08:11 +0000298 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000299 }
300 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000301 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000302 }
303
Chris Lattner2079fde2001-10-13 06:41:08 +0000304 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000305 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Reid Spencer61c83e02006-08-18 08:43:06 +0000306 GenerateError("FP constant invalid for type!!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000307 return ConstantFP::get(Ty, D.ConstPoolFP);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000308
Chris Lattner2079fde2001-10-13 06:41:08 +0000309 case ValID::ConstNullVal: // Is it a null value?
Chris Lattner9b625032002-05-06 16:15:30 +0000310 if (!isa<PointerType>(Ty))
Reid Spencer61c83e02006-08-18 08:43:06 +0000311 GenerateError("Cannot create a a non pointer null!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000312 return ConstantPointerNull::get(cast<PointerType>(Ty));
Misha Brukman5a2a3822005-05-10 22:02:28 +0000313
Chris Lattner16710e92004-10-16 18:17:13 +0000314 case ValID::ConstUndefVal: // Is it an undef value?
315 return UndefValue::get(Ty);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000316
Chris Lattnerf1f03df2005-12-21 17:53:02 +0000317 case ValID::ConstZeroVal: // Is it a zero value?
318 return Constant::getNullValue(Ty);
319
Chris Lattnerd78700d2002-08-16 21:14:40 +0000320 case ValID::ConstantVal: // Fully resolved constant?
321 if (D.ConstantValue->getType() != Ty)
Reid Spencer61c83e02006-08-18 08:43:06 +0000322 GenerateError("Constant expression type different from required type!");
Chris Lattnerd78700d2002-08-16 21:14:40 +0000323 return D.ConstantValue;
324
Chris Lattneraa2c8532006-01-25 22:26:43 +0000325 case ValID::InlineAsmVal: { // Inline asm expression
326 const PointerType *PTy = dyn_cast<PointerType>(Ty);
327 const FunctionType *FTy =
328 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
329 if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints))
Reid Spencer61c83e02006-08-18 08:43:06 +0000330 GenerateError("Invalid type for asm constraint string!");
Chris Lattneraa2c8532006-01-25 22:26:43 +0000331 InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints,
332 D.IAD->HasSideEffects);
333 D.destroy(); // Free InlineAsmDescriptor.
334 return IA;
335 }
Chris Lattner30c89792001-09-07 16:35:17 +0000336 default:
337 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000338 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000339 } // End of switch
340
Chris Lattner2079fde2001-10-13 06:41:08 +0000341 assert(0 && "Unhandled case!");
342 return 0;
343}
344
Chris Lattner2079fde2001-10-13 06:41:08 +0000345// getVal - This function is identical to getValNonImprovising, except that if a
346// value is not already defined, it "improvises" by creating a placeholder var
347// that looks and acts just like the requested variable. When the value is
348// defined later, all uses of the placeholder variable are replaced with the
349// real thing.
350//
Chris Lattner64116f92004-07-14 01:33:11 +0000351static Value *getVal(const Type *Ty, const ValID &ID) {
352 if (Ty == Type::LabelTy)
Reid Spencer61c83e02006-08-18 08:43:06 +0000353 GenerateError("Cannot use a basic block here");
Chris Lattner2079fde2001-10-13 06:41:08 +0000354
Chris Lattner64116f92004-07-14 01:33:11 +0000355 // See if the value has already been defined.
356 Value *V = getValNonImprovising(Ty, ID);
Chris Lattner2079fde2001-10-13 06:41:08 +0000357 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000358
Chris Lattner60cd9552005-03-23 01:29:26 +0000359 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty))
Reid Spencer61c83e02006-08-18 08:43:06 +0000360 GenerateError("Invalid use of a composite type!");
Chris Lattner60cd9552005-03-23 01:29:26 +0000361
Chris Lattner00950542001-06-06 20:29:01 +0000362 // If we reached here, we referenced either a symbol that we don't know about
363 // or an id number that hasn't been read yet. We may be referencing something
364 // forward, so just create an entry to be resolved later and get to it...
365 //
Chris Lattner64116f92004-07-14 01:33:11 +0000366 V = new Argument(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000367
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000368 // Remember where this forward reference came from. FIXME, shouldn't we try
369 // to recycle these things??
Chris Lattner64116f92004-07-14 01:33:11 +0000370 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000371 llvmAsmlineno)));
372
Chris Lattner79df7c02002-03-26 18:01:55 +0000373 if (inFunctionScope())
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000374 InsertValue(V, CurFun.LateResolveValues);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000375 else
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000376 InsertValue(V, CurModule.LateResolveValues);
377 return V;
Chris Lattner00950542001-06-06 20:29:01 +0000378}
379
Chris Lattner2e2ed292004-07-14 06:28:35 +0000380/// getBBVal - This is used for two purposes:
381/// * If isDefinition is true, a new basic block with the specified ID is being
382/// defined.
383/// * If isDefinition is true, this is a reference to a basic block, which may
384/// or may not be a forward reference.
385///
386static BasicBlock *getBBVal(const ValID &ID, bool isDefinition = false) {
Chris Lattner64116f92004-07-14 01:33:11 +0000387 assert(inFunctionScope() && "Can't get basic block at global scope!");
388
Chris Lattner2e2ed292004-07-14 06:28:35 +0000389 std::string Name;
390 BasicBlock *BB = 0;
391 switch (ID.Type) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000392 default: GenerateError("Illegal label reference " + ID.getName());
Chris Lattner2e2ed292004-07-14 06:28:35 +0000393 case ValID::NumberVal: // Is it a numbered definition?
394 if (unsigned(ID.Num) >= CurFun.NumberedBlocks.size())
395 CurFun.NumberedBlocks.resize(ID.Num+1);
396 BB = CurFun.NumberedBlocks[ID.Num];
397 break;
398 case ValID::NameVal: // Is it a named definition?
399 Name = ID.Name;
Chris Lattnera09000d2004-07-14 23:03:46 +0000400 if (Value *N = CurFun.CurrentFunction->
401 getSymbolTable().lookup(Type::LabelTy, Name))
Chris Lattner2e2ed292004-07-14 06:28:35 +0000402 BB = cast<BasicBlock>(N);
403 break;
404 }
Chris Lattner64116f92004-07-14 01:33:11 +0000405
Chris Lattner2e2ed292004-07-14 06:28:35 +0000406 // See if the block has already been defined.
407 if (BB) {
408 // If this is the definition of the block, make sure the existing value was
409 // just a forward reference. If it was a forward reference, there will be
410 // an entry for it in the PlaceHolderInfo map.
411 if (isDefinition && !CurFun.BBForwardRefs.erase(BB))
412 // The existing value was a definition, not a forward reference.
Reid Spencer61c83e02006-08-18 08:43:06 +0000413 GenerateError("Redefinition of label " + ID.getName());
Chris Lattner64116f92004-07-14 01:33:11 +0000414
Chris Lattner2e2ed292004-07-14 06:28:35 +0000415 ID.destroy(); // Free strdup'd memory.
416 return BB;
417 }
418
419 // Otherwise this block has not been seen before.
420 BB = new BasicBlock("", CurFun.CurrentFunction);
421 if (ID.Type == ValID::NameVal) {
422 BB->setName(ID.Name);
423 } else {
424 CurFun.NumberedBlocks[ID.Num] = BB;
425 }
426
427 // If this is not a definition, keep track of it so we can use it as a forward
428 // reference.
429 if (!isDefinition) {
430 // Remember where this forward reference came from.
431 CurFun.BBForwardRefs[BB] = std::make_pair(ID, llvmAsmlineno);
432 } else {
433 // The forward declaration could have been inserted anywhere in the
434 // function: insert it into the correct place now.
435 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
436 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
437 }
Chris Lattnere8343a52004-10-26 18:26:14 +0000438 ID.destroy();
Chris Lattner64116f92004-07-14 01:33:11 +0000439 return BB;
440}
441
Chris Lattner00950542001-06-06 20:29:01 +0000442
443//===----------------------------------------------------------------------===//
444// Code to handle forward references in instructions
445//===----------------------------------------------------------------------===//
446//
447// This code handles the late binding needed with statements that reference
448// values not defined yet... for example, a forward branch, or the PHI node for
449// a loop body.
450//
Chris Lattner394f2eb2003-10-16 20:12:13 +0000451// This keeps a table (CurFun.LateResolveValues) of all such forward references
Chris Lattner00950542001-06-06 20:29:01 +0000452// and back patchs after we are done.
453//
454
Misha Brukman5a2a3822005-05-10 22:02:28 +0000455// ResolveDefinitions - If we could not resolve some defs at parsing
456// time (forward branches, phi functions for loops, etc...) resolve the
Chris Lattner00950542001-06-06 20:29:01 +0000457// defs now...
458//
Misha Brukman5a2a3822005-05-10 22:02:28 +0000459static void
460ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers,
461 std::map<const Type*,ValueList> *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000462 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Chris Lattner735f2702004-07-08 22:30:50 +0000463 for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(),
Chris Lattner16af11d2004-02-09 21:03:38 +0000464 E = LateResolvers.end(); LRI != E; ++LRI) {
465 ValueList &List = LRI->second;
466 while (!List.empty()) {
467 Value *V = List.back();
468 List.pop_back();
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000469
470 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
471 CurModule.PlaceHolderInfo.find(V);
472 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
473
474 ValID &DID = PHI->second.first;
Chris Lattner00950542001-06-06 20:29:01 +0000475
Chris Lattner735f2702004-07-08 22:30:50 +0000476 Value *TheRealValue = getValNonImprovising(LRI->first, DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000477 if (TheRealValue) {
478 V->replaceAllUsesWith(TheRealValue);
479 delete V;
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000480 CurModule.PlaceHolderInfo.erase(PHI);
Chris Lattner386a3b72001-10-16 19:54:17 +0000481 } else if (FutureLateResolvers) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000482 // Functions have their unresolved items forwarded to the module late
Chris Lattner386a3b72001-10-16 19:54:17 +0000483 // resolver table
484 InsertValue(V, *FutureLateResolvers);
485 } else {
Reid Spencer3271ed52004-07-18 00:08:11 +0000486 if (DID.Type == ValID::NameVal)
Reid Spencer61c83e02006-08-18 08:43:06 +0000487 GenerateError("Reference to an invalid definition: '" +DID.getName()+
Reid Spencer3271ed52004-07-18 00:08:11 +0000488 "' of type '" + V->getType()->getDescription() + "'",
489 PHI->second.second);
490 else
Reid Spencer61c83e02006-08-18 08:43:06 +0000491 GenerateError("Reference to an invalid definition: #" +
Misha Brukman5a2a3822005-05-10 22:02:28 +0000492 itostr(DID.Num) + " of type '" +
Reid Spencer3271ed52004-07-18 00:08:11 +0000493 V->getType()->getDescription() + "'",
494 PHI->second.second);
Chris Lattner30c89792001-09-07 16:35:17 +0000495 }
Chris Lattner00950542001-06-06 20:29:01 +0000496 }
497 }
498
499 LateResolvers.clear();
500}
501
Chris Lattner4a42e902001-10-22 05:56:09 +0000502// ResolveTypeTo - A brand new type was just declared. This means that (if
503// name is not null) things referencing Name can be resolved. Otherwise, things
504// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000505//
Chris Lattner4a42e902001-10-22 05:56:09 +0000506static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner355ad1f2005-03-21 06:27:42 +0000507 ValID D;
508 if (Name) D = ValID::create(Name);
509 else D = ValID::create((int)CurModule.Types.size());
Chris Lattner00950542001-06-06 20:29:01 +0000510
Chris Lattner355ad1f2005-03-21 06:27:42 +0000511 std::map<ValID, PATypeHolder>::iterator I =
512 CurModule.LateResolveTypes.find(D);
513 if (I != CurModule.LateResolveTypes.end()) {
514 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
515 CurModule.LateResolveTypes.erase(I);
Chris Lattner30c89792001-09-07 16:35:17 +0000516 }
517}
518
Chris Lattner1781aca2001-09-18 04:00:54 +0000519// setValueName - Set the specified value to the name given. The name may be
520// null potentially, in which case this is a noop. The string passed in is
Chris Lattner8ab406d2004-07-13 07:59:27 +0000521// assumed to be a malloc'd string buffer, and is free'd by this function.
522//
523static void setValueName(Value *V, char *NameStr) {
524 if (NameStr) {
525 std::string Name(NameStr); // Copy string
526 free(NameStr); // Free old string
Chris Lattnerc9aea522004-07-13 08:12:39 +0000527
Misha Brukman5a2a3822005-05-10 22:02:28 +0000528 if (V->getType() == Type::VoidTy)
Reid Spencer61c83e02006-08-18 08:43:06 +0000529 GenerateError("Can't assign name '" + Name+"' to value with void type!");
Misha Brukman5a2a3822005-05-10 22:02:28 +0000530
Chris Lattner8ab406d2004-07-13 07:59:27 +0000531 assert(inFunctionScope() && "Must be in function scope!");
532 SymbolTable &ST = CurFun.CurrentFunction->getSymbolTable();
533 if (ST.lookup(V->getType(), Name))
Reid Spencer61c83e02006-08-18 08:43:06 +0000534 GenerateError("Redefinition of value named '" + Name + "' in the '" +
Chris Lattner8ab406d2004-07-13 07:59:27 +0000535 V->getType()->getDescription() + "' type plane!");
Misha Brukman5a2a3822005-05-10 22:02:28 +0000536
Chris Lattnerc9aea522004-07-13 08:12:39 +0000537 // Set the name.
Chris Lattner262bb9a2005-03-05 19:04:07 +0000538 V->setName(Name);
Chris Lattner8ab406d2004-07-13 07:59:27 +0000539 }
540}
541
Chris Lattnerd88998d2004-07-14 08:23:52 +0000542/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
543/// this is a declaration, otherwise it is a definition.
Chris Lattnerb2b96672005-11-12 18:21:21 +0000544static GlobalVariable *
545ParseGlobalVariable(char *NameStr,GlobalValue::LinkageTypes Linkage,
546 bool isConstantGlobal, const Type *Ty,
547 Constant *Initializer) {
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000548 if (isa<FunctionType>(Ty))
Reid Spencer61c83e02006-08-18 08:43:06 +0000549 GenerateError("Cannot declare global vars of function type!");
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000550
Misha Brukman5a2a3822005-05-10 22:02:28 +0000551 const PointerType *PTy = PointerType::get(Ty);
Chris Lattnera09000d2004-07-14 23:03:46 +0000552
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000553 std::string Name;
554 if (NameStr) {
555 Name = NameStr; // Copy string
556 free(NameStr); // Free old string
Chris Lattnerd88998d2004-07-14 08:23:52 +0000557 }
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000558
Chris Lattner8a327842004-07-14 21:44:00 +0000559 // See if this global value was forward referenced. If so, recycle the
560 // object.
Misha Brukman5a2a3822005-05-10 22:02:28 +0000561 ValID ID;
Chris Lattner8a327842004-07-14 21:44:00 +0000562 if (!Name.empty()) {
563 ID = ValID::create((char*)Name.c_str());
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000564 } else {
Chris Lattner8a327842004-07-14 21:44:00 +0000565 ID = ValID::create((int)CurModule.Values[PTy].size());
566 }
567
568 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000569 // Move the global to the end of the list, from whereever it was
Chris Lattner8a327842004-07-14 21:44:00 +0000570 // previously inserted.
571 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
572 CurModule.CurrentModule->getGlobalList().remove(GV);
573 CurModule.CurrentModule->getGlobalList().push_back(GV);
574 GV->setInitializer(Initializer);
575 GV->setLinkage(Linkage);
576 GV->setConstant(isConstantGlobal);
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000577 InsertValue(GV, CurModule.Values);
Chris Lattnerb2b96672005-11-12 18:21:21 +0000578 return GV;
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000579 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000580
581 // If this global has a name, check to see if there is already a definition
582 // of this global in the module. If so, merge as appropriate. Note that
583 // this is really just a hack around problems in the CFE. :(
584 if (!Name.empty()) {
585 // We are a simple redefinition of a value, check to see if it is defined
586 // the same as the old one.
Misha Brukman5a2a3822005-05-10 22:02:28 +0000587 if (GlobalVariable *EGV =
Chris Lattnera09000d2004-07-14 23:03:46 +0000588 CurModule.CurrentModule->getGlobalVariable(Name, Ty)) {
589 // We are allowed to redefine a global variable in two circumstances:
Misha Brukman5a2a3822005-05-10 22:02:28 +0000590 // 1. If at least one of the globals is uninitialized or
Chris Lattnera09000d2004-07-14 23:03:46 +0000591 // 2. If both initializers have the same value.
592 //
593 if (!EGV->hasInitializer() || !Initializer ||
594 EGV->getInitializer() == Initializer) {
595
596 // Make sure the existing global version gets the initializer! Make
597 // sure that it also gets marked const if the new version is.
598 if (Initializer && !EGV->hasInitializer())
599 EGV->setInitializer(Initializer);
600 if (isConstantGlobal)
601 EGV->setConstant(true);
602 EGV->setLinkage(Linkage);
Chris Lattnerb2b96672005-11-12 18:21:21 +0000603 return EGV;
Chris Lattnera09000d2004-07-14 23:03:46 +0000604 }
605
Reid Spencer61c83e02006-08-18 08:43:06 +0000606 GenerateError("Redefinition of global variable named '" + Name +
Chris Lattnera09000d2004-07-14 23:03:46 +0000607 "' in the '" + Ty->getDescription() + "' type plane!");
608 }
609 }
610
611 // Otherwise there is no existing GV to use, create one now.
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000612 GlobalVariable *GV =
Misha Brukman5a2a3822005-05-10 22:02:28 +0000613 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000614 CurModule.CurrentModule);
615 InsertValue(GV, CurModule.Values);
Chris Lattnerb2b96672005-11-12 18:21:21 +0000616 return GV;
Chris Lattnerd88998d2004-07-14 08:23:52 +0000617}
Chris Lattner8ab406d2004-07-13 07:59:27 +0000618
Reid Spencer75719bf2004-05-26 21:48:31 +0000619// setTypeName - Set the specified type to the name given. The name may be
620// null potentially, in which case this is a noop. The string passed in is
621// assumed to be a malloc'd string buffer, and is freed by this function.
622//
623// This function returns true if the type has already been defined, but is
624// allowed to be redefined in the specified context. If the name is a new name
625// for the type plane, it is inserted and false is returned.
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000626static bool setTypeName(const Type *T, char *NameStr) {
Chris Lattnera09000d2004-07-14 23:03:46 +0000627 assert(!inFunctionScope() && "Can't give types function-local names!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000628 if (NameStr == 0) return false;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000629
Reid Spencer75719bf2004-05-26 21:48:31 +0000630 std::string Name(NameStr); // Copy string
631 free(NameStr); // Free old string
632
633 // We don't allow assigning names to void type
Misha Brukman5a2a3822005-05-10 22:02:28 +0000634 if (T == Type::VoidTy)
Reid Spencer61c83e02006-08-18 08:43:06 +0000635 GenerateError("Can't assign name '" + Name + "' to the void type!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000636
Chris Lattnera09000d2004-07-14 23:03:46 +0000637 // Set the type name, checking for conflicts as we do so.
638 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000639
Chris Lattnera09000d2004-07-14 23:03:46 +0000640 if (AlreadyExists) { // Inserting a name that is already defined???
641 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
642 assert(Existing && "Conflict but no matching type?");
643
Reid Spencer75719bf2004-05-26 21:48:31 +0000644 // There is only one case where this is allowed: when we are refining an
645 // opaque type. In this case, Existing will be an opaque type.
646 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
647 // We ARE replacing an opaque type!
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000648 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000649 return true;
650 }
651
652 // Otherwise, this is an attempt to redefine a type. That's okay if
653 // the redefinition is identical to the original. This will be so if
654 // Existing and T point to the same Type object. In this one case we
655 // allow the equivalent redefinition.
656 if (Existing == T) return true; // Yes, it's equal.
657
658 // Any other kind of (non-equivalent) redefinition is an error.
Reid Spencer61c83e02006-08-18 08:43:06 +0000659 GenerateError("Redefinition of type named '" + Name + "' in the '" +
Reid Spencer3271ed52004-07-18 00:08:11 +0000660 T->getDescription() + "' type plane!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000661 }
662
Reid Spencer75719bf2004-05-26 21:48:31 +0000663 return false;
664}
Chris Lattner8896eda2001-07-09 19:38:36 +0000665
Chris Lattner30c89792001-09-07 16:35:17 +0000666//===----------------------------------------------------------------------===//
667// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000668//
Chris Lattner8896eda2001-07-09 19:38:36 +0000669
Chris Lattner3b073862004-02-09 03:19:29 +0000670// TypeContains - Returns true if Ty directly contains E in it.
Chris Lattner30c89792001-09-07 16:35:17 +0000671//
672static bool TypeContains(const Type *Ty, const Type *E) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000673 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
Chris Lattner6d8dbec2004-12-08 16:13:53 +0000674 E) != Ty->subtype_end();
Chris Lattner3b073862004-02-09 03:19:29 +0000675}
676
677namespace {
678 struct UpRefRecord {
679 // NestingLevel - The number of nesting levels that need to be popped before
680 // this type is resolved.
681 unsigned NestingLevel;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000682
Chris Lattner3b073862004-02-09 03:19:29 +0000683 // LastContainedTy - This is the type at the current binding level for the
684 // type. Every time we reduce the nesting level, this gets updated.
685 const Type *LastContainedTy;
686
687 // UpRefTy - This is the actual opaque type that the upreference is
688 // represented with.
689 OpaqueType *UpRefTy;
690
691 UpRefRecord(unsigned NL, OpaqueType *URTy)
692 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
693 };
Chris Lattner30c89792001-09-07 16:35:17 +0000694}
Chris Lattner698b56e2001-07-20 19:15:08 +0000695
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000696// UpRefs - A list of the outstanding upreferences that need to be resolved.
Chris Lattner3b073862004-02-09 03:19:29 +0000697static std::vector<UpRefRecord> UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000698
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000699/// HandleUpRefs - Every time we finish a new layer of types, this function is
700/// called. It loops through the UpRefs vector, which is a list of the
701/// currently active types. For each type, if the up reference is contained in
702/// the newly completed type, we decrement the level count. When the level
703/// count reaches zero, the upreferenced type is the type that is passed in:
704/// thus we can complete the cycle.
705///
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000706static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner2c37c182004-02-09 03:03:10 +0000707 if (!ty->isAbstract()) return ty;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000708 PATypeHolder Ty(ty);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000709 UR_OUT("Type '" << Ty->getDescription() <<
Chris Lattner5084d032001-11-02 07:46:26 +0000710 "' newly formed. Resolving upreferences.\n" <<
711 UpRefs.size() << " upreferences active!\n");
Chris Lattner026a8ce2004-02-09 18:53:54 +0000712
713 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
714 // to zero), we resolve them all together before we resolve them to Ty. At
715 // the end of the loop, if there is anything to resolve to Ty, it will be in
716 // this variable.
717 OpaqueType *TypeToResolve = 0;
718
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000719 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000720 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
721 << UpRefs[i].second->getDescription() << ") = "
Reid Spencer3271ed52004-07-18 00:08:11 +0000722 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner3b073862004-02-09 03:19:29 +0000723 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
724 // Decrement level of upreference
725 unsigned Level = --UpRefs[i].NestingLevel;
726 UpRefs[i].LastContainedTy = Ty;
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000727 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Misha Brukman5a2a3822005-05-10 22:02:28 +0000728 if (Level == 0) { // Upreference should be resolved!
Chris Lattner026a8ce2004-02-09 18:53:54 +0000729 if (!TypeToResolve) {
730 TypeToResolve = UpRefs[i].UpRefTy;
731 } else {
732 UR_OUT(" * Resolving upreference for "
733 << UpRefs[i].second->getDescription() << "\n";
734 std::string OldName = UpRefs[i].UpRefTy->getDescription());
735 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
736 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
737 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
738 }
Reid Spencer3271ed52004-07-18 00:08:11 +0000739 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000740 --i; // Do not skip the next element...
Chris Lattner30c89792001-09-07 16:35:17 +0000741 }
742 }
Chris Lattner8896eda2001-07-09 19:38:36 +0000743 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000744
Chris Lattner026a8ce2004-02-09 18:53:54 +0000745 if (TypeToResolve) {
746 UR_OUT(" * Resolving upreference for "
747 << UpRefs[i].second->getDescription() << "\n";
Chris Lattner16af11d2004-02-09 21:03:38 +0000748 std::string OldName = TypeToResolve->getDescription());
Chris Lattner026a8ce2004-02-09 18:53:54 +0000749 TypeToResolve->refineAbstractTypeTo(Ty);
750 }
751
Chris Lattner8896eda2001-07-09 19:38:36 +0000752 return Ty;
753}
754
Chris Lattner30c89792001-09-07 16:35:17 +0000755
Chris Lattner6184feb2005-05-20 03:25:47 +0000756// common code from the two 'RunVMAsmParser' functions
757 static Module * RunParser(Module * M) {
758
759 llvmAsmlineno = 1; // Reset the current line number...
Andrew Lenharth558bc882005-06-18 18:34:52 +0000760 ObsoleteVarArgs = false;
Andrew Lenharthe78f50d2005-06-19 03:53:56 +0000761 NewVarArgs = false;
Chris Lattner6184feb2005-05-20 03:25:47 +0000762
763 CurModule.CurrentModule = M;
764 yyparse(); // Parse the file, potentially throwing exception
Reid Spencer61c83e02006-08-18 08:43:06 +0000765 if (!ParserResult)
766 return 0;
Chris Lattner6184feb2005-05-20 03:25:47 +0000767
768 Module *Result = ParserResult;
769 ParserResult = 0;
770
Andrew Lenharth31c98bf2005-06-20 15:41:37 +0000771 //Not all functions use vaarg, so make a second check for ObsoleteVarArgs
772 {
773 Function* F;
774 if ((F = Result->getNamedFunction("llvm.va_start"))
775 && F->getFunctionType()->getNumParams() == 0)
776 ObsoleteVarArgs = true;
777 if((F = Result->getNamedFunction("llvm.va_copy"))
778 && F->getFunctionType()->getNumParams() == 1)
779 ObsoleteVarArgs = true;
780 }
781
Andrew Lenharthe78f50d2005-06-19 03:53:56 +0000782 if (ObsoleteVarArgs && NewVarArgs)
Reid Spencer61c83e02006-08-18 08:43:06 +0000783 GenerateError("This file is corrupt: it uses both new and old style varargs");
Andrew Lenharthe78f50d2005-06-19 03:53:56 +0000784
Andrew Lenharth558bc882005-06-18 18:34:52 +0000785 if(ObsoleteVarArgs) {
786 if(Function* F = Result->getNamedFunction("llvm.va_start")) {
Andrew Lenharth213e5572005-06-22 21:04:42 +0000787 if (F->arg_size() != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +0000788 GenerateError("Obsolete va_start takes 0 argument!");
Andrew Lenharth558bc882005-06-18 18:34:52 +0000789
790 //foo = va_start()
791 // ->
792 //bar = alloca typeof(foo)
793 //va_start(bar)
794 //foo = load bar
795
796 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
797 const Type* ArgTy = F->getFunctionType()->getReturnType();
798 const Type* ArgTyPtr = PointerType::get(ArgTy);
799 Function* NF = Result->getOrInsertFunction("llvm.va_start",
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000800 RetTy, ArgTyPtr, (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000801
802 while (!F->use_empty()) {
803 CallInst* CI = cast<CallInst>(F->use_back());
804 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vastart.fix.1", CI);
805 new CallInst(NF, bar, "", CI);
806 Value* foo = new LoadInst(bar, "vastart.fix.2", CI);
807 CI->replaceAllUsesWith(foo);
808 CI->getParent()->getInstList().erase(CI);
809 }
810 Result->getFunctionList().erase(F);
811 }
812
813 if(Function* F = Result->getNamedFunction("llvm.va_end")) {
Andrew Lenharth213e5572005-06-22 21:04:42 +0000814 if(F->arg_size() != 1)
Reid Spencer61c83e02006-08-18 08:43:06 +0000815 GenerateError("Obsolete va_end takes 1 argument!");
Andrew Lenharth213e5572005-06-22 21:04:42 +0000816
Andrew Lenharth558bc882005-06-18 18:34:52 +0000817 //vaend foo
818 // ->
819 //bar = alloca 1 of typeof(foo)
820 //vaend bar
821 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
822 const Type* ArgTy = F->getFunctionType()->getParamType(0);
823 const Type* ArgTyPtr = PointerType::get(ArgTy);
824 Function* NF = Result->getOrInsertFunction("llvm.va_end",
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000825 RetTy, ArgTyPtr, (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000826
827 while (!F->use_empty()) {
828 CallInst* CI = cast<CallInst>(F->use_back());
829 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vaend.fix.1", CI);
Andrew Lenharth017fba92005-06-19 14:04:55 +0000830 new StoreInst(CI->getOperand(1), bar, CI);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000831 new CallInst(NF, bar, "", CI);
832 CI->getParent()->getInstList().erase(CI);
833 }
834 Result->getFunctionList().erase(F);
835 }
836
837 if(Function* F = Result->getNamedFunction("llvm.va_copy")) {
Andrew Lenharth213e5572005-06-22 21:04:42 +0000838 if(F->arg_size() != 1)
Reid Spencer61c83e02006-08-18 08:43:06 +0000839 GenerateError("Obsolete va_copy takes 1 argument!");
Andrew Lenharth558bc882005-06-18 18:34:52 +0000840 //foo = vacopy(bar)
841 // ->
842 //a = alloca 1 of typeof(foo)
Andrew Lenharth213e5572005-06-22 21:04:42 +0000843 //b = alloca 1 of typeof(foo)
844 //store bar -> b
845 //vacopy(a, b)
Andrew Lenharth558bc882005-06-18 18:34:52 +0000846 //foo = load a
847
848 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
849 const Type* ArgTy = F->getFunctionType()->getReturnType();
850 const Type* ArgTyPtr = PointerType::get(ArgTy);
851 Function* NF = Result->getOrInsertFunction("llvm.va_copy",
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000852 RetTy, ArgTyPtr, ArgTyPtr,
853 (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000854
855 while (!F->use_empty()) {
856 CallInst* CI = cast<CallInst>(F->use_back());
857 AllocaInst* a = new AllocaInst(ArgTy, 0, "vacopy.fix.1", CI);
Andrew Lenharth213e5572005-06-22 21:04:42 +0000858 AllocaInst* b = new AllocaInst(ArgTy, 0, "vacopy.fix.2", CI);
859 new StoreInst(CI->getOperand(1), b, CI);
860 new CallInst(NF, a, b, "", CI);
861 Value* foo = new LoadInst(a, "vacopy.fix.3", CI);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000862 CI->replaceAllUsesWith(foo);
863 CI->getParent()->getInstList().erase(CI);
864 }
865 Result->getFunctionList().erase(F);
866 }
867 }
868
Chris Lattner6184feb2005-05-20 03:25:47 +0000869 return Result;
870
871 }
872
Chris Lattner00950542001-06-06 20:29:01 +0000873//===----------------------------------------------------------------------===//
874// RunVMAsmParser - Define an interface to this parser
875//===----------------------------------------------------------------------===//
876//
Chris Lattner86427632004-07-14 06:39:48 +0000877Module *llvm::RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner6184feb2005-05-20 03:25:47 +0000878 set_scan_file(F);
879
Chris Lattnera2850432001-07-22 18:36:00 +0000880 CurFilename = Filename;
Chris Lattner6184feb2005-05-20 03:25:47 +0000881 return RunParser(new Module(CurFilename));
882}
Chris Lattner00950542001-06-06 20:29:01 +0000883
Chris Lattner6184feb2005-05-20 03:25:47 +0000884Module *llvm::RunVMAsmParser(const char * AsmString, Module * M) {
885 set_scan_string(AsmString);
Chris Lattner42570982003-11-26 07:24:58 +0000886
Chris Lattner6184feb2005-05-20 03:25:47 +0000887 CurFilename = "from_memory";
888 if (M == NULL) {
889 return RunParser(new Module (CurFilename));
890 } else {
891 return RunParser(M);
892 }
Chris Lattner00950542001-06-06 20:29:01 +0000893}
894
895%}
896
897%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000898 llvm::Module *ModuleVal;
899 llvm::Function *FunctionVal;
900 std::pair<llvm::PATypeHolder*, char*> *ArgVal;
901 llvm::BasicBlock *BasicBlockVal;
902 llvm::TerminatorInst *TermInstVal;
903 llvm::Instruction *InstVal;
904 llvm::Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000905
Brian Gaeked0fde302003-11-11 22:41:34 +0000906 const llvm::Type *PrimType;
907 llvm::PATypeHolder *TypeVal;
908 llvm::Value *ValueVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000909
Brian Gaeked0fde302003-11-11 22:41:34 +0000910 std::vector<std::pair<llvm::PATypeHolder*,char*> > *ArgList;
911 std::vector<llvm::Value*> *ValueList;
912 std::list<llvm::PATypeHolder> *TypeList;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000913 // Represent the RHS of PHI node
Brian Gaeked0fde302003-11-11 22:41:34 +0000914 std::list<std::pair<llvm::Value*,
Misha Brukman5a2a3822005-05-10 22:02:28 +0000915 llvm::BasicBlock*> > *PHIList;
Brian Gaeked0fde302003-11-11 22:41:34 +0000916 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
917 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000918
Brian Gaeked0fde302003-11-11 22:41:34 +0000919 llvm::GlobalValue::LinkageTypes Linkage;
Chris Lattner30c89792001-09-07 16:35:17 +0000920 int64_t SInt64Val;
921 uint64_t UInt64Val;
922 int SIntVal;
923 unsigned UIntVal;
924 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000925 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000926
Chris Lattner30c89792001-09-07 16:35:17 +0000927 char *StrVal; // This memory is strdup'd!
Brian Gaeked0fde302003-11-11 22:41:34 +0000928 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000929
Brian Gaeked0fde302003-11-11 22:41:34 +0000930 llvm::Instruction::BinaryOps BinaryOpVal;
931 llvm::Instruction::TermOps TermOpVal;
932 llvm::Instruction::MemoryOps MemOpVal;
933 llvm::Instruction::OtherOps OtherOpVal;
934 llvm::Module::Endianness Endianness;
Chris Lattner00950542001-06-06 20:29:01 +0000935}
936
Chris Lattner79df7c02002-03-26 18:01:55 +0000937%type <ModuleVal> Module FunctionList
938%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000939%type <BasicBlockVal> BasicBlock InstructionList
940%type <TermInstVal> BBTerminatorInst
941%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000942%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000943%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000944%type <ArgList> ArgList ArgListH
945%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000946%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000947%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000948%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000949%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000950%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +0000951%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner15c9c032003-09-08 18:20:29 +0000952%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattnerddb6db42005-05-06 05:51:46 +0000953%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
Chris Lattneraa2c8532006-01-25 22:26:43 +0000954%type <BoolVal> OptSideEffect // 'sideeffect' or not.
Chris Lattner4ad02e72003-04-16 20:28:45 +0000955%type <Linkage> OptLinkage
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000956%type <Endianness> BigOrLittle
Chris Lattner00950542001-06-06 20:29:01 +0000957
Chris Lattner2079fde2001-10-13 06:41:08 +0000958// ValueRef - Unresolved reference to a definition or BB
959%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000960%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000961// Tokens and types for handling constant integer values
962//
963// ESINT64VAL - A negative number within long long range
964%token <SInt64Val> ESINT64VAL
965
966// EUINT64VAL - A positive number within uns. long long range
967%token <UInt64Val> EUINT64VAL
968%type <SInt64Val> EINT64VAL
969
970%token <SIntVal> SINTVAL // Signed 32 bit ints...
971%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
972%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000973%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000974
975// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000976%type <TypeVal> Types TypesV UpRTypes UpRTypesV
977%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000978%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
979%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000980
Chris Lattner6c23f572003-08-22 05:42:10 +0000981%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
982%type <StrVal> Name OptName OptAssign
Chris Lattner87ac9722005-11-06 06:46:28 +0000983%type <UIntVal> OptAlign OptCAlign
Chris Lattnerb2b96672005-11-12 18:21:21 +0000984%type <StrVal> OptSection SectionString
Chris Lattner00950542001-06-06 20:29:01 +0000985
Chris Lattner91ef4602004-03-31 03:49:47 +0000986%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Chris Lattner164c3782005-11-12 00:11:10 +0000987%token DECLARE GLOBAL CONSTANT SECTION VOLATILE
Chris Lattner16710e92004-10-16 18:17:13 +0000988%token TO DOTDOTDOT NULL_TOK UNDEF CONST INTERNAL LINKONCE WEAK APPENDING
Nate Begeman14b05292005-11-05 09:21:28 +0000989%token OPAQUE NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG ALIGN
Chris Lattneraa2c8532006-01-25 22:26:43 +0000990%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
Chris Lattner515906d2006-05-19 21:28:34 +0000991%token CC_TOK CCC_TOK CSRETCC_TOK FASTCC_TOK COLDCC_TOK
Chris Lattnera8e8f162005-05-06 20:27:19 +0000992%type <UIntVal> OptCallingConv
Chris Lattner00950542001-06-06 20:29:01 +0000993
Misha Brukman5a2a3822005-05-10 22:02:28 +0000994// Basic Block Terminating Operators
Chris Lattner16710e92004-10-16 18:17:13 +0000995%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
Chris Lattner00950542001-06-06 20:29:01 +0000996
Misha Brukman5a2a3822005-05-10 22:02:28 +0000997// Binary Operators
Chris Lattner4a6482b2002-09-10 19:57:26 +0000998%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattner42c9e772001-10-20 09:32:59 +0000999%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +00001000%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +00001001
1002// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001003%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +00001004
Chris Lattner027dcc52001-07-08 21:10:27 +00001005// Other Operators
1006%type <OtherOpVal> ShiftOps
Robert Bocchino2def1b32006-01-17 20:06:25 +00001007%token <OtherOpVal> PHI_TOK CAST SELECT SHL SHR VAARG
Chris Lattner4c949082006-04-08 01:18:35 +00001008%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Andrew Lenharth558bc882005-06-18 18:34:52 +00001009%token VAARG_old VANEXT_old //OBSOLETE
Chris Lattnerddb6db42005-05-06 05:51:46 +00001010
Chris Lattner027dcc52001-07-08 21:10:27 +00001011
Chris Lattner00950542001-06-06 20:29:01 +00001012%start Module
1013%%
1014
1015// Handle constant integer size restriction and conversion...
1016//
Chris Lattner51727be2002-06-04 21:58:56 +00001017INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +00001018INTVAL : UINTVAL {
1019 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
Reid Spencer61c83e02006-08-18 08:43:06 +00001020 GEN_ERROR("Value too large for type!");
Chris Lattner00950542001-06-06 20:29:01 +00001021 $$ = (int32_t)$1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001022 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001023};
Chris Lattner00950542001-06-06 20:29:01 +00001024
1025
Chris Lattner51727be2002-06-04 21:58:56 +00001026EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +00001027EINT64VAL : EUINT64VAL {
1028 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
Reid Spencer61c83e02006-08-18 08:43:06 +00001029 GEN_ERROR("Value too large for type!");
Chris Lattner00950542001-06-06 20:29:01 +00001030 $$ = (int64_t)$1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001031 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001032};
Chris Lattner00950542001-06-06 20:29:01 +00001033
Misha Brukman5a2a3822005-05-10 22:02:28 +00001034// Operations that are notably excluded from this list include:
Chris Lattner00950542001-06-06 20:29:01 +00001035// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1036//
Chris Lattner4a6482b2002-09-10 19:57:26 +00001037ArithmeticOps: ADD | SUB | MUL | DIV | REM;
1038LogicalOps : AND | OR | XOR;
1039SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
Chris Lattner4a6482b2002-09-10 19:57:26 +00001040
Chris Lattner51727be2002-06-04 21:58:56 +00001041ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +00001042
Chris Lattnere98dda62001-07-14 06:10:16 +00001043// These are some types that allow classification if we only want a particular
1044// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +00001045SIntType : LONG | INT | SHORT | SBYTE;
1046UIntType : ULONG | UINT | USHORT | UBYTE;
1047IntType : SIntType | UIntType;
1048FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +00001049
Chris Lattnere98dda62001-07-14 06:10:16 +00001050// OptAssign - Value producing statements have an optional assignment component
Chris Lattner6c23f572003-08-22 05:42:10 +00001051OptAssign : Name '=' {
Chris Lattner00950542001-06-06 20:29:01 +00001052 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001053 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001054 }
Misha Brukman5a2a3822005-05-10 22:02:28 +00001055 | /*empty*/ {
1056 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00001057 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001058 };
Chris Lattner00950542001-06-06 20:29:01 +00001059
Chris Lattner4ad02e72003-04-16 20:28:45 +00001060OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
1061 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
Chris Lattner72ac148d2003-10-16 18:29:00 +00001062 WEAK { $$ = GlobalValue::WeakLinkage; } |
Chris Lattner4ad02e72003-04-16 20:28:45 +00001063 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
1064 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattner30c89792001-09-07 16:35:17 +00001065
Chris Lattnera8e8f162005-05-06 20:27:19 +00001066OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
1067 CCC_TOK { $$ = CallingConv::C; } |
Chris Lattner515906d2006-05-19 21:28:34 +00001068 CSRETCC_TOK { $$ = CallingConv::CSRet; } |
Chris Lattnera8e8f162005-05-06 20:27:19 +00001069 FASTCC_TOK { $$ = CallingConv::Fast; } |
1070 COLDCC_TOK { $$ = CallingConv::Cold; } |
1071 CC_TOK EUINT64VAL {
1072 if ((unsigned)$2 != $2)
Reid Spencer61c83e02006-08-18 08:43:06 +00001073 GEN_ERROR("Calling conv too large!");
Chris Lattnera8e8f162005-05-06 20:27:19 +00001074 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001075 CHECK_FOR_ERROR
Chris Lattnera8e8f162005-05-06 20:27:19 +00001076 };
1077
Chris Lattner66db8e42005-11-06 06:34:12 +00001078// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1079// a comma before it.
Chris Lattner87ac9722005-11-06 06:46:28 +00001080OptAlign : /*empty*/ { $$ = 0; } |
Chris Lattnerb2b96672005-11-12 18:21:21 +00001081 ALIGN EUINT64VAL {
1082 $$ = $2;
1083 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencer61c83e02006-08-18 08:43:06 +00001084 GEN_ERROR("Alignment must be a power of two!");
1085 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001086};
Chris Lattner66db8e42005-11-06 06:34:12 +00001087OptCAlign : /*empty*/ { $$ = 0; } |
Chris Lattnerb2b96672005-11-12 18:21:21 +00001088 ',' ALIGN EUINT64VAL {
1089 $$ = $3;
1090 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencer61c83e02006-08-18 08:43:06 +00001091 GEN_ERROR("Alignment must be a power of two!");
1092 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001093};
1094
Chris Lattner66db8e42005-11-06 06:34:12 +00001095
Chris Lattner164c3782005-11-12 00:11:10 +00001096SectionString : SECTION STRINGCONSTANT {
1097 for (unsigned i = 0, e = strlen($2); i != e; ++i)
1098 if ($2[i] == '"' || $2[i] == '\\')
Reid Spencer61c83e02006-08-18 08:43:06 +00001099 GEN_ERROR("Invalid character in section name!");
Chris Lattner164c3782005-11-12 00:11:10 +00001100 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001101 CHECK_FOR_ERROR
Chris Lattner164c3782005-11-12 00:11:10 +00001102};
1103
1104OptSection : /*empty*/ { $$ = 0; } |
1105 SectionString { $$ = $1; };
Chris Lattner164c3782005-11-12 00:11:10 +00001106
Chris Lattnerb2b96672005-11-12 18:21:21 +00001107// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1108// is set to be the global we are processing.
1109//
1110GlobalVarAttributes : /* empty */ {} |
1111 ',' GlobalVarAttribute GlobalVarAttributes {};
1112GlobalVarAttribute : SectionString {
1113 CurGV->setSection($1);
1114 free($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001115 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001116 }
1117 | ALIGN EUINT64VAL {
1118 if ($2 != 0 && !isPowerOf2_32($2))
Reid Spencer61c83e02006-08-18 08:43:06 +00001119 GEN_ERROR("Alignment must be a power of two!");
Chris Lattnerb2b96672005-11-12 18:21:21 +00001120 CurGV->setAlignment($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001121 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001122 };
Chris Lattner164c3782005-11-12 00:11:10 +00001123
Chris Lattner30c89792001-09-07 16:35:17 +00001124//===----------------------------------------------------------------------===//
1125// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +00001126// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +00001127// access to it, a user must explicitly use TypesV.
1128//
1129
1130// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +00001131TypesV : Types | VOID { $$ = new PATypeHolder($1); };
1132UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +00001133
1134Types : UpRTypes {
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001135 if (!UpRefs.empty())
Reid Spencer61c83e02006-08-18 08:43:06 +00001136 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001137 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001138 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001139 };
Chris Lattner30c89792001-09-07 16:35:17 +00001140
1141
1142// Derived types are added later...
1143//
Chris Lattner51727be2002-06-04 21:58:56 +00001144PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
1145PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001146UpRTypes : OPAQUE {
1147 $$ = new PATypeHolder(OpaqueType::get());
Reid Spencer61c83e02006-08-18 08:43:06 +00001148 CHECK_FOR_ERROR
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001149 }
1150 | PrimType {
1151 $$ = new PATypeHolder($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001152 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001153 };
Chris Lattnerd78700d2002-08-16 21:14:40 +00001154UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001155 $$ = new PATypeHolder(getTypeVal($1));
Reid Spencer61c83e02006-08-18 08:43:06 +00001156 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001157};
Chris Lattner30c89792001-09-07 16:35:17 +00001158
Chris Lattner30c89792001-09-07 16:35:17 +00001159// Include derived types in the Types production.
1160//
1161UpRTypes : '\\' EUINT64VAL { // Type UpReference
Reid Spencer61c83e02006-08-18 08:43:06 +00001162 if ($2 > (uint64_t)~0U) GEN_ERROR("Value out of range!");
Chris Lattner30c89792001-09-07 16:35:17 +00001163 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3b073862004-02-09 03:19:29 +00001164 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001165 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +00001166 UR_OUT("New Upreference!\n");
Reid Spencer61c83e02006-08-18 08:43:06 +00001167 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001168 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001169 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner9232b992003-04-22 18:42:41 +00001170 std::vector<const Type*> Params;
Chris Lattnera08976b2005-02-22 23:13:58 +00001171 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1172 E = $3->end(); I != E; ++I)
1173 Params.push_back(*I);
Chris Lattner2079fde2001-10-13 06:41:08 +00001174 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1175 if (isVarArg) Params.pop_back();
1176
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001177 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +00001178 delete $3; // Delete the argument list
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001179 delete $1; // Delete the return type handle
Reid Spencer61c83e02006-08-18 08:43:06 +00001180 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001181 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001182 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001183 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001184 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00001185 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001186 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001187 | '<' EUINT64VAL 'x' UpRTypes '>' { // Packed array type?
1188 const llvm::Type* ElemTy = $4->get();
Chris Lattner9547d7f2005-11-10 01:42:43 +00001189 if ((unsigned)$2 != $2)
Reid Spencer61c83e02006-08-18 08:43:06 +00001190 GEN_ERROR("Unsigned result not equal to signed result");
Chris Lattner9547d7f2005-11-10 01:42:43 +00001191 if (!ElemTy->isPrimitiveType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001192 GEN_ERROR("Elemental type of a PackedType must be primitive");
Chris Lattner9547d7f2005-11-10 01:42:43 +00001193 if (!isPowerOf2_32($2))
Reid Spencer61c83e02006-08-18 08:43:06 +00001194 GEN_ERROR("Vector length should be a power of 2!");
Brian Gaeke715c90b2004-08-20 06:00:58 +00001195 $$ = new PATypeHolder(HandleUpRefs(PackedType::get(*$4, (unsigned)$2)));
1196 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00001197 CHECK_FOR_ERROR
Brian Gaeke715c90b2004-08-20 06:00:58 +00001198 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001199 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001200 std::vector<const Type*> Elements;
Chris Lattnera08976b2005-02-22 23:13:58 +00001201 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
1202 E = $2->end(); I != E; ++I)
1203 Elements.push_back(*I);
Misha Brukman5a2a3822005-05-10 22:02:28 +00001204
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001205 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001206 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001207 CHECK_FOR_ERROR
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001208 }
1209 | '{' '}' { // Empty structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001210 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Reid Spencer61c83e02006-08-18 08:43:06 +00001211 CHECK_FOR_ERROR
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001212 }
1213 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001214 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001215 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001216 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001217 };
Chris Lattner30c89792001-09-07 16:35:17 +00001218
Chris Lattner7e708292002-06-25 16:13:24 +00001219// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +00001220// declaration type lists
1221//
1222TypeListI : UpRTypes {
Chris Lattner9232b992003-04-22 18:42:41 +00001223 $$ = new std::list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +00001224 $$->push_back(*$1); delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001225 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001226 }
1227 | TypeListI ',' UpRTypes {
1228 ($$=$1)->push_back(*$3); delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001229 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001230 };
Chris Lattner30c89792001-09-07 16:35:17 +00001231
Chris Lattner7e708292002-06-25 16:13:24 +00001232// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +00001233ArgTypeListI : TypeListI
1234 | TypeListI ',' DOTDOTDOT {
1235 ($$=$1)->push_back(Type::VoidTy);
Reid Spencer61c83e02006-08-18 08:43:06 +00001236 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001237 }
1238 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001239 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
Reid Spencer61c83e02006-08-18 08:43:06 +00001240 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001241 }
1242 | /*empty*/ {
Chris Lattner9232b992003-04-22 18:42:41 +00001243 $$ = new std::list<PATypeHolder>();
Reid Spencer61c83e02006-08-18 08:43:06 +00001244 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001245 };
Chris Lattner30c89792001-09-07 16:35:17 +00001246
Chris Lattnere98dda62001-07-14 06:10:16 +00001247// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +00001248// production is used ONLY to represent constants that show up AFTER a 'const',
1249// 'constant' or 'global' token at global scope. Constants that can be inlined
1250// into other expressions (such as integers and constexprs) are handled by the
1251// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +00001252//
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001253ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Chris Lattner949a3622003-07-23 15:30:06 +00001254 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001255 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001256 GEN_ERROR("Cannot make array constant with type: '" +
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001257 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001258 const Type *ETy = ATy->getElementType();
1259 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +00001260
Chris Lattner30c89792001-09-07 16:35:17 +00001261 // Verify that we have the correct size...
1262 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001263 GEN_ERROR("Type mismatch: constant sized array initialized with " +
Reid Spencer3271ed52004-07-18 00:08:11 +00001264 utostr($3->size()) + " arguments, but has size of " +
1265 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +00001266
Chris Lattner30c89792001-09-07 16:35:17 +00001267 // Verify all elements are correct type!
1268 for (unsigned i = 0; i < $3->size(); i++) {
1269 if (ETy != (*$3)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001270 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Reid Spencer3271ed52004-07-18 00:08:11 +00001271 ETy->getDescription() +"' as required!\nIt is of type '"+
1272 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +00001273 }
1274
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001275 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001276 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001277 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001278 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001279 | Types '[' ']' {
Chris Lattner949a3622003-07-23 15:30:06 +00001280 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001281 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001282 GEN_ERROR("Cannot make array constant with type: '" +
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001283 (*$1)->getDescription() + "'!");
1284
1285 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +00001286 if (NumElements != -1 && NumElements != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001287 GEN_ERROR("Type mismatch: constant sized array initialized with 0"
Reid Spencer3271ed52004-07-18 00:08:11 +00001288 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattner9232b992003-04-22 18:42:41 +00001289 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +00001290 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001291 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001292 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001293 | Types 'c' STRINGCONSTANT {
Chris Lattner949a3622003-07-23 15:30:06 +00001294 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001295 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001296 GEN_ERROR("Cannot make array constant with type: '" +
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001297 (*$1)->getDescription() + "'!");
1298
Chris Lattner30c89792001-09-07 16:35:17 +00001299 int NumElements = ATy->getNumElements();
1300 const Type *ETy = ATy->getElementType();
1301 char *EndStr = UnEscapeLexed($3, true);
1302 if (NumElements != -1 && NumElements != (EndStr-$3))
Reid Spencer61c83e02006-08-18 08:43:06 +00001303 GEN_ERROR("Can't build string constant of size " +
Reid Spencer3271ed52004-07-18 00:08:11 +00001304 itostr((int)(EndStr-$3)) +
1305 " when array has size " + itostr(NumElements) + "!");
Chris Lattner9232b992003-04-22 18:42:41 +00001306 std::vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +00001307 if (ETy == Type::SByteTy) {
Chris Lattneree454772006-01-23 23:05:15 +00001308 for (signed char *C = (signed char *)$3; C != (signed char *)EndStr; ++C)
Reid Spencer3271ed52004-07-18 00:08:11 +00001309 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +00001310 } else if (ETy == Type::UByteTy) {
Chris Lattneree454772006-01-23 23:05:15 +00001311 for (unsigned char *C = (unsigned char *)$3;
1312 C != (unsigned char*)EndStr; ++C)
1313 Vals.push_back(ConstantUInt::get(ETy, *C));
Chris Lattner93750fa2001-07-28 17:48:55 +00001314 } else {
Chris Lattner30c89792001-09-07 16:35:17 +00001315 free($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00001316 GEN_ERROR("Cannot build string arrays of non byte sized elements!");
Chris Lattner93750fa2001-07-28 17:48:55 +00001317 }
Chris Lattner30c89792001-09-07 16:35:17 +00001318 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001319 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +00001320 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001321 CHECK_FOR_ERROR
Chris Lattner93750fa2001-07-28 17:48:55 +00001322 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001323 | Types '<' ConstVector '>' { // Nonempty unsized arr
1324 const PackedType *PTy = dyn_cast<PackedType>($1->get());
1325 if (PTy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001326 GEN_ERROR("Cannot make packed constant with type: '" +
Brian Gaeke715c90b2004-08-20 06:00:58 +00001327 (*$1)->getDescription() + "'!");
1328 const Type *ETy = PTy->getElementType();
1329 int NumElements = PTy->getNumElements();
1330
1331 // Verify that we have the correct size...
1332 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001333 GEN_ERROR("Type mismatch: constant sized packed initialized with " +
Brian Gaeke715c90b2004-08-20 06:00:58 +00001334 utostr($3->size()) + " arguments, but has size of " +
1335 itostr(NumElements) + "!");
1336
1337 // Verify all elements are correct type!
1338 for (unsigned i = 0; i < $3->size(); i++) {
1339 if (ETy != (*$3)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001340 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Brian Gaeke715c90b2004-08-20 06:00:58 +00001341 ETy->getDescription() +"' as required!\nIt is of type '"+
1342 (*$3)[i]->getType()->getDescription() + "'.");
1343 }
1344
1345 $$ = ConstantPacked::get(PTy, *$3);
1346 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001347 CHECK_FOR_ERROR
Brian Gaeke715c90b2004-08-20 06:00:58 +00001348 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001349 | Types '{' ConstVector '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001350 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001351 if (STy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001352 GEN_ERROR("Cannot make struct constant with type: '" +
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001353 (*$1)->getDescription() + "'!");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001354
Chris Lattnerc6212f12003-05-21 16:06:56 +00001355 if ($3->size() != STy->getNumContainedTypes())
Reid Spencer61c83e02006-08-18 08:43:06 +00001356 GEN_ERROR("Illegal number of initializers for structure type!");
Chris Lattnerc6212f12003-05-21 16:06:56 +00001357
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001358 // Check to ensure that constants are compatible with the type initializer!
1359 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Chris Lattnerd21cd802004-02-09 04:37:31 +00001360 if ((*$3)[i]->getType() != STy->getElementType(i))
Reid Spencer61c83e02006-08-18 08:43:06 +00001361 GEN_ERROR("Expected type '" +
Chris Lattnerd21cd802004-02-09 04:37:31 +00001362 STy->getElementType(i)->getDescription() +
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001363 "' for element #" + utostr(i) +
1364 " of structure initializer!");
1365
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001366 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001367 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001368 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001369 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001370 | Types '{' '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001371 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001372 if (STy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001373 GEN_ERROR("Cannot make struct constant with type: '" +
Chris Lattnerc6212f12003-05-21 16:06:56 +00001374 (*$1)->getDescription() + "'!");
1375
1376 if (STy->getNumContainedTypes() != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001377 GEN_ERROR("Illegal number of initializers for structure type!");
Chris Lattnerc6212f12003-05-21 16:06:56 +00001378
1379 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1380 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001381 CHECK_FOR_ERROR
Chris Lattnerc6212f12003-05-21 16:06:56 +00001382 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001383 | Types NULL_TOK {
Chris Lattner949a3622003-07-23 15:30:06 +00001384 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001385 if (PTy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001386 GEN_ERROR("Cannot make null pointer constant with type: '" +
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001387 (*$1)->getDescription() + "'!");
1388
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001389 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001390 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001391 CHECK_FOR_ERROR
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001392 }
Chris Lattner16710e92004-10-16 18:17:13 +00001393 | Types UNDEF {
1394 $$ = UndefValue::get($1->get());
1395 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001396 CHECK_FOR_ERROR
Chris Lattner16710e92004-10-16 18:17:13 +00001397 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001398 | Types SymbolicValueRef {
Chris Lattner949a3622003-07-23 15:30:06 +00001399 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001400 if (Ty == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001401 GEN_ERROR("Global const reference must be a pointer type!");
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001402
Chris Lattner3101c252002-08-15 17:58:33 +00001403 // ConstExprs can exist in the body of a function, thus creating
Reid Spencer3271ed52004-07-18 00:08:11 +00001404 // GlobalValues whenever they refer to a variable. Because we are in
Chris Lattner3101c252002-08-15 17:58:33 +00001405 // the context of a function, getValNonImprovising will search the functions
1406 // symbol table instead of the module symbol table for the global symbol,
1407 // which throws things all off. To get around this, we just tell
1408 // getValNonImprovising that we are at global scope here.
1409 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001410 Function *SavedCurFn = CurFun.CurrentFunction;
1411 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001412
Chris Lattner2079fde2001-10-13 06:41:08 +00001413 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001414
Chris Lattner394f2eb2003-10-16 20:12:13 +00001415 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001416
Chris Lattner2079fde2001-10-13 06:41:08 +00001417 // If this is an initializer for a constant pointer, which is referencing a
1418 // (currently) undefined variable, create a stub now that shall be replaced
1419 // in the future with the right type of variable.
1420 //
1421 if (V == 0) {
1422 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1423 const PointerType *PT = cast<PointerType>(Ty);
1424
1425 // First check to see if the forward references value is already created!
1426 PerModuleInfo::GlobalRefsType::iterator I =
Reid Spencer3271ed52004-07-18 00:08:11 +00001427 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001428
1429 if (I != CurModule.GlobalRefs.end()) {
Reid Spencer3271ed52004-07-18 00:08:11 +00001430 V = I->second; // Placeholder already exists, use it...
Chris Lattnera989b232003-12-23 20:05:15 +00001431 $2.destroy();
Chris Lattner2079fde2001-10-13 06:41:08 +00001432 } else {
Chris Lattner8a327842004-07-14 21:44:00 +00001433 std::string Name;
Chris Lattnera09000d2004-07-14 23:03:46 +00001434 if ($2.Type == ValID::NameVal) Name = $2.Name;
Chris Lattner8a327842004-07-14 21:44:00 +00001435
Reid Spencer3271ed52004-07-18 00:08:11 +00001436 // Create the forward referenced global.
Chris Lattnera09000d2004-07-14 23:03:46 +00001437 GlobalValue *GV;
1438 if (const FunctionType *FTy =
1439 dyn_cast<FunctionType>(PT->getElementType())) {
1440 GV = new Function(FTy, GlobalValue::ExternalLinkage, Name,
1441 CurModule.CurrentModule);
1442 } else {
Reid Spencer3271ed52004-07-18 00:08:11 +00001443 GV = new GlobalVariable(PT->getElementType(), false,
Chris Lattnera09000d2004-07-14 23:03:46 +00001444 GlobalValue::ExternalLinkage, 0,
1445 Name, CurModule.CurrentModule);
1446 }
Chris Lattner8a327842004-07-14 21:44:00 +00001447
Reid Spencer3271ed52004-07-18 00:08:11 +00001448 // Keep track of the fact that we have a forward ref to recycle it
1449 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1450 V = GV;
Chris Lattner2079fde2001-10-13 06:41:08 +00001451 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001452 }
1453
Reid Spencer3271ed52004-07-18 00:08:11 +00001454 $$ = cast<GlobalValue>(V);
Chris Lattner2079fde2001-10-13 06:41:08 +00001455 delete $1; // Free the type handle
Reid Spencer61c83e02006-08-18 08:43:06 +00001456 CHECK_FOR_ERROR
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001457 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001458 | Types ConstExpr {
1459 if ($1->get() != $2->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001460 GEN_ERROR("Mismatched types for constant expression!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001461 $$ = $2;
1462 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001463 CHECK_FOR_ERROR
Chris Lattnerf4600482003-06-28 20:01:34 +00001464 }
1465 | Types ZEROINITIALIZER {
Chris Lattner78915932004-11-28 16:45:45 +00001466 const Type *Ty = $1->get();
1467 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
Reid Spencer61c83e02006-08-18 08:43:06 +00001468 GEN_ERROR("Cannot create a null initialized value of this type!");
Chris Lattner78915932004-11-28 16:45:45 +00001469 $$ = Constant::getNullValue(Ty);
Chris Lattnerf4600482003-06-28 20:01:34 +00001470 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001471 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001472 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001473
Chris Lattnere43f40b2002-10-09 00:25:32 +00001474ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001475 if (!ConstantSInt::isValueValidForType($1, $2))
Reid Spencer61c83e02006-08-18 08:43:06 +00001476 GEN_ERROR("Constant value doesn't fit in type!");
Chris Lattnerd05e3592002-08-15 18:17:28 +00001477 $$ = ConstantSInt::get($1, $2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001478 CHECK_FOR_ERROR
Chris Lattnere43f40b2002-10-09 00:25:32 +00001479 }
1480 | UIntType EUINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001481 if (!ConstantUInt::isValueValidForType($1, $2))
Reid Spencer61c83e02006-08-18 08:43:06 +00001482 GEN_ERROR("Constant value doesn't fit in type!");
Chris Lattnerd05e3592002-08-15 18:17:28 +00001483 $$ = ConstantUInt::get($1, $2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001484 CHECK_FOR_ERROR
Chris Lattnere43f40b2002-10-09 00:25:32 +00001485 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001486 | BOOL TRUETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001487 $$ = ConstantBool::True;
Reid Spencer61c83e02006-08-18 08:43:06 +00001488 CHECK_FOR_ERROR
Chris Lattnerd05e3592002-08-15 18:17:28 +00001489 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001490 | BOOL FALSETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001491 $$ = ConstantBool::False;
Reid Spencer61c83e02006-08-18 08:43:06 +00001492 CHECK_FOR_ERROR
Chris Lattnerd05e3592002-08-15 18:17:28 +00001493 }
1494 | FPType FPVAL { // Float & Double constants
Reid Spencer9f9b3ac2004-12-06 22:18:25 +00001495 if (!ConstantFP::isValueValidForType($1, $2))
Reid Spencer61c83e02006-08-18 08:43:06 +00001496 GEN_ERROR("Floating point constant invalid for type!!");
Chris Lattnerd05e3592002-08-15 18:17:28 +00001497 $$ = ConstantFP::get($1, $2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001498 CHECK_FOR_ERROR
Chris Lattnerd05e3592002-08-15 18:17:28 +00001499 };
1500
Chris Lattner00950542001-06-06 20:29:01 +00001501
Chris Lattnerd78700d2002-08-16 21:14:40 +00001502ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattnerae711a82003-11-21 20:27:35 +00001503 if (!$3->getType()->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001504 GEN_ERROR("cast constant expression from a non-primitive type: '" +
Chris Lattnerae711a82003-11-21 20:27:35 +00001505 $3->getType()->getDescription() + "'!");
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001506 if (!$5->get()->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001507 GEN_ERROR("cast constant expression to a non-primitive type: '" +
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001508 $5->get()->getDescription() + "'!");
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001509 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001510 delete $5;
Reid Spencer61c83e02006-08-18 08:43:06 +00001511 CHECK_FOR_ERROR
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001512 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001513 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1514 if (!isa<PointerType>($3->getType()))
Reid Spencer61c83e02006-08-18 08:43:06 +00001515 GEN_ERROR("GetElementPtr requires a pointer operand!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001516
Chris Lattner830b6f92004-04-05 01:30:04 +00001517 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
1518 // indices to uint struct indices for compatibility.
1519 generic_gep_type_iterator<std::vector<Value*>::iterator>
1520 GTI = gep_type_begin($3->getType(), $4->begin(), $4->end()),
1521 GTE = gep_type_end($3->getType(), $4->begin(), $4->end());
1522 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
1523 if (isa<StructType>(*GTI)) // Only change struct indices
1524 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
1525 if (CUI->getType() == Type::UByteTy)
1526 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
1527
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001528 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001529 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001530 if (!IdxTy)
Reid Spencer61c83e02006-08-18 08:43:06 +00001531 GEN_ERROR("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001532
Chris Lattner9232b992003-04-22 18:42:41 +00001533 std::vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001534 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1535 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001536 IdxVec.push_back(C);
1537 else
Reid Spencer61c83e02006-08-18 08:43:06 +00001538 GEN_ERROR("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001539
Chris Lattnerd78700d2002-08-16 21:14:40 +00001540 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001541
Chris Lattnerd78700d2002-08-16 21:14:40 +00001542 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Reid Spencer61c83e02006-08-18 08:43:06 +00001543 CHECK_FOR_ERROR
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001544 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001545 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1546 if ($3->getType() != Type::BoolTy)
Reid Spencer61c83e02006-08-18 08:43:06 +00001547 GEN_ERROR("Select condition must be of boolean type!");
Chris Lattner76f0ff32004-03-12 05:51:36 +00001548 if ($5->getType() != $7->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001549 GEN_ERROR("Select operand types must match!");
Chris Lattner76f0ff32004-03-12 05:51:36 +00001550 $$ = ConstantExpr::getSelect($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001551 CHECK_FOR_ERROR
Chris Lattner76f0ff32004-03-12 05:51:36 +00001552 }
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001553 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001554 if ($3->getType() != $5->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001555 GEN_ERROR("Binary operator types must match!");
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001556 // HACK: llvm 1.3 and earlier used to emit invalid pointer constant exprs.
1557 // To retain backward compatibility with these early compilers, we emit a
1558 // cast to the appropriate integer type automatically if we are in the
1559 // broken case. See PR424 for more information.
1560 if (!isa<PointerType>($3->getType())) {
1561 $$ = ConstantExpr::get($1, $3, $5);
1562 } else {
Chris Lattner42db1a02004-08-20 18:07:39 +00001563 const Type *IntPtrTy = 0;
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001564 switch (CurModule.CurrentModule->getPointerSize()) {
1565 case Module::Pointer32: IntPtrTy = Type::IntTy; break;
1566 case Module::Pointer64: IntPtrTy = Type::LongTy; break;
Reid Spencer61c83e02006-08-18 08:43:06 +00001567 default: GEN_ERROR("invalid pointer binary constant expr!");
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001568 }
1569 $$ = ConstantExpr::get($1, ConstantExpr::getCast($3, IntPtrTy),
1570 ConstantExpr::getCast($5, IntPtrTy));
1571 $$ = ConstantExpr::getCast($$, $3->getType());
1572 }
Reid Spencer61c83e02006-08-18 08:43:06 +00001573 CHECK_FOR_ERROR
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001574 }
1575 | LogicalOps '(' ConstVal ',' ConstVal ')' {
1576 if ($3->getType() != $5->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001577 GEN_ERROR("Logical operator types must match!");
Chris Lattner0a017832005-12-21 18:31:29 +00001578 if (!$3->getType()->isIntegral()) {
1579 if (!isa<PackedType>($3->getType()) ||
1580 !cast<PackedType>($3->getType())->getElementType()->isIntegral())
Reid Spencer61c83e02006-08-18 08:43:06 +00001581 GEN_ERROR("Logical operator requires integral operands!");
Chris Lattner0a017832005-12-21 18:31:29 +00001582 }
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001583 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001584 CHECK_FOR_ERROR
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001585 }
1586 | SetCondOps '(' ConstVal ',' ConstVal ')' {
1587 if ($3->getType() != $5->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001588 GEN_ERROR("setcc operand types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001589 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001590 CHECK_FOR_ERROR
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001591 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001592 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001593 if ($5->getType() != Type::UByteTy)
Reid Spencer61c83e02006-08-18 08:43:06 +00001594 GEN_ERROR("Shift count for shift constant must be unsigned byte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001595 if (!$3->getType()->isInteger())
Reid Spencer61c83e02006-08-18 08:43:06 +00001596 GEN_ERROR("Shift constant expression requires integer operand!");
Chris Lattnerb16689b2004-01-12 19:08:43 +00001597 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001598 CHECK_FOR_ERROR
Robert Bocchino9c62b562006-01-10 19:04:32 +00001599 }
1600 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Chris Lattner2d7349a2006-04-08 04:08:32 +00001601 if (!ExtractElementInst::isValidOperands($3, $5))
Reid Spencer61c83e02006-08-18 08:43:06 +00001602 GEN_ERROR("Invalid extractelement operands!");
Robert Bocchino9c62b562006-01-10 19:04:32 +00001603 $$ = ConstantExpr::getExtractElement($3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001604 CHECK_FOR_ERROR
Chris Lattnerca73cd82006-04-08 03:53:34 +00001605 }
1606 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Chris Lattner2d7349a2006-04-08 04:08:32 +00001607 if (!InsertElementInst::isValidOperands($3, $5, $7))
Reid Spencer61c83e02006-08-18 08:43:06 +00001608 GEN_ERROR("Invalid insertelement operands!");
Chris Lattnerca73cd82006-04-08 03:53:34 +00001609 $$ = ConstantExpr::getInsertElement($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001610 CHECK_FOR_ERROR
Chris Lattnerca73cd82006-04-08 03:53:34 +00001611 }
1612 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1613 if (!ShuffleVectorInst::isValidOperands($3, $5, $7))
Reid Spencer61c83e02006-08-18 08:43:06 +00001614 GEN_ERROR("Invalid shufflevector operands!");
Chris Lattnerca73cd82006-04-08 03:53:34 +00001615 $$ = ConstantExpr::getShuffleVector($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001616 CHECK_FOR_ERROR
Chris Lattner699f1eb2002-08-14 17:12:33 +00001617 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001618
Chris Lattnerca73cd82006-04-08 03:53:34 +00001619
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001620// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001621ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001622 ($$ = $1)->push_back($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00001623 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001624 }
1625 | ConstVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001626 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001627 $$->push_back($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001628 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001629 };
Chris Lattner00950542001-06-06 20:29:01 +00001630
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001631
Chris Lattner1781aca2001-09-18 04:00:54 +00001632// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001633GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001634
Chris Lattner00950542001-06-06 20:29:01 +00001635
Chris Lattner0e73ce62002-05-02 19:11:13 +00001636//===----------------------------------------------------------------------===//
1637// Rules to match Modules
1638//===----------------------------------------------------------------------===//
1639
1640// Module rule: Capture the result of parsing the whole file into a result
1641// variable...
1642//
1643Module : FunctionList {
1644 $$ = ParserResult = $1;
1645 CurModule.ModuleDone();
Reid Spencer61c83e02006-08-18 08:43:06 +00001646 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001647};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001648
Chris Lattner7e708292002-06-25 16:13:24 +00001649// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001650//
1651FunctionList : FunctionList Function {
1652 $$ = $1;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001653 CurFun.FunctionDone();
Reid Spencer61c83e02006-08-18 08:43:06 +00001654 CHECK_FOR_ERROR
Chris Lattner0e73ce62002-05-02 19:11:13 +00001655 }
1656 | FunctionList FunctionProto {
1657 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001658 CHECK_FOR_ERROR
Chris Lattner0e73ce62002-05-02 19:11:13 +00001659 }
Chris Lattner71cdba32006-01-24 00:40:17 +00001660 | FunctionList MODULE ASM_TOK AsmBlock {
Chris Lattneree454772006-01-23 23:05:15 +00001661 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001662 CHECK_FOR_ERROR
Chris Lattneree454772006-01-23 23:05:15 +00001663 }
Chris Lattner0e73ce62002-05-02 19:11:13 +00001664 | FunctionList IMPLEMENTATION {
1665 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001666 CHECK_FOR_ERROR
Chris Lattner0e73ce62002-05-02 19:11:13 +00001667 }
1668 | ConstPool {
1669 $$ = CurModule.CurrentModule;
Chris Lattner355ad1f2005-03-21 06:27:42 +00001670 // Emit an error if there are any unresolved types left.
1671 if (!CurModule.LateResolveTypes.empty()) {
1672 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
Reid Spencer61c83e02006-08-18 08:43:06 +00001673 if (DID.Type == ValID::NameVal) {
1674 GEN_ERROR("Reference to an undefined type: '"+DID.getName() + "'");
1675 } else {
1676 GEN_ERROR("Reference to an undefined type: #" + itostr(DID.Num));
1677 }
Chris Lattner355ad1f2005-03-21 06:27:42 +00001678 }
Reid Spencer61c83e02006-08-18 08:43:06 +00001679 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001680 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001681
Chris Lattnere98dda62001-07-14 06:10:16 +00001682// ConstPool - Constants with optional names assigned to them.
Chris Lattner355ad1f2005-03-21 06:27:42 +00001683ConstPool : ConstPool OptAssign TYPE TypesV {
Chris Lattner4a42e902001-10-22 05:56:09 +00001684 // Eagerly resolve types. This is not an optimization, this is a
1685 // requirement that is due to the fact that we could have this:
1686 //
1687 // %list = type { %list * }
1688 // %list = type { %list * } ; repeated type decl
1689 //
1690 // If types are not resolved eagerly, then the two types will not be
1691 // determined to be the same type!
1692 //
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001693 ResolveTypeTo($2, *$4);
Chris Lattner4a42e902001-10-22 05:56:09 +00001694
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001695 if (!setTypeName(*$4, $2) && !$2) {
1696 // If this is a named type that is not a redefinition, add it to the slot
1697 // table.
Chris Lattner355ad1f2005-03-21 06:27:42 +00001698 CurModule.Types.push_back(*$4);
Chris Lattner30c89792001-09-07 16:35:17 +00001699 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001700
1701 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00001702 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001703 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001704 | ConstPool FunctionProto { // Function prototypes can be in const pool
Reid Spencer61c83e02006-08-18 08:43:06 +00001705 CHECK_FOR_ERROR
Chris Lattner93750fa2001-07-28 17:48:55 +00001706 }
Chris Lattner71cdba32006-01-24 00:40:17 +00001707 | ConstPool MODULE ASM_TOK AsmBlock { // Asm blocks can be in the const pool
Reid Spencer61c83e02006-08-18 08:43:06 +00001708 CHECK_FOR_ERROR
Chris Lattneree454772006-01-23 23:05:15 +00001709 }
Chris Lattnerb2b96672005-11-12 18:21:21 +00001710 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Reid Spencer61c83e02006-08-18 08:43:06 +00001711 if ($5 == 0) GEN_ERROR("Global value initializer is not a constant!");
Chris Lattnerb2b96672005-11-12 18:21:21 +00001712 CurGV = ParseGlobalVariable($2, $3, $4, $5->getType(), $5);
1713 } GlobalVarAttributes {
1714 CurGV = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00001715 CHECK_FOR_ERROR
Chris Lattner1781aca2001-09-18 04:00:54 +00001716 }
Chris Lattnerb2b96672005-11-12 18:21:21 +00001717 | ConstPool OptAssign EXTERNAL GlobalType Types {
1718 CurGV = ParseGlobalVariable($2, GlobalValue::ExternalLinkage,
1719 $4, *$5, 0);
Chris Lattner1f862af2003-04-16 18:13:57 +00001720 delete $5;
Chris Lattnerb2b96672005-11-12 18:21:21 +00001721 } GlobalVarAttributes {
1722 CurGV = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00001723 CHECK_FOR_ERROR
Chris Lattnere98dda62001-07-14 06:10:16 +00001724 }
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001725 | ConstPool TARGET TargetDefinition {
Reid Spencer61c83e02006-08-18 08:43:06 +00001726 CHECK_FOR_ERROR
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001727 }
Reid Spencer0be13e72004-07-25 17:58:28 +00001728 | ConstPool DEPLIBS '=' LibrariesDefinition {
Reid Spencer61c83e02006-08-18 08:43:06 +00001729 CHECK_FOR_ERROR
Reid Spencer0be13e72004-07-25 17:58:28 +00001730 }
Chris Lattner00950542001-06-06 20:29:01 +00001731 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001732 };
Chris Lattner00950542001-06-06 20:29:01 +00001733
1734
Chris Lattneree454772006-01-23 23:05:15 +00001735AsmBlock : STRINGCONSTANT {
Chris Lattner66316012006-01-24 04:14:29 +00001736 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
Chris Lattneree454772006-01-23 23:05:15 +00001737 char *EndStr = UnEscapeLexed($1, true);
1738 std::string NewAsm($1, EndStr);
1739 free($1);
1740
1741 if (AsmSoFar.empty())
Chris Lattner66316012006-01-24 04:14:29 +00001742 CurModule.CurrentModule->setModuleInlineAsm(NewAsm);
Chris Lattneree454772006-01-23 23:05:15 +00001743 else
Chris Lattner66316012006-01-24 04:14:29 +00001744 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+NewAsm);
Reid Spencer61c83e02006-08-18 08:43:06 +00001745 CHECK_FOR_ERROR
Chris Lattneree454772006-01-23 23:05:15 +00001746};
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001747
1748BigOrLittle : BIG { $$ = Module::BigEndian; };
1749BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1750
1751TargetDefinition : ENDIAN '=' BigOrLittle {
1752 CurModule.CurrentModule->setEndianness($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00001753 CHECK_FOR_ERROR
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001754 }
1755 | POINTERSIZE '=' EUINT64VAL {
1756 if ($3 == 32)
1757 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1758 else if ($3 == 64)
1759 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1760 else
Reid Spencer61c83e02006-08-18 08:43:06 +00001761 GEN_ERROR("Invalid pointer size: '" + utostr($3) + "'!");
1762 CHECK_FOR_ERROR
Reid Spencer0be13e72004-07-25 17:58:28 +00001763 }
1764 | TRIPLE '=' STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001765 CurModule.CurrentModule->setTargetTriple($3);
1766 free($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00001767 CHECK_FOR_ERROR
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001768 };
1769
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001770LibrariesDefinition : '[' LibList ']';
Reid Spencer0be13e72004-07-25 17:58:28 +00001771
1772LibList : LibList ',' STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001773 CurModule.CurrentModule->addLibrary($3);
1774 free($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00001775 CHECK_FOR_ERROR
Reid Spencer0be13e72004-07-25 17:58:28 +00001776 }
1777 | STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001778 CurModule.CurrentModule->addLibrary($1);
1779 free($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001780 CHECK_FOR_ERROR
Reid Spencer0be13e72004-07-25 17:58:28 +00001781 }
1782 | /* empty: end of list */ {
Reid Spencer61c83e02006-08-18 08:43:06 +00001783 CHECK_FOR_ERROR
Reid Spencer0be13e72004-07-25 17:58:28 +00001784 }
1785 ;
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001786
Chris Lattner00950542001-06-06 20:29:01 +00001787//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001788// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001789//===----------------------------------------------------------------------===//
1790
Chris Lattner6c23f572003-08-22 05:42:10 +00001791Name : VAR_ID | STRINGCONSTANT;
1792OptName : Name | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001793
Chris Lattner6c23f572003-08-22 05:42:10 +00001794ArgVal : Types OptName {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001795 if (*$1 == Type::VoidTy)
Reid Spencer61c83e02006-08-18 08:43:06 +00001796 GEN_ERROR("void typed arguments are invalid!");
Chris Lattner9232b992003-04-22 18:42:41 +00001797 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001798 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001799};
Chris Lattner00950542001-06-06 20:29:01 +00001800
Chris Lattner69da5cf2002-10-13 20:57:00 +00001801ArgListH : ArgListH ',' ArgVal {
1802 $$ = $1;
1803 $1->push_back(*$3);
1804 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001805 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001806 }
1807 | ArgVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001808 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
Chris Lattner69da5cf2002-10-13 20:57:00 +00001809 $$->push_back(*$1);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001810 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001811 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001812 };
Chris Lattner00950542001-06-06 20:29:01 +00001813
1814ArgList : ArgListH {
1815 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001816 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001817 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001818 | ArgListH ',' DOTDOTDOT {
1819 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001820 $$->push_back(std::pair<PATypeHolder*,
1821 char*>(new PATypeHolder(Type::VoidTy), 0));
Reid Spencer61c83e02006-08-18 08:43:06 +00001822 CHECK_FOR_ERROR
Chris Lattner69da5cf2002-10-13 20:57:00 +00001823 }
1824 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001825 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1826 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
Reid Spencer61c83e02006-08-18 08:43:06 +00001827 CHECK_FOR_ERROR
Chris Lattner69da5cf2002-10-13 20:57:00 +00001828 }
Chris Lattner00950542001-06-06 20:29:01 +00001829 | /* empty */ {
1830 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00001831 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001832 };
Chris Lattner00950542001-06-06 20:29:01 +00001833
Chris Lattner164c3782005-11-12 00:11:10 +00001834FunctionHeaderH : OptCallingConv TypesV Name '(' ArgList ')'
1835 OptSection OptAlign {
Chris Lattnera8e8f162005-05-06 20:27:19 +00001836 UnEscapeLexed($3);
1837 std::string FunctionName($3);
1838 free($3); // Free strdup'd memory!
Chris Lattnerdda71962001-11-26 18:54:16 +00001839
Chris Lattnera8e8f162005-05-06 20:27:19 +00001840 if (!(*$2)->isFirstClassType() && *$2 != Type::VoidTy)
Reid Spencer61c83e02006-08-18 08:43:06 +00001841 GEN_ERROR("LLVM functions cannot return aggregate types!");
Chris Lattnerc282f5a2003-11-21 22:32:23 +00001842
Chris Lattner9232b992003-04-22 18:42:41 +00001843 std::vector<const Type*> ParamTypeList;
Chris Lattnera8e8f162005-05-06 20:27:19 +00001844 if ($5) { // If there are arguments...
1845 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $5->begin();
1846 I != $5->end(); ++I)
Chris Lattner69da5cf2002-10-13 20:57:00 +00001847 ParamTypeList.push_back(I->first->get());
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001848 }
Chris Lattner00950542001-06-06 20:29:01 +00001849
Chris Lattner2079fde2001-10-13 06:41:08 +00001850 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1851 if (isVarArg) ParamTypeList.pop_back();
1852
Chris Lattnera8e8f162005-05-06 20:27:19 +00001853 const FunctionType *FT = FunctionType::get(*$2, ParamTypeList, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001854 const PointerType *PFT = PointerType::get(FT);
Chris Lattnera8e8f162005-05-06 20:27:19 +00001855 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001856
Chris Lattnera09000d2004-07-14 23:03:46 +00001857 ValID ID;
1858 if (!FunctionName.empty()) {
1859 ID = ValID::create((char*)FunctionName.c_str());
1860 } else {
1861 ID = ValID::create((int)CurModule.Values[PFT].size());
1862 }
1863
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001864 Function *Fn = 0;
Chris Lattnera09000d2004-07-14 23:03:46 +00001865 // See if this function was forward referenced. If so, recycle the object.
1866 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
1867 // Move the function to the end of the list, from whereever it was
1868 // previously inserted.
1869 Fn = cast<Function>(FWRef);
1870 CurModule.CurrentModule->getFunctionList().remove(Fn);
1871 CurModule.CurrentModule->getFunctionList().push_back(Fn);
1872 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
1873 (Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1874 // If this is the case, either we need to be a forward decl, or it needs
1875 // to be.
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001876 if (!CurFun.isDeclare && !Fn->isExternal())
Reid Spencer61c83e02006-08-18 08:43:06 +00001877 GEN_ERROR("Redefinition of function '" + FunctionName + "'!");
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001878
Chris Lattnera09000d2004-07-14 23:03:46 +00001879 // Make sure to strip off any argument names so we can't get conflicts.
1880 if (Fn->isExternal())
Chris Lattnere4d5c442005-03-15 04:54:21 +00001881 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
Chris Lattnera09000d2004-07-14 23:03:46 +00001882 AI != AE; ++AI)
1883 AI->setName("");
Chris Lattner5659dd12002-07-15 00:10:33 +00001884
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001885 } else { // Not already defined?
1886 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
1887 CurModule.CurrentModule);
1888 InsertValue(Fn, CurModule.Values);
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001889 }
Chris Lattner00950542001-06-06 20:29:01 +00001890
Chris Lattner394f2eb2003-10-16 20:12:13 +00001891 CurFun.FunctionStart(Fn);
Chris Lattnera8e8f162005-05-06 20:27:19 +00001892 Fn->setCallingConv($1);
Chris Lattner164c3782005-11-12 00:11:10 +00001893 Fn->setAlignment($8);
1894 if ($7) {
1895 Fn->setSection($7);
1896 free($7);
1897 }
Chris Lattner00950542001-06-06 20:29:01 +00001898
Chris Lattner7e708292002-06-25 16:13:24 +00001899 // Add all of the arguments we parsed to the function...
Chris Lattnera8e8f162005-05-06 20:27:19 +00001900 if ($5) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001901 if (isVarArg) { // Nuke the last entry
Chris Lattnera8e8f162005-05-06 20:27:19 +00001902 assert($5->back().first->get() == Type::VoidTy && $5->back().second == 0&&
Chris Lattner69da5cf2002-10-13 20:57:00 +00001903 "Not a varargs marker!");
Chris Lattnera8e8f162005-05-06 20:27:19 +00001904 delete $5->back().first;
1905 $5->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00001906 }
Chris Lattnere4d5c442005-03-15 04:54:21 +00001907 Function::arg_iterator ArgIt = Fn->arg_begin();
Chris Lattnera8e8f162005-05-06 20:27:19 +00001908 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $5->begin();
1909 I != $5->end(); ++I, ++ArgIt) {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001910 delete I->first; // Delete the typeholder...
1911
Chris Lattner8ab406d2004-07-13 07:59:27 +00001912 setValueName(ArgIt, I->second); // Insert arg into symtab...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001913 InsertValue(ArgIt);
Chris Lattner00950542001-06-06 20:29:01 +00001914 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001915
Chris Lattnera8e8f162005-05-06 20:27:19 +00001916 delete $5; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00001917 }
Reid Spencer61c83e02006-08-18 08:43:06 +00001918 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001919};
Chris Lattner00950542001-06-06 20:29:01 +00001920
Chris Lattner9b02cc32002-05-03 18:23:48 +00001921BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1922
Chris Lattner4ad02e72003-04-16 20:28:45 +00001923FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00001924 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001925
Chris Lattner4ad02e72003-04-16 20:28:45 +00001926 // Make sure that we keep track of the linkage type even if there was a
1927 // previous "declare".
1928 $$->setLinkage($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001929};
Chris Lattner00950542001-06-06 20:29:01 +00001930
Chris Lattner9b02cc32002-05-03 18:23:48 +00001931END : ENDTOK | '}'; // Allow end of '}' to end a function
1932
Chris Lattner79df7c02002-03-26 18:01:55 +00001933Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001934 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001935 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001936};
Chris Lattner00950542001-06-06 20:29:01 +00001937
Chris Lattner394f2eb2003-10-16 20:12:13 +00001938FunctionProto : DECLARE { CurFun.isDeclare = true; } FunctionHeaderH {
1939 $$ = CurFun.CurrentFunction;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001940 CurFun.FunctionDone();
Reid Spencer61c83e02006-08-18 08:43:06 +00001941 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001942};
Chris Lattner00950542001-06-06 20:29:01 +00001943
1944//===----------------------------------------------------------------------===//
1945// Rules to match Basic Blocks
1946//===----------------------------------------------------------------------===//
1947
Chris Lattneraa2c8532006-01-25 22:26:43 +00001948OptSideEffect : /* empty */ {
1949 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00001950 CHECK_FOR_ERROR
Chris Lattneraa2c8532006-01-25 22:26:43 +00001951 }
1952 | SIDEEFFECT {
1953 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00001954 CHECK_FOR_ERROR
Chris Lattneraa2c8532006-01-25 22:26:43 +00001955 };
1956
Chris Lattner00950542001-06-06 20:29:01 +00001957ConstValueRef : ESINT64VAL { // A reference to a direct constant
1958 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001959 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001960 }
1961 | EUINT64VAL {
1962 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001963 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001964 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001965 | FPVAL { // Perhaps it's an FP constant?
1966 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001967 CHECK_FOR_ERROR
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001968 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001969 | TRUETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001970 $$ = ValID::create(ConstantBool::True);
Reid Spencer61c83e02006-08-18 08:43:06 +00001971 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001972 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001973 | FALSETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001974 $$ = ValID::create(ConstantBool::False);
Reid Spencer61c83e02006-08-18 08:43:06 +00001975 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001976 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001977 | NULL_TOK {
1978 $$ = ValID::createNull();
Reid Spencer61c83e02006-08-18 08:43:06 +00001979 CHECK_FOR_ERROR
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001980 }
Chris Lattner16710e92004-10-16 18:17:13 +00001981 | UNDEF {
1982 $$ = ValID::createUndef();
Reid Spencer61c83e02006-08-18 08:43:06 +00001983 CHECK_FOR_ERROR
Chris Lattner16710e92004-10-16 18:17:13 +00001984 }
Chris Lattnerf1f03df2005-12-21 17:53:02 +00001985 | ZEROINITIALIZER { // A vector zero constant.
1986 $$ = ValID::createZeroInit();
Reid Spencer61c83e02006-08-18 08:43:06 +00001987 CHECK_FOR_ERROR
Chris Lattnerf1f03df2005-12-21 17:53:02 +00001988 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001989 | '<' ConstVector '>' { // Nonempty unsized packed vector
1990 const Type *ETy = (*$2)[0]->getType();
1991 int NumElements = $2->size();
1992
1993 PackedType* pt = PackedType::get(ETy, NumElements);
1994 PATypeHolder* PTy = new PATypeHolder(
1995 HandleUpRefs(
1996 PackedType::get(
1997 ETy,
1998 NumElements)
1999 )
2000 );
2001
2002 // Verify all elements are correct type!
2003 for (unsigned i = 0; i < $2->size(); i++) {
2004 if (ETy != (*$2)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002005 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Brian Gaeke715c90b2004-08-20 06:00:58 +00002006 ETy->getDescription() +"' as required!\nIt is of type '" +
2007 (*$2)[i]->getType()->getDescription() + "'.");
2008 }
2009
2010 $$ = ValID::create(ConstantPacked::get(pt, *$2));
2011 delete PTy; delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002012 CHECK_FOR_ERROR
Brian Gaeke715c90b2004-08-20 06:00:58 +00002013 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00002014 | ConstExpr {
2015 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002016 CHECK_FOR_ERROR
Chris Lattneraa2c8532006-01-25 22:26:43 +00002017 }
2018 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
2019 char *End = UnEscapeLexed($3, true);
2020 std::string AsmStr = std::string($3, End);
2021 End = UnEscapeLexed($5, true);
2022 std::string Constraints = std::string($5, End);
2023 $$ = ValID::createInlineAsm(AsmStr, Constraints, $2);
2024 free($3);
2025 free($5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002026 CHECK_FOR_ERROR
Chris Lattnerd78700d2002-08-16 21:14:40 +00002027 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00002028
Chris Lattner2079fde2001-10-13 06:41:08 +00002029// SymbolicValueRef - Reference to one of two ways of symbolically refering to
2030// another value.
2031//
2032SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00002033 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002034 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002035 }
Chris Lattner6c23f572003-08-22 05:42:10 +00002036 | Name { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00002037 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002038 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002039 };
Chris Lattner2079fde2001-10-13 06:41:08 +00002040
2041// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00002042ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00002043
Chris Lattner00950542001-06-06 20:29:01 +00002044
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002045// ResolvedVal - a <type> <value> pair. This is used only in cases where the
2046// type immediately preceeds the value reference, and allows complex constant
2047// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00002048ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00002049 $$ = getVal(*$1, $2); delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002050 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002051 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00002052
Chris Lattner00950542001-06-06 20:29:01 +00002053BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner8ab406d2004-07-13 07:59:27 +00002054 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002055 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002056 }
Chris Lattner7e708292002-06-25 16:13:24 +00002057 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
Chris Lattner8ab406d2004-07-13 07:59:27 +00002058 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002059 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002060 };
Chris Lattner00950542001-06-06 20:29:01 +00002061
2062
2063// Basic blocks are terminated by branching instructions:
2064// br, br/cc, switch, ret
2065//
Chris Lattner2079fde2001-10-13 06:41:08 +00002066BasicBlock : InstructionList OptAssign BBTerminatorInst {
Chris Lattner8ab406d2004-07-13 07:59:27 +00002067 setValueName($3, $2);
Chris Lattner2079fde2001-10-13 06:41:08 +00002068 InsertValue($3);
2069
2070 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00002071 InsertValue($1);
2072 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002073 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002074 };
Chris Lattner00950542001-06-06 20:29:01 +00002075
2076InstructionList : InstructionList Inst {
2077 $1->getInstList().push_back($2);
2078 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002079 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002080 }
2081 | /* empty */ {
Andrew Lenharth558bc882005-06-18 18:34:52 +00002082 $$ = CurBB = getBBVal(ValID::create((int)CurFun.NextBBNum++), true);
Chris Lattner8d65a062004-07-26 01:40:20 +00002083
2084 // Make sure to move the basic block to the correct location in the
2085 // function, instead of leaving it inserted wherever it was first
2086 // referenced.
Chris Lattnerf924a4c2005-05-06 19:58:35 +00002087 Function::BasicBlockListType &BBL =
2088 CurFun.CurrentFunction->getBasicBlockList();
2089 BBL.splice(BBL.end(), BBL, $$);
Reid Spencer61c83e02006-08-18 08:43:06 +00002090 CHECK_FOR_ERROR
Chris Lattner22ae2322004-07-13 08:39:15 +00002091 }
2092 | LABELSTR {
Andrew Lenharth558bc882005-06-18 18:34:52 +00002093 $$ = CurBB = getBBVal(ValID::create($1), true);
Chris Lattner8d65a062004-07-26 01:40:20 +00002094
2095 // Make sure to move the basic block to the correct location in the
2096 // function, instead of leaving it inserted wherever it was first
2097 // referenced.
Chris Lattnerf924a4c2005-05-06 19:58:35 +00002098 Function::BasicBlockListType &BBL =
2099 CurFun.CurrentFunction->getBasicBlockList();
2100 BBL.splice(BBL.end(), BBL, $$);
Reid Spencer61c83e02006-08-18 08:43:06 +00002101 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002102 };
Chris Lattner00950542001-06-06 20:29:01 +00002103
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002104BBTerminatorInst : RET ResolvedVal { // Return with a result...
2105 $$ = new ReturnInst($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00002106 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002107 }
2108 | RET VOID { // Return with no result...
2109 $$ = new ReturnInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002110 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002111 }
2112 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner64116f92004-07-14 01:33:11 +00002113 $$ = new BranchInst(getBBVal($3));
Reid Spencer61c83e02006-08-18 08:43:06 +00002114 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002115 } // Conditional Branch...
2116 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner64116f92004-07-14 01:33:11 +00002117 $$ = new BranchInst(getBBVal($6), getBBVal($9), getVal(Type::BoolTy, $3));
Reid Spencer61c83e02006-08-18 08:43:06 +00002118 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002119 }
2120 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Chris Lattnerce1df9e2005-01-29 00:35:55 +00002121 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6), $8->size());
Chris Lattner00950542001-06-06 20:29:01 +00002122 $$ = S;
2123
Chris Lattner9232b992003-04-22 18:42:41 +00002124 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00002125 E = $8->end();
Chris Lattner69331f52005-02-24 05:25:17 +00002126 for (; I != E; ++I) {
2127 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
2128 S->addCase(CI, I->second);
2129 else
Reid Spencer61c83e02006-08-18 08:43:06 +00002130 GEN_ERROR("Switch case is constant, but not a simple integer!");
Chris Lattner69331f52005-02-24 05:25:17 +00002131 }
Chris Lattnerf57a43d2004-04-17 23:49:15 +00002132 delete $8;
Reid Spencer61c83e02006-08-18 08:43:06 +00002133 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002134 }
Chris Lattner196850c2003-05-15 21:30:00 +00002135 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Chris Lattnerce1df9e2005-01-29 00:35:55 +00002136 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6), 0);
Chris Lattner196850c2003-05-15 21:30:00 +00002137 $$ = S;
Reid Spencer61c83e02006-08-18 08:43:06 +00002138 CHECK_FOR_ERROR
Chris Lattner196850c2003-05-15 21:30:00 +00002139 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00002140 | INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
2141 TO LABEL ValueRef UNWIND LABEL ValueRef {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002142 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00002143 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00002144
Chris Lattnera8e8f162005-05-06 20:27:19 +00002145 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002146 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00002147 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00002148 std::vector<const Type*> ParamTypes;
Chris Lattnera8e8f162005-05-06 20:27:19 +00002149 if ($6) {
2150 for (std::vector<Value*>::iterator I = $6->begin(), E = $6->end();
Chris Lattner9232b992003-04-22 18:42:41 +00002151 I != E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00002152 ParamTypes.push_back((*I)->getType());
2153 }
2154
2155 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
2156 if (isVarArg) ParamTypes.pop_back();
2157
Chris Lattnera8e8f162005-05-06 20:27:19 +00002158 Ty = FunctionType::get($3->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002159 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00002160 }
Chris Lattner2079fde2001-10-13 06:41:08 +00002161
Chris Lattnera8e8f162005-05-06 20:27:19 +00002162 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00002163
Chris Lattnera8e8f162005-05-06 20:27:19 +00002164 BasicBlock *Normal = getBBVal($10);
2165 BasicBlock *Except = getBBVal($13);
Chris Lattner2079fde2001-10-13 06:41:08 +00002166
2167 // Create the call node...
Chris Lattnera8e8f162005-05-06 20:27:19 +00002168 if (!$6) { // Has no arguments?
Chris Lattner9232b992003-04-22 18:42:41 +00002169 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00002170 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00002171 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00002172 // correctly!
2173 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00002174 FunctionType::param_iterator I = Ty->param_begin();
2175 FunctionType::param_iterator E = Ty->param_end();
Chris Lattnera8e8f162005-05-06 20:27:19 +00002176 std::vector<Value*>::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00002177
2178 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
Reid Spencer3271ed52004-07-18 00:08:11 +00002179 if ((*ArgI)->getType() != *I)
Reid Spencer61c83e02006-08-18 08:43:06 +00002180 GEN_ERROR("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Reid Spencer3271ed52004-07-18 00:08:11 +00002181 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00002182
2183 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002184 GEN_ERROR("Invalid number of parameters detected!");
Chris Lattner2079fde2001-10-13 06:41:08 +00002185
Chris Lattnera8e8f162005-05-06 20:27:19 +00002186 $$ = new InvokeInst(V, Normal, Except, *$6);
Chris Lattner2079fde2001-10-13 06:41:08 +00002187 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00002188 cast<InvokeInst>($$)->setCallingConv($2);
2189
2190 delete $3;
2191 delete $6;
Reid Spencer61c83e02006-08-18 08:43:06 +00002192 CHECK_FOR_ERROR
Chris Lattner36143fc2003-09-08 18:54:55 +00002193 }
2194 | UNWIND {
2195 $$ = new UnwindInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002196 CHECK_FOR_ERROR
Chris Lattner16710e92004-10-16 18:17:13 +00002197 }
2198 | UNREACHABLE {
2199 $$ = new UnreachableInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002200 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002201 };
Chris Lattner2079fde2001-10-13 06:41:08 +00002202
2203
Chris Lattner00950542001-06-06 20:29:01 +00002204
2205JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
2206 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00002207 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00002208 if (V == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002209 GEN_ERROR("May only switch on a constant pool value!");
Chris Lattner00950542001-06-06 20:29:01 +00002210
Chris Lattner64116f92004-07-14 01:33:11 +00002211 $$->push_back(std::make_pair(V, getBBVal($6)));
Reid Spencer61c83e02006-08-18 08:43:06 +00002212 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002213 }
2214 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00002215 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00002216 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00002217
2218 if (V == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002219 GEN_ERROR("May only switch on a constant pool value!");
Chris Lattner00950542001-06-06 20:29:01 +00002220
Chris Lattner64116f92004-07-14 01:33:11 +00002221 $$->push_back(std::make_pair(V, getBBVal($5)));
Reid Spencer61c83e02006-08-18 08:43:06 +00002222 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002223 };
Chris Lattner00950542001-06-06 20:29:01 +00002224
2225Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00002226 // Is this definition named?? if so, assign the name...
Chris Lattner8ab406d2004-07-13 07:59:27 +00002227 setValueName($2, $1);
Chris Lattner00950542001-06-06 20:29:01 +00002228 InsertValue($2);
2229 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002230 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002231};
Chris Lattner00950542001-06-06 20:29:01 +00002232
Chris Lattnerc24d2082001-06-11 15:04:20 +00002233PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Chris Lattner9232b992003-04-22 18:42:41 +00002234 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Chris Lattner64116f92004-07-14 01:33:11 +00002235 $$->push_back(std::make_pair(getVal(*$1, $3), getBBVal($5)));
Chris Lattner30c89792001-09-07 16:35:17 +00002236 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002237 CHECK_FOR_ERROR
Chris Lattnerc24d2082001-06-11 15:04:20 +00002238 }
2239 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
2240 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00002241 $1->push_back(std::make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner64116f92004-07-14 01:33:11 +00002242 getBBVal($6)));
Reid Spencer61c83e02006-08-18 08:43:06 +00002243 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002244 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00002245
2246
Chris Lattner30c89792001-09-07 16:35:17 +00002247ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner9232b992003-04-22 18:42:41 +00002248 $$ = new std::vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002249 $$->push_back($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002250 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002251 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002252 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00002253 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002254 $1->push_back($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002255 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002256 };
Chris Lattner00950542001-06-06 20:29:01 +00002257
2258// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00002259ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00002260
Chris Lattnerddb6db42005-05-06 05:51:46 +00002261OptTailCall : TAIL CALL {
2262 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002263 CHECK_FOR_ERROR
Chris Lattnerddb6db42005-05-06 05:51:46 +00002264 }
2265 | CALL {
2266 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002267 CHECK_FOR_ERROR
Chris Lattnerddb6db42005-05-06 05:51:46 +00002268 };
2269
2270
2271
Chris Lattner4a6482b2002-09-10 19:57:26 +00002272InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Brian Gaeke715c90b2004-08-20 06:00:58 +00002273 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
2274 !isa<PackedType>((*$2).get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002275 GEN_ERROR(
Brian Gaeke715c90b2004-08-20 06:00:58 +00002276 "Arithmetic operator requires integer, FP, or packed operands!");
Misha Brukman5a2a3822005-05-10 22:02:28 +00002277 if (isa<PackedType>((*$2).get()) && $1 == Instruction::Rem)
Reid Spencer61c83e02006-08-18 08:43:06 +00002278 GEN_ERROR("Rem not supported on packed types!");
Chris Lattner4a6482b2002-09-10 19:57:26 +00002279 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
2280 if ($$ == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002281 GEN_ERROR("binary operator returned null!");
Chris Lattner4a6482b2002-09-10 19:57:26 +00002282 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002283 CHECK_FOR_ERROR
Chris Lattner4a6482b2002-09-10 19:57:26 +00002284 }
2285 | LogicalOps Types ValueRef ',' ValueRef {
Chris Lattner0a017832005-12-21 18:31:29 +00002286 if (!(*$2)->isIntegral()) {
2287 if (!isa<PackedType>($2->get()) ||
2288 !cast<PackedType>($2->get())->getElementType()->isIntegral())
Reid Spencer61c83e02006-08-18 08:43:06 +00002289 GEN_ERROR("Logical operator requires integral operands!");
Chris Lattner0a017832005-12-21 18:31:29 +00002290 }
Chris Lattner4a6482b2002-09-10 19:57:26 +00002291 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
2292 if ($$ == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002293 GEN_ERROR("binary operator returned null!");
Chris Lattner4a6482b2002-09-10 19:57:26 +00002294 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002295 CHECK_FOR_ERROR
Chris Lattner4a6482b2002-09-10 19:57:26 +00002296 }
2297 | SetCondOps Types ValueRef ',' ValueRef {
Reid Spencer57b6eec2004-08-21 16:11:02 +00002298 if(isa<PackedType>((*$2).get())) {
Reid Spencer61c83e02006-08-18 08:43:06 +00002299 GEN_ERROR(
Reid Spencer57b6eec2004-08-21 16:11:02 +00002300 "PackedTypes currently not supported in setcc instructions!");
2301 }
Chris Lattner1cff96a2002-09-10 22:37:46 +00002302 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00002303 if ($$ == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002304 GEN_ERROR("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00002305 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002306 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002307 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00002308 | NOT ResolvedVal {
2309 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
2310 << " Replacing with 'xor'.\n";
2311
2312 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
2313 if (Ones == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002314 GEN_ERROR("Expected integral type for not instruction!");
Chris Lattner699f1eb2002-08-14 17:12:33 +00002315
2316 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00002317 if ($$ == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002318 GEN_ERROR("Could not create a xor instruction!");
2319 CHECK_FOR_ERROR
Chris Lattner09083092001-07-08 04:57:15 +00002320 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002321 | ShiftOps ResolvedVal ',' ResolvedVal {
2322 if ($4->getType() != Type::UByteTy)
Reid Spencer61c83e02006-08-18 08:43:06 +00002323 GEN_ERROR("Shift amount must be ubyte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00002324 if (!$2->getType()->isInteger())
Reid Spencer61c83e02006-08-18 08:43:06 +00002325 GEN_ERROR("Shift constant expression requires integer operand!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002326 $$ = new ShiftInst($1, $2, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002327 CHECK_FOR_ERROR
Chris Lattner027dcc52001-07-08 21:10:27 +00002328 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002329 | CAST ResolvedVal TO Types {
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00002330 if (!$4->get()->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002331 GEN_ERROR("cast instruction to a non-primitive type: '" +
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00002332 $4->get()->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00002333 $$ = new CastInst($2, *$4);
2334 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00002335 CHECK_FOR_ERROR
Chris Lattner09083092001-07-08 04:57:15 +00002336 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00002337 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
2338 if ($2->getType() != Type::BoolTy)
Reid Spencer61c83e02006-08-18 08:43:06 +00002339 GEN_ERROR("select condition must be boolean!");
Chris Lattner76f0ff32004-03-12 05:51:36 +00002340 if ($4->getType() != $6->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002341 GEN_ERROR("select value types should match!");
Chris Lattner76f0ff32004-03-12 05:51:36 +00002342 $$ = new SelectInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002343 CHECK_FOR_ERROR
Chris Lattner76f0ff32004-03-12 05:51:36 +00002344 }
Chris Lattner99e7ab72003-10-18 05:53:13 +00002345 | VAARG ResolvedVal ',' Types {
Andrew Lenharthe78f50d2005-06-19 03:53:56 +00002346 NewVarArgs = true;
Chris Lattner99e7ab72003-10-18 05:53:13 +00002347 $$ = new VAArgInst($2, *$4);
2348 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00002349 CHECK_FOR_ERROR
Chris Lattner99e7ab72003-10-18 05:53:13 +00002350 }
Andrew Lenharth558bc882005-06-18 18:34:52 +00002351 | VAARG_old ResolvedVal ',' Types {
2352 ObsoleteVarArgs = true;
2353 const Type* ArgTy = $2->getType();
2354 Function* NF = CurModule.CurrentModule->
Jeff Cohen66c5fd62005-10-23 04:37:20 +00002355 getOrInsertFunction("llvm.va_copy", ArgTy, ArgTy, (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +00002356
2357 //b = vaarg a, t ->
2358 //foo = alloca 1 of t
2359 //bar = vacopy a
2360 //store bar -> foo
2361 //b = vaarg foo, t
2362 AllocaInst* foo = new AllocaInst(ArgTy, 0, "vaarg.fix");
2363 CurBB->getInstList().push_back(foo);
2364 CallInst* bar = new CallInst(NF, $2);
2365 CurBB->getInstList().push_back(bar);
2366 CurBB->getInstList().push_back(new StoreInst(bar, foo));
2367 $$ = new VAArgInst(foo, *$4);
2368 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00002369 CHECK_FOR_ERROR
Andrew Lenharth558bc882005-06-18 18:34:52 +00002370 }
2371 | VANEXT_old ResolvedVal ',' Types {
2372 ObsoleteVarArgs = true;
2373 const Type* ArgTy = $2->getType();
2374 Function* NF = CurModule.CurrentModule->
Jeff Cohen66c5fd62005-10-23 04:37:20 +00002375 getOrInsertFunction("llvm.va_copy", ArgTy, ArgTy, (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +00002376
2377 //b = vanext a, t ->
2378 //foo = alloca 1 of t
2379 //bar = vacopy a
2380 //store bar -> foo
2381 //tmp = vaarg foo, t
2382 //b = load foo
2383 AllocaInst* foo = new AllocaInst(ArgTy, 0, "vanext.fix");
2384 CurBB->getInstList().push_back(foo);
2385 CallInst* bar = new CallInst(NF, $2);
2386 CurBB->getInstList().push_back(bar);
2387 CurBB->getInstList().push_back(new StoreInst(bar, foo));
2388 Instruction* tmp = new VAArgInst(foo, *$4);
2389 CurBB->getInstList().push_back(tmp);
2390 $$ = new LoadInst(foo);
Chris Lattner8f77dae2003-05-08 02:44:12 +00002391 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00002392 CHECK_FOR_ERROR
Chris Lattner8f77dae2003-05-08 02:44:12 +00002393 }
Robert Bocchino9c62b562006-01-10 19:04:32 +00002394 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Chris Lattner2d7349a2006-04-08 04:08:32 +00002395 if (!ExtractElementInst::isValidOperands($2, $4))
Reid Spencer61c83e02006-08-18 08:43:06 +00002396 GEN_ERROR("Invalid extractelement operands!");
Robert Bocchino9c62b562006-01-10 19:04:32 +00002397 $$ = new ExtractElementInst($2, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002398 CHECK_FOR_ERROR
Robert Bocchino9c62b562006-01-10 19:04:32 +00002399 }
Robert Bocchino2def1b32006-01-17 20:06:25 +00002400 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Chris Lattner2d7349a2006-04-08 04:08:32 +00002401 if (!InsertElementInst::isValidOperands($2, $4, $6))
Reid Spencer61c83e02006-08-18 08:43:06 +00002402 GEN_ERROR("Invalid insertelement operands!");
Robert Bocchino2def1b32006-01-17 20:06:25 +00002403 $$ = new InsertElementInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002404 CHECK_FOR_ERROR
Robert Bocchino2def1b32006-01-17 20:06:25 +00002405 }
Chris Lattner4c949082006-04-08 01:18:35 +00002406 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
2407 if (!ShuffleVectorInst::isValidOperands($2, $4, $6))
Reid Spencer61c83e02006-08-18 08:43:06 +00002408 GEN_ERROR("Invalid shufflevector operands!");
Chris Lattner4c949082006-04-08 01:18:35 +00002409 $$ = new ShuffleVectorInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002410 CHECK_FOR_ERROR
Chris Lattner4c949082006-04-08 01:18:35 +00002411 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00002412 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00002413 const Type *Ty = $2->front().first->getType();
Chris Lattner8ea8f362003-10-30 01:38:18 +00002414 if (!Ty->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002415 GEN_ERROR("PHI node operands must be of first class type!");
Chris Lattnerc24d2082001-06-11 15:04:20 +00002416 $$ = new PHINode(Ty);
Chris Lattnerce1df9e2005-01-29 00:35:55 +00002417 ((PHINode*)$$)->reserveOperandSpace($2->size());
Chris Lattner00950542001-06-06 20:29:01 +00002418 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00002419 if ($2->front().first->getType() != Ty)
Reid Spencer61c83e02006-08-18 08:43:06 +00002420 GEN_ERROR("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00002421 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00002422 $2->pop_front();
2423 }
2424 delete $2; // Free the list...
Reid Spencer61c83e02006-08-18 08:43:06 +00002425 CHECK_FOR_ERROR
Chris Lattnerddb6db42005-05-06 05:51:46 +00002426 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00002427 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002428 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00002429 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00002430
Chris Lattnera8e8f162005-05-06 20:27:19 +00002431 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002432 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00002433 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00002434 std::vector<const Type*> ParamTypes;
Chris Lattnera8e8f162005-05-06 20:27:19 +00002435 if ($6) {
2436 for (std::vector<Value*>::iterator I = $6->begin(), E = $6->end();
Chris Lattner9232b992003-04-22 18:42:41 +00002437 I != E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00002438 ParamTypes.push_back((*I)->getType());
2439 }
Chris Lattner2079fde2001-10-13 06:41:08 +00002440
2441 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
2442 if (isVarArg) ParamTypes.pop_back();
2443
Chris Lattnera8e8f162005-05-06 20:27:19 +00002444 if (!(*$3)->isFirstClassType() && *$3 != Type::VoidTy)
Reid Spencer61c83e02006-08-18 08:43:06 +00002445 GEN_ERROR("LLVM functions cannot return aggregate types!");
Chris Lattner595f06c2005-02-01 01:47:42 +00002446
Chris Lattnera8e8f162005-05-06 20:27:19 +00002447 Ty = FunctionType::get($3->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002448 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00002449 }
Chris Lattner00950542001-06-06 20:29:01 +00002450
Chris Lattnera8e8f162005-05-06 20:27:19 +00002451 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00002452
Chris Lattner8b81bf52001-07-25 22:47:46 +00002453 // Create the call node...
Chris Lattnera8e8f162005-05-06 20:27:19 +00002454 if (!$6) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00002455 // Make sure no arguments is a good thing!
2456 if (Ty->getNumParams() != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002457 GEN_ERROR("No arguments passed to a function that "
Chris Lattnera4e25182002-07-25 20:52:56 +00002458 "expects arguments!");
2459
Chris Lattner9232b992003-04-22 18:42:41 +00002460 $$ = new CallInst(V, std::vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00002461 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00002462 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00002463 // correctly!
2464 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00002465 FunctionType::param_iterator I = Ty->param_begin();
2466 FunctionType::param_iterator E = Ty->param_end();
Chris Lattnera8e8f162005-05-06 20:27:19 +00002467 std::vector<Value*>::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00002468
2469 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
Reid Spencer3271ed52004-07-18 00:08:11 +00002470 if ((*ArgI)->getType() != *I)
Reid Spencer61c83e02006-08-18 08:43:06 +00002471 GEN_ERROR("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Reid Spencer3271ed52004-07-18 00:08:11 +00002472 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00002473
Chris Lattner8b81bf52001-07-25 22:47:46 +00002474 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002475 GEN_ERROR("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00002476
Chris Lattnera8e8f162005-05-06 20:27:19 +00002477 $$ = new CallInst(V, *$6);
Chris Lattner8b81bf52001-07-25 22:47:46 +00002478 }
Chris Lattnerddb6db42005-05-06 05:51:46 +00002479 cast<CallInst>($$)->setTailCall($1);
Chris Lattnera8e8f162005-05-06 20:27:19 +00002480 cast<CallInst>($$)->setCallingConv($2);
2481 delete $3;
2482 delete $6;
Reid Spencer61c83e02006-08-18 08:43:06 +00002483 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002484 }
2485 | MemoryInst {
2486 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002487 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002488 };
Chris Lattner00950542001-06-06 20:29:01 +00002489
Chris Lattner6cdb0112001-11-26 16:54:11 +00002490
2491// IndexList - List of indices for GEP based instructions...
2492IndexList : ',' ValueRefList {
Chris Lattner15c9c032003-09-08 18:20:29 +00002493 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002494 CHECK_FOR_ERROR
Chris Lattner15c9c032003-09-08 18:20:29 +00002495 } | /* empty */ {
2496 $$ = new std::vector<Value*>();
Reid Spencer61c83e02006-08-18 08:43:06 +00002497 CHECK_FOR_ERROR
Chris Lattner15c9c032003-09-08 18:20:29 +00002498 };
2499
2500OptVolatile : VOLATILE {
2501 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002502 CHECK_FOR_ERROR
Chris Lattner15c9c032003-09-08 18:20:29 +00002503 }
2504 | /* empty */ {
2505 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002506 CHECK_FOR_ERROR
Chris Lattner15c9c032003-09-08 18:20:29 +00002507 };
2508
Chris Lattner027dcc52001-07-08 21:10:27 +00002509
Chris Lattnerddb6db42005-05-06 05:51:46 +00002510
Chris Lattner66db8e42005-11-06 06:34:12 +00002511MemoryInst : MALLOC Types OptCAlign {
Chris Lattner66db8e42005-11-06 06:34:12 +00002512 $$ = new MallocInst(*$2, 0, $3);
Chris Lattner30c89792001-09-07 16:35:17 +00002513 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002514 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002515 }
Chris Lattner66db8e42005-11-06 06:34:12 +00002516 | MALLOC Types ',' UINT ValueRef OptCAlign {
Chris Lattner66db8e42005-11-06 06:34:12 +00002517 $$ = new MallocInst(*$2, getVal($4, $5), $6);
Nate Begeman14b05292005-11-05 09:21:28 +00002518 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002519 CHECK_FOR_ERROR
Nate Begeman14b05292005-11-05 09:21:28 +00002520 }
Chris Lattner66db8e42005-11-06 06:34:12 +00002521 | ALLOCA Types OptCAlign {
Chris Lattner66db8e42005-11-06 06:34:12 +00002522 $$ = new AllocaInst(*$2, 0, $3);
Nate Begeman14b05292005-11-05 09:21:28 +00002523 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002524 CHECK_FOR_ERROR
Nate Begeman14b05292005-11-05 09:21:28 +00002525 }
Chris Lattner66db8e42005-11-06 06:34:12 +00002526 | ALLOCA Types ',' UINT ValueRef OptCAlign {
Chris Lattner66db8e42005-11-06 06:34:12 +00002527 $$ = new AllocaInst(*$2, getVal($4, $5), $6);
Nate Begeman14b05292005-11-05 09:21:28 +00002528 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002529 CHECK_FOR_ERROR
Nate Begeman14b05292005-11-05 09:21:28 +00002530 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002531 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00002532 if (!isa<PointerType>($2->getType()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002533 GEN_ERROR("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00002534 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002535 $$ = new FreeInst($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00002536 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002537 }
2538
Chris Lattner15c9c032003-09-08 18:20:29 +00002539 | OptVolatile LOAD Types ValueRef {
2540 if (!isa<PointerType>($3->get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002541 GEN_ERROR("Can't load from nonpointer type: " +
Reid Spencer3271ed52004-07-18 00:08:11 +00002542 (*$3)->getDescription());
Chris Lattner1c1bb332004-10-09 02:18:58 +00002543 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002544 GEN_ERROR("Can't load from pointer of non-first-class type: " +
Chris Lattner1c1bb332004-10-09 02:18:58 +00002545 (*$3)->getDescription());
Chris Lattner79ad13802003-09-08 20:29:46 +00002546 $$ = new LoadInst(getVal(*$3, $4), "", $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002547 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002548 CHECK_FOR_ERROR
Chris Lattner027dcc52001-07-08 21:10:27 +00002549 }
Chris Lattner15c9c032003-09-08 18:20:29 +00002550 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
2551 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002552 if (!PT)
Reid Spencer61c83e02006-08-18 08:43:06 +00002553 GEN_ERROR("Can't store to a nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00002554 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002555 const Type *ElTy = PT->getElementType();
Chris Lattner15c9c032003-09-08 18:20:29 +00002556 if (ElTy != $3->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002557 GEN_ERROR("Can't store '" + $3->getType()->getDescription() +
Chris Lattner72e00252001-12-14 16:28:42 +00002558 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner0383cc42002-08-21 23:51:21 +00002559
Chris Lattner79ad13802003-09-08 20:29:46 +00002560 $$ = new StoreInst($3, getVal(*$5, $6), $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002561 delete $5;
Reid Spencer61c83e02006-08-18 08:43:06 +00002562 CHECK_FOR_ERROR
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002563 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00002564 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00002565 if (!isa<PointerType>($2->get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002566 GEN_ERROR("getelementptr insn requires pointer operand!");
Chris Lattner830b6f92004-04-05 01:30:04 +00002567
2568 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
2569 // indices to uint struct indices for compatibility.
2570 generic_gep_type_iterator<std::vector<Value*>::iterator>
2571 GTI = gep_type_begin($2->get(), $4->begin(), $4->end()),
2572 GTE = gep_type_end($2->get(), $4->begin(), $4->end());
2573 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
2574 if (isa<StructType>(*GTI)) // Only change struct indices
2575 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
2576 if (CUI->getType() == Type::UByteTy)
2577 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
2578
Chris Lattner30c89792001-09-07 16:35:17 +00002579 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Reid Spencer61c83e02006-08-18 08:43:06 +00002580 GEN_ERROR("Invalid getelementptr indices for type '" +
Chris Lattner830b6f92004-04-05 01:30:04 +00002581 (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00002582 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
2583 delete $2; delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00002584 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002585 };
Chris Lattner027dcc52001-07-08 21:10:27 +00002586
Brian Gaeked0fde302003-11-11 22:41:34 +00002587
Chris Lattner00950542001-06-06 20:29:01 +00002588%%
Reid Spencer61c83e02006-08-18 08:43:06 +00002589
2590void llvm::GenerateError(const std::string &message, int LineNo) {
2591 if (LineNo == -1) LineNo = llvmAsmlineno;
2592 // TODO: column number in exception
2593 if (TheParseError)
2594 TheParseError->setError(CurFilename, message, LineNo);
2595 TriggerError = 1;
2596}
2597
Chris Lattner09083092001-07-08 04:57:15 +00002598int yyerror(const char *ErrorMsg) {
Chris Lattner9232b992003-04-22 18:42:41 +00002599 std::string where
2600 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002601 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Chris Lattner9232b992003-04-22 18:42:41 +00002602 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
Chris Lattnerbc280ab2004-03-30 20:58:25 +00002603 if (yychar == YYEMPTY || yychar == 0)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002604 errMsg += "end-of-file.";
2605 else
Chris Lattner9232b992003-04-22 18:42:41 +00002606 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Reid Spencer61c83e02006-08-18 08:43:06 +00002607 GenerateError(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00002608 return 0;
2609}