blob: 425d2abb8913604ebf9f6323885a6c39f34bb647 [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files --------*- C++ -*-===//
Misha Brukman5a2a3822005-05-10 22:02:28 +00002//
John Criswell856ba762003-10-21 15:17:13 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman5a2a3822005-05-10 22:02:28 +00007//
John Criswell856ba762003-10-21 15:17:13 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This file implements the bison parser for LLVM assembly languages files.
11//
Chris Lattnercf3056d2003-10-13 03:32:08 +000012//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +000013
Chris Lattner00950542001-06-06 20:29:01 +000014%{
15#include "ParserInternals.h"
Chris Lattnera8e8f162005-05-06 20:27:19 +000016#include "llvm/CallingConv.h"
Chris Lattneraa2c8532006-01-25 22:26:43 +000017#include "llvm/InlineAsm.h"
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000018#include "llvm/Instructions.h"
Chris Lattner00950542001-06-06 20:29:01 +000019#include "llvm/Module.h"
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000020#include "llvm/SymbolTable.h"
Reid Spencer0b118202006-01-16 21:12:35 +000021#include "llvm/Assembly/AutoUpgrade.h"
Chris Lattner830b6f92004-04-05 01:30:04 +000022#include "llvm/Support/GetElementPtrTypeIterator.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000023#include "llvm/ADT/STLExtras.h"
Chris Lattner87ac9722005-11-06 06:46:28 +000024#include "llvm/Support/MathExtras.h"
Reid Spencer8ce1da72004-07-04 12:19:05 +000025#include <algorithm>
26#include <iostream>
Chris Lattner00950542001-06-06 20:29:01 +000027#include <list>
Chris Lattnerc188eeb2002-07-30 18:54:25 +000028#include <utility>
Chris Lattner00950542001-06-06 20:29:01 +000029
Reid Spencere4f47592006-08-18 17:32:55 +000030// The following is a gross hack. In order to rid the libAsmParser library of
31// exceptions, we have to have a way of getting the yyparse function to go into
32// an error situation. So, whenever we want an error to occur, the GenerateError
33// function (see bottom of file) sets TriggerError. Then, at the end of each
34// production in the grammer we use CHECK_FOR_ERROR which will invoke YYERROR
35// (a goto) to put YACC in error state. Furthermore, several calls to
36// GenerateError are made from inside productions and they must simulate the
37// previous exception behavior by exiting the production immediately. We have
38// replaced these with the GEN_ERROR macro which calls GeneratError and then
39// immediately invokes YYERROR. This would be so much cleaner if it was a
40// recursive descent parser.
Reid Spencer61c83e02006-08-18 08:43:06 +000041static bool TriggerError = false;
Reid Spencerf63697d2006-10-09 17:36:59 +000042#define CHECK_FOR_ERROR { if (TriggerError) { TriggerError = false; YYABORT; } }
Reid Spencer61c83e02006-08-18 08:43:06 +000043#define GEN_ERROR(msg) { GenerateError(msg); YYERROR; }
44
Chris Lattner386a3b72001-10-16 19:54:17 +000045int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
Chris Lattner09083092001-07-08 04:57:15 +000046int yylex(); // declaration" of xxx warnings.
Chris Lattner00950542001-06-06 20:29:01 +000047int yyparse();
48
Brian Gaeked0fde302003-11-11 22:41:34 +000049namespace llvm {
Chris Lattner86427632004-07-14 06:39:48 +000050 std::string CurFilename;
51}
52using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000053
Chris Lattner00950542001-06-06 20:29:01 +000054static Module *ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +000055
Chris Lattner30c89792001-09-07 16:35:17 +000056// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
57// relating to upreferences in the input stream.
58//
59//#define DEBUG_UPREFS 1
60#ifdef DEBUG_UPREFS
Chris Lattner699f1eb2002-08-14 17:12:33 +000061#define UR_OUT(X) std::cerr << X
Chris Lattner30c89792001-09-07 16:35:17 +000062#else
63#define UR_OUT(X)
64#endif
65
Vikram S. Adved3f7eb02002-07-14 22:59:28 +000066#define YYERROR_VERBOSE 1
67
Andrew Lenharth558bc882005-06-18 18:34:52 +000068static bool ObsoleteVarArgs;
Andrew Lenharthe78f50d2005-06-19 03:53:56 +000069static bool NewVarArgs;
Chris Lattnerb2b96672005-11-12 18:21:21 +000070static BasicBlock *CurBB;
71static GlobalVariable *CurGV;
Andrew Lenharth558bc882005-06-18 18:34:52 +000072
73
Chris Lattner7e708292002-06-25 16:13:24 +000074// This contains info used when building the body of a function. It is
75// destroyed when the function is completed.
Chris Lattner00950542001-06-06 20:29:01 +000076//
Chris Lattner9232b992003-04-22 18:42:41 +000077typedef std::vector<Value *> ValueList; // Numbered defs
Misha Brukman5a2a3822005-05-10 22:02:28 +000078static void
79ResolveDefinitions(std::map<const Type *,ValueList> &LateResolvers,
80 std::map<const Type *,ValueList> *FutureLateResolvers = 0);
Chris Lattner00950542001-06-06 20:29:01 +000081
82static struct PerModuleInfo {
83 Module *CurrentModule;
Chris Lattner735f2702004-07-08 22:30:50 +000084 std::map<const Type *, ValueList> Values; // Module level numbered definitions
85 std::map<const Type *,ValueList> LateResolveValues;
Chris Lattner16af11d2004-02-09 21:03:38 +000086 std::vector<PATypeHolder> Types;
Chris Lattner9232b992003-04-22 18:42:41 +000087 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000088
Chris Lattnerbc721ed2004-07-13 08:28:21 +000089 /// PlaceHolderInfo - When temporary placeholder objects are created, remember
Reid Spencerfd20c0a2006-05-29 02:34:34 +000090 /// how they were referenced and on which line of the input they came from so
Chris Lattner22ae2322004-07-13 08:39:15 +000091 /// that we can resolve them later and print error messages as appropriate.
Chris Lattnerbc721ed2004-07-13 08:28:21 +000092 std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
93
Chris Lattner2079fde2001-10-13 06:41:08 +000094 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
95 // references to global values. Global values may be referenced before they
96 // are defined, and if so, the temporary object that they represent is held
Reid Spencer3271ed52004-07-18 00:08:11 +000097 // here. This is used for forward references of GlobalValues.
Chris Lattner2079fde2001-10-13 06:41:08 +000098 //
Chris Lattner9232b992003-04-22 18:42:41 +000099 typedef std::map<std::pair<const PointerType *,
Chris Lattnerbc207592004-03-08 06:09:57 +0000100 ValID>, GlobalValue*> GlobalRefsType;
Chris Lattner2079fde2001-10-13 06:41:08 +0000101 GlobalRefsType GlobalRefs;
102
Chris Lattner00950542001-06-06 20:29:01 +0000103 void ModuleDone() {
Chris Lattner7e708292002-06-25 16:13:24 +0000104 // If we could not resolve some functions at function compilation time
105 // (calls to functions before they are defined), resolve them now... Types
106 // are resolved when the constant pool has been completely parsed.
Chris Lattner30c89792001-09-07 16:35:17 +0000107 //
Chris Lattner00950542001-06-06 20:29:01 +0000108 ResolveDefinitions(LateResolveValues);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000109 if (TriggerError)
110 return;
Chris Lattner00950542001-06-06 20:29:01 +0000111
Chris Lattner2079fde2001-10-13 06:41:08 +0000112 // Check to make sure that all global value forward references have been
113 // resolved!
114 //
115 if (!GlobalRefs.empty()) {
Chris Lattner9232b992003-04-22 18:42:41 +0000116 std::string UndefinedReferences = "Unresolved global references exist:\n";
Misha Brukman5a2a3822005-05-10 22:02:28 +0000117
Chris Lattner749ce032002-03-11 22:12:39 +0000118 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
119 I != E; ++I) {
120 UndefinedReferences += " " + I->first.first->getDescription() + " " +
121 I->first.second.getName() + "\n";
122 }
Reid Spencer61c83e02006-08-18 08:43:06 +0000123 GenerateError(UndefinedReferences);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000124 return;
Chris Lattner2079fde2001-10-13 06:41:08 +0000125 }
126
Reid Spencere812fb22006-01-19 01:21:04 +0000127 // Look for intrinsic functions and CallInst that need to be upgraded
Chris Lattner7d5c1e12006-03-04 07:53:16 +0000128 for (Module::iterator FI = CurrentModule->begin(),
129 FE = CurrentModule->end(); FI != FE; )
130 UpgradeCallsToIntrinsic(FI++);
Reid Spencer0b118202006-01-16 21:12:35 +0000131
Chris Lattner7e708292002-06-25 16:13:24 +0000132 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000133 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +0000134 CurrentModule = 0;
135 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000136
Chris Lattner8a327842004-07-14 21:44:00 +0000137 // GetForwardRefForGlobal - Check to see if there is a forward reference
138 // for this global. If so, remove it from the GlobalRefs map and return it.
139 // If not, just return null.
140 GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) {
141 // Check to see if there is a forward reference to this global variable...
142 // if there is, eliminate it and patch the reference to use the new def'n.
143 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID));
144 GlobalValue *Ret = 0;
145 if (I != GlobalRefs.end()) {
146 Ret = I->second;
147 GlobalRefs.erase(I);
148 }
149 return Ret;
150 }
Chris Lattner00950542001-06-06 20:29:01 +0000151} CurModule;
152
Chris Lattner79df7c02002-03-26 18:01:55 +0000153static struct PerFunctionInfo {
Chris Lattner7e708292002-06-25 16:13:24 +0000154 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner00950542001-06-06 20:29:01 +0000155
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000156 std::map<const Type*, ValueList> Values; // Keep track of #'d definitions
Chris Lattner735f2702004-07-08 22:30:50 +0000157 std::map<const Type*, ValueList> LateResolveValues;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000158 bool isDeclare; // Is this function a forward declararation?
159 GlobalValue::LinkageTypes Linkage; // Linkage for forward declaration.
Chris Lattner00950542001-06-06 20:29:01 +0000160
Chris Lattner2e2ed292004-07-14 06:28:35 +0000161 /// BBForwardRefs - When we see forward references to basic blocks, keep
162 /// track of them here.
163 std::map<BasicBlock*, std::pair<ValID, int> > BBForwardRefs;
164 std::vector<BasicBlock*> NumberedBlocks;
165 unsigned NextBBNum;
166
Chris Lattner79df7c02002-03-26 18:01:55 +0000167 inline PerFunctionInfo() {
168 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000169 isDeclare = false;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000170 Linkage = GlobalValue::ExternalLinkage;
Chris Lattner00950542001-06-06 20:29:01 +0000171 }
172
Chris Lattner79df7c02002-03-26 18:01:55 +0000173 inline void FunctionStart(Function *M) {
174 CurrentFunction = M;
Chris Lattner2e2ed292004-07-14 06:28:35 +0000175 NextBBNum = 0;
Chris Lattner00950542001-06-06 20:29:01 +0000176 }
177
Chris Lattner79df7c02002-03-26 18:01:55 +0000178 void FunctionDone() {
Chris Lattner2e2ed292004-07-14 06:28:35 +0000179 NumberedBlocks.clear();
180
181 // Any forward referenced blocks left?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000182 if (!BBForwardRefs.empty()) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000183 GenerateError("Undefined reference to label " +
Chris Lattner83beacd2005-02-24 04:59:49 +0000184 BBForwardRefs.begin()->first->getName());
Reid Spencer5b7e7532006-09-28 19:28:24 +0000185 return;
186 }
Chris Lattner2e2ed292004-07-14 06:28:35 +0000187
188 // Resolve all forward references now.
Chris Lattner386a3b72001-10-16 19:54:17 +0000189 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000190
Chris Lattner7e708292002-06-25 16:13:24 +0000191 Values.clear(); // Clear out function local definitions
Chris Lattner79df7c02002-03-26 18:01:55 +0000192 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000193 isDeclare = false;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000194 Linkage = GlobalValue::ExternalLinkage;
Chris Lattner00950542001-06-06 20:29:01 +0000195 }
Chris Lattner394f2eb2003-10-16 20:12:13 +0000196} CurFun; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000197
Chris Lattner394f2eb2003-10-16 20:12:13 +0000198static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000199
Chris Lattner00950542001-06-06 20:29:01 +0000200
201//===----------------------------------------------------------------------===//
202// Code to handle definitions of all the types
203//===----------------------------------------------------------------------===//
204
Chris Lattner735f2702004-07-08 22:30:50 +0000205static int InsertValue(Value *V,
206 std::map<const Type*,ValueList> &ValueTab = CurFun.Values) {
207 if (V->hasName()) return -1; // Is this a numbered definition?
Chris Lattner2079fde2001-10-13 06:41:08 +0000208
209 // Yes, insert the value into the value table...
Chris Lattner735f2702004-07-08 22:30:50 +0000210 ValueList &List = ValueTab[V->getType()];
211 List.push_back(V);
Chris Lattner16af11d2004-02-09 21:03:38 +0000212 return List.size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000213}
214
Chris Lattner30c89792001-09-07 16:35:17 +0000215static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000216 switch (D.Type) {
Chris Lattnera09000d2004-07-14 23:03:46 +0000217 case ValID::NumberVal: // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000218 // Module constants occupy the lowest numbered slots...
Misha Brukman5a2a3822005-05-10 22:02:28 +0000219 if ((unsigned)D.Num < CurModule.Types.size())
Chris Lattnera09000d2004-07-14 23:03:46 +0000220 return CurModule.Types[(unsigned)D.Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000221 break;
Chris Lattnera09000d2004-07-14 23:03:46 +0000222 case ValID::NameVal: // Is it a named definition?
223 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.Name)) {
224 D.destroy(); // Free old strdup'd memory...
225 return N;
Chris Lattner6e6026b2002-11-20 18:36:02 +0000226 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000227 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000228 default:
Reid Spencer61c83e02006-08-18 08:43:06 +0000229 GenerateError("Internal parser error: Invalid symbol type reference!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000230 return 0;
Chris Lattner30c89792001-09-07 16:35:17 +0000231 }
232
233 // If we reached here, we referenced either a symbol that we don't know about
234 // or an id number that hasn't been read yet. We may be referencing something
235 // forward, so just create an entry to be resolved later and get to it...
236 //
237 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
238
Chris Lattner355ad1f2005-03-21 06:27:42 +0000239
240 if (inFunctionScope()) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000241 if (D.Type == ValID::NameVal) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000242 GenerateError("Reference to an undefined type: '" + D.getName() + "'");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000243 return 0;
244 } else {
Reid Spencer61c83e02006-08-18 08:43:06 +0000245 GenerateError("Reference to an undefined type: #" + itostr(D.Num));
Reid Spencer5b7e7532006-09-28 19:28:24 +0000246 return 0;
247 }
Chris Lattner4a42e902001-10-22 05:56:09 +0000248 }
Chris Lattner30c89792001-09-07 16:35:17 +0000249
Chris Lattner355ad1f2005-03-21 06:27:42 +0000250 std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D);
251 if (I != CurModule.LateResolveTypes.end())
252 return I->second;
253
Chris Lattner82269592001-10-22 06:01:08 +0000254 Type *Typ = OpaqueType::get();
Chris Lattner355ad1f2005-03-21 06:27:42 +0000255 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
Chris Lattner30c89792001-09-07 16:35:17 +0000256 return Typ;
Chris Lattner355ad1f2005-03-21 06:27:42 +0000257 }
Chris Lattner30c89792001-09-07 16:35:17 +0000258
Chris Lattner9232b992003-04-22 18:42:41 +0000259static Value *lookupInSymbolTable(const Type *Ty, const std::string &Name) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000260 SymbolTable &SymTab =
Chris Lattner394f2eb2003-10-16 20:12:13 +0000261 inFunctionScope() ? CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner9705a152002-05-02 19:27:42 +0000262 CurModule.CurrentModule->getSymbolTable();
Chris Lattner6e6026b2002-11-20 18:36:02 +0000263 return SymTab.lookup(Ty, Name);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000264}
265
Chris Lattner2079fde2001-10-13 06:41:08 +0000266// getValNonImprovising - Look up the value specified by the provided type and
267// the provided ValID. If the value exists and has already been defined, return
268// it. Otherwise return null.
269//
270static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000271 if (isa<FunctionType>(Ty)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000272 GenerateError("Functions are not values and "
Chris Lattner79df7c02002-03-26 18:01:55 +0000273 "must be referenced as pointers");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000274 return 0;
275 }
Chris Lattner386a3b72001-10-16 19:54:17 +0000276
Chris Lattner30c89792001-09-07 16:35:17 +0000277 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000278 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner00950542001-06-06 20:29:01 +0000279 unsigned Num = (unsigned)D.Num;
280
281 // Module constants occupy the lowest numbered slots...
Chris Lattner735f2702004-07-08 22:30:50 +0000282 std::map<const Type*,ValueList>::iterator VI = CurModule.Values.find(Ty);
Chris Lattner16af11d2004-02-09 21:03:38 +0000283 if (VI != CurModule.Values.end()) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000284 if (Num < VI->second.size())
Chris Lattner16af11d2004-02-09 21:03:38 +0000285 return VI->second[Num];
286 Num -= VI->second.size();
Chris Lattner00950542001-06-06 20:29:01 +0000287 }
288
289 // Make sure that our type is within bounds
Chris Lattner735f2702004-07-08 22:30:50 +0000290 VI = CurFun.Values.find(Ty);
Chris Lattner16af11d2004-02-09 21:03:38 +0000291 if (VI == CurFun.Values.end()) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000292
293 // Check that the number is within bounds...
Chris Lattner16af11d2004-02-09 21:03:38 +0000294 if (VI->second.size() <= Num) return 0;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000295
Chris Lattner16af11d2004-02-09 21:03:38 +0000296 return VI->second[Num];
Chris Lattner00950542001-06-06 20:29:01 +0000297 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000298
Chris Lattner1a1cb112001-09-30 22:46:54 +0000299 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000300 Value *N = lookupInSymbolTable(Ty, std::string(D.Name));
Chris Lattner2079fde2001-10-13 06:41:08 +0000301 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000302
303 D.destroy(); // Free old strdup'd memory...
304 return N;
305 }
306
Misha Brukman5a2a3822005-05-10 22:02:28 +0000307 // Check to make sure that "Ty" is an integral type, and that our
Chris Lattner2079fde2001-10-13 06:41:08 +0000308 // value will fit into the specified type...
309 case ValID::ConstSIntVal: // Is it a constant pool reference??
Reid Spencerb83eb642006-10-20 07:07:24 +0000310 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000311 GenerateError("Signed integral constant '" +
Misha Brukman5a2a3822005-05-10 22:02:28 +0000312 itostr(D.ConstPool64) + "' is invalid for type '" +
Chris Lattnerd78700d2002-08-16 21:14:40 +0000313 Ty->getDescription() + "'!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000314 return 0;
315 }
Reid Spencerb83eb642006-10-20 07:07:24 +0000316 return ConstantInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000317
318 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Reid Spencerb83eb642006-10-20 07:07:24 +0000319 if (!ConstantInt::isValueValidForType(Ty, D.UConstPool64)) {
320 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000321 GenerateError("Integral constant '" + utostr(D.UConstPool64) +
Chris Lattnerf8dff732002-07-18 05:18:37 +0000322 "' is invalid or out of range!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000323 return 0;
Chris Lattner2079fde2001-10-13 06:41:08 +0000324 } else { // This is really a signed reference. Transmogrify.
Reid Spencerb83eb642006-10-20 07:07:24 +0000325 return ConstantInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000326 }
327 } else {
Reid Spencerb83eb642006-10-20 07:07:24 +0000328 return ConstantInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000329 }
330
Chris Lattner2079fde2001-10-13 06:41:08 +0000331 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000332 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000333 GenerateError("FP constant invalid for type!!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000334 return 0;
335 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000336 return ConstantFP::get(Ty, D.ConstPoolFP);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000337
Chris Lattner2079fde2001-10-13 06:41:08 +0000338 case ValID::ConstNullVal: // Is it a null value?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000339 if (!isa<PointerType>(Ty)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000340 GenerateError("Cannot create a a non pointer null!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000341 return 0;
342 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000343 return ConstantPointerNull::get(cast<PointerType>(Ty));
Misha Brukman5a2a3822005-05-10 22:02:28 +0000344
Chris Lattner16710e92004-10-16 18:17:13 +0000345 case ValID::ConstUndefVal: // Is it an undef value?
346 return UndefValue::get(Ty);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000347
Chris Lattnerf1f03df2005-12-21 17:53:02 +0000348 case ValID::ConstZeroVal: // Is it a zero value?
349 return Constant::getNullValue(Ty);
350
Chris Lattnerd78700d2002-08-16 21:14:40 +0000351 case ValID::ConstantVal: // Fully resolved constant?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000352 if (D.ConstantValue->getType() != Ty) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000353 GenerateError("Constant expression type different from required type!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000354 return 0;
355 }
Chris Lattnerd78700d2002-08-16 21:14:40 +0000356 return D.ConstantValue;
357
Chris Lattneraa2c8532006-01-25 22:26:43 +0000358 case ValID::InlineAsmVal: { // Inline asm expression
359 const PointerType *PTy = dyn_cast<PointerType>(Ty);
360 const FunctionType *FTy =
361 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000362 if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000363 GenerateError("Invalid type for asm constraint string!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000364 return 0;
365 }
Chris Lattneraa2c8532006-01-25 22:26:43 +0000366 InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints,
367 D.IAD->HasSideEffects);
368 D.destroy(); // Free InlineAsmDescriptor.
369 return IA;
370 }
Chris Lattner30c89792001-09-07 16:35:17 +0000371 default:
372 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000373 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000374 } // End of switch
375
Chris Lattner2079fde2001-10-13 06:41:08 +0000376 assert(0 && "Unhandled case!");
377 return 0;
378}
379
Chris Lattner2079fde2001-10-13 06:41:08 +0000380// getVal - This function is identical to getValNonImprovising, except that if a
381// value is not already defined, it "improvises" by creating a placeholder var
382// that looks and acts just like the requested variable. When the value is
383// defined later, all uses of the placeholder variable are replaced with the
384// real thing.
385//
Chris Lattner64116f92004-07-14 01:33:11 +0000386static Value *getVal(const Type *Ty, const ValID &ID) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000387 if (Ty == Type::LabelTy) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000388 GenerateError("Cannot use a basic block here");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000389 return 0;
390 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000391
Chris Lattner64116f92004-07-14 01:33:11 +0000392 // See if the value has already been defined.
393 Value *V = getValNonImprovising(Ty, ID);
Chris Lattner2079fde2001-10-13 06:41:08 +0000394 if (V) return V;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000395 if (TriggerError) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000396
Reid Spencer5b7e7532006-09-28 19:28:24 +0000397 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000398 GenerateError("Invalid use of a composite type!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000399 return 0;
400 }
Chris Lattner60cd9552005-03-23 01:29:26 +0000401
Chris Lattner00950542001-06-06 20:29:01 +0000402 // If we reached here, we referenced either a symbol that we don't know about
403 // or an id number that hasn't been read yet. We may be referencing something
404 // forward, so just create an entry to be resolved later and get to it...
405 //
Chris Lattner64116f92004-07-14 01:33:11 +0000406 V = new Argument(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000407
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000408 // Remember where this forward reference came from. FIXME, shouldn't we try
409 // to recycle these things??
Chris Lattner64116f92004-07-14 01:33:11 +0000410 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000411 llvmAsmlineno)));
412
Chris Lattner79df7c02002-03-26 18:01:55 +0000413 if (inFunctionScope())
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000414 InsertValue(V, CurFun.LateResolveValues);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000415 else
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000416 InsertValue(V, CurModule.LateResolveValues);
417 return V;
Chris Lattner00950542001-06-06 20:29:01 +0000418}
419
Chris Lattner2e2ed292004-07-14 06:28:35 +0000420/// getBBVal - This is used for two purposes:
421/// * If isDefinition is true, a new basic block with the specified ID is being
422/// defined.
423/// * If isDefinition is true, this is a reference to a basic block, which may
424/// or may not be a forward reference.
425///
426static BasicBlock *getBBVal(const ValID &ID, bool isDefinition = false) {
Chris Lattner64116f92004-07-14 01:33:11 +0000427 assert(inFunctionScope() && "Can't get basic block at global scope!");
428
Chris Lattner2e2ed292004-07-14 06:28:35 +0000429 std::string Name;
430 BasicBlock *BB = 0;
431 switch (ID.Type) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000432 default:
433 GenerateError("Illegal label reference " + ID.getName());
434 return 0;
Chris Lattner2e2ed292004-07-14 06:28:35 +0000435 case ValID::NumberVal: // Is it a numbered definition?
436 if (unsigned(ID.Num) >= CurFun.NumberedBlocks.size())
437 CurFun.NumberedBlocks.resize(ID.Num+1);
438 BB = CurFun.NumberedBlocks[ID.Num];
439 break;
440 case ValID::NameVal: // Is it a named definition?
441 Name = ID.Name;
Chris Lattnera09000d2004-07-14 23:03:46 +0000442 if (Value *N = CurFun.CurrentFunction->
443 getSymbolTable().lookup(Type::LabelTy, Name))
Chris Lattner2e2ed292004-07-14 06:28:35 +0000444 BB = cast<BasicBlock>(N);
445 break;
446 }
Chris Lattner64116f92004-07-14 01:33:11 +0000447
Chris Lattner2e2ed292004-07-14 06:28:35 +0000448 // See if the block has already been defined.
449 if (BB) {
450 // If this is the definition of the block, make sure the existing value was
451 // just a forward reference. If it was a forward reference, there will be
452 // an entry for it in the PlaceHolderInfo map.
Reid Spencer5b7e7532006-09-28 19:28:24 +0000453 if (isDefinition && !CurFun.BBForwardRefs.erase(BB)) {
Chris Lattner2e2ed292004-07-14 06:28:35 +0000454 // The existing value was a definition, not a forward reference.
Reid Spencer61c83e02006-08-18 08:43:06 +0000455 GenerateError("Redefinition of label " + ID.getName());
Reid Spencer5b7e7532006-09-28 19:28:24 +0000456 return 0;
457 }
Chris Lattner64116f92004-07-14 01:33:11 +0000458
Chris Lattner2e2ed292004-07-14 06:28:35 +0000459 ID.destroy(); // Free strdup'd memory.
460 return BB;
461 }
462
463 // Otherwise this block has not been seen before.
464 BB = new BasicBlock("", CurFun.CurrentFunction);
465 if (ID.Type == ValID::NameVal) {
466 BB->setName(ID.Name);
467 } else {
468 CurFun.NumberedBlocks[ID.Num] = BB;
469 }
470
471 // If this is not a definition, keep track of it so we can use it as a forward
472 // reference.
473 if (!isDefinition) {
474 // Remember where this forward reference came from.
475 CurFun.BBForwardRefs[BB] = std::make_pair(ID, llvmAsmlineno);
476 } else {
477 // The forward declaration could have been inserted anywhere in the
478 // function: insert it into the correct place now.
479 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
480 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
481 }
Chris Lattnere8343a52004-10-26 18:26:14 +0000482 ID.destroy();
Chris Lattner64116f92004-07-14 01:33:11 +0000483 return BB;
484}
485
Chris Lattner00950542001-06-06 20:29:01 +0000486
487//===----------------------------------------------------------------------===//
488// Code to handle forward references in instructions
489//===----------------------------------------------------------------------===//
490//
491// This code handles the late binding needed with statements that reference
492// values not defined yet... for example, a forward branch, or the PHI node for
493// a loop body.
494//
Chris Lattner394f2eb2003-10-16 20:12:13 +0000495// This keeps a table (CurFun.LateResolveValues) of all such forward references
Chris Lattner00950542001-06-06 20:29:01 +0000496// and back patchs after we are done.
497//
498
Misha Brukman5a2a3822005-05-10 22:02:28 +0000499// ResolveDefinitions - If we could not resolve some defs at parsing
500// time (forward branches, phi functions for loops, etc...) resolve the
Chris Lattner00950542001-06-06 20:29:01 +0000501// defs now...
502//
Misha Brukman5a2a3822005-05-10 22:02:28 +0000503static void
504ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers,
505 std::map<const Type*,ValueList> *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000506 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Chris Lattner735f2702004-07-08 22:30:50 +0000507 for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(),
Chris Lattner16af11d2004-02-09 21:03:38 +0000508 E = LateResolvers.end(); LRI != E; ++LRI) {
509 ValueList &List = LRI->second;
510 while (!List.empty()) {
511 Value *V = List.back();
512 List.pop_back();
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000513
514 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
515 CurModule.PlaceHolderInfo.find(V);
516 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
517
518 ValID &DID = PHI->second.first;
Chris Lattner00950542001-06-06 20:29:01 +0000519
Chris Lattner735f2702004-07-08 22:30:50 +0000520 Value *TheRealValue = getValNonImprovising(LRI->first, DID);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000521 if (TriggerError)
522 return;
Chris Lattner386a3b72001-10-16 19:54:17 +0000523 if (TheRealValue) {
524 V->replaceAllUsesWith(TheRealValue);
525 delete V;
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000526 CurModule.PlaceHolderInfo.erase(PHI);
Chris Lattner386a3b72001-10-16 19:54:17 +0000527 } else if (FutureLateResolvers) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000528 // Functions have their unresolved items forwarded to the module late
Chris Lattner386a3b72001-10-16 19:54:17 +0000529 // resolver table
530 InsertValue(V, *FutureLateResolvers);
531 } else {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000532 if (DID.Type == ValID::NameVal) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000533 GenerateError("Reference to an invalid definition: '" +DID.getName()+
Reid Spencer3271ed52004-07-18 00:08:11 +0000534 "' of type '" + V->getType()->getDescription() + "'",
535 PHI->second.second);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000536 return;
537 } else {
Reid Spencer61c83e02006-08-18 08:43:06 +0000538 GenerateError("Reference to an invalid definition: #" +
Misha Brukman5a2a3822005-05-10 22:02:28 +0000539 itostr(DID.Num) + " of type '" +
Reid Spencer3271ed52004-07-18 00:08:11 +0000540 V->getType()->getDescription() + "'",
541 PHI->second.second);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000542 return;
543 }
Chris Lattner30c89792001-09-07 16:35:17 +0000544 }
Chris Lattner00950542001-06-06 20:29:01 +0000545 }
546 }
547
548 LateResolvers.clear();
549}
550
Chris Lattner4a42e902001-10-22 05:56:09 +0000551// ResolveTypeTo - A brand new type was just declared. This means that (if
552// name is not null) things referencing Name can be resolved. Otherwise, things
553// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000554//
Chris Lattner4a42e902001-10-22 05:56:09 +0000555static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner355ad1f2005-03-21 06:27:42 +0000556 ValID D;
557 if (Name) D = ValID::create(Name);
558 else D = ValID::create((int)CurModule.Types.size());
Chris Lattner00950542001-06-06 20:29:01 +0000559
Chris Lattner355ad1f2005-03-21 06:27:42 +0000560 std::map<ValID, PATypeHolder>::iterator I =
561 CurModule.LateResolveTypes.find(D);
562 if (I != CurModule.LateResolveTypes.end()) {
563 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
564 CurModule.LateResolveTypes.erase(I);
Chris Lattner30c89792001-09-07 16:35:17 +0000565 }
566}
567
Chris Lattner1781aca2001-09-18 04:00:54 +0000568// setValueName - Set the specified value to the name given. The name may be
569// null potentially, in which case this is a noop. The string passed in is
Chris Lattner8ab406d2004-07-13 07:59:27 +0000570// assumed to be a malloc'd string buffer, and is free'd by this function.
571//
572static void setValueName(Value *V, char *NameStr) {
573 if (NameStr) {
574 std::string Name(NameStr); // Copy string
575 free(NameStr); // Free old string
Chris Lattnerc9aea522004-07-13 08:12:39 +0000576
Reid Spencer5b7e7532006-09-28 19:28:24 +0000577 if (V->getType() == Type::VoidTy) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000578 GenerateError("Can't assign name '" + Name+"' to value with void type!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000579 return;
580 }
Misha Brukman5a2a3822005-05-10 22:02:28 +0000581
Chris Lattner8ab406d2004-07-13 07:59:27 +0000582 assert(inFunctionScope() && "Must be in function scope!");
583 SymbolTable &ST = CurFun.CurrentFunction->getSymbolTable();
Reid Spencer5b7e7532006-09-28 19:28:24 +0000584 if (ST.lookup(V->getType(), Name)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000585 GenerateError("Redefinition of value named '" + Name + "' in the '" +
Chris Lattner8ab406d2004-07-13 07:59:27 +0000586 V->getType()->getDescription() + "' type plane!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000587 return;
588 }
Misha Brukman5a2a3822005-05-10 22:02:28 +0000589
Chris Lattnerc9aea522004-07-13 08:12:39 +0000590 // Set the name.
Chris Lattner262bb9a2005-03-05 19:04:07 +0000591 V->setName(Name);
Chris Lattner8ab406d2004-07-13 07:59:27 +0000592 }
593}
594
Chris Lattnerd88998d2004-07-14 08:23:52 +0000595/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
596/// this is a declaration, otherwise it is a definition.
Chris Lattnerb2b96672005-11-12 18:21:21 +0000597static GlobalVariable *
598ParseGlobalVariable(char *NameStr,GlobalValue::LinkageTypes Linkage,
599 bool isConstantGlobal, const Type *Ty,
600 Constant *Initializer) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000601 if (isa<FunctionType>(Ty)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000602 GenerateError("Cannot declare global vars of function type!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000603 return 0;
604 }
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000605
Misha Brukman5a2a3822005-05-10 22:02:28 +0000606 const PointerType *PTy = PointerType::get(Ty);
Chris Lattnera09000d2004-07-14 23:03:46 +0000607
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000608 std::string Name;
609 if (NameStr) {
610 Name = NameStr; // Copy string
611 free(NameStr); // Free old string
Chris Lattnerd88998d2004-07-14 08:23:52 +0000612 }
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000613
Chris Lattner8a327842004-07-14 21:44:00 +0000614 // See if this global value was forward referenced. If so, recycle the
615 // object.
Misha Brukman5a2a3822005-05-10 22:02:28 +0000616 ValID ID;
Chris Lattner8a327842004-07-14 21:44:00 +0000617 if (!Name.empty()) {
618 ID = ValID::create((char*)Name.c_str());
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000619 } else {
Chris Lattner8a327842004-07-14 21:44:00 +0000620 ID = ValID::create((int)CurModule.Values[PTy].size());
621 }
622
623 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000624 // Move the global to the end of the list, from whereever it was
Chris Lattner8a327842004-07-14 21:44:00 +0000625 // previously inserted.
626 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
627 CurModule.CurrentModule->getGlobalList().remove(GV);
628 CurModule.CurrentModule->getGlobalList().push_back(GV);
629 GV->setInitializer(Initializer);
630 GV->setLinkage(Linkage);
631 GV->setConstant(isConstantGlobal);
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000632 InsertValue(GV, CurModule.Values);
Chris Lattnerb2b96672005-11-12 18:21:21 +0000633 return GV;
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000634 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000635
636 // If this global has a name, check to see if there is already a definition
637 // of this global in the module. If so, merge as appropriate. Note that
638 // this is really just a hack around problems in the CFE. :(
639 if (!Name.empty()) {
640 // We are a simple redefinition of a value, check to see if it is defined
641 // the same as the old one.
Misha Brukman5a2a3822005-05-10 22:02:28 +0000642 if (GlobalVariable *EGV =
Chris Lattnera09000d2004-07-14 23:03:46 +0000643 CurModule.CurrentModule->getGlobalVariable(Name, Ty)) {
644 // We are allowed to redefine a global variable in two circumstances:
Misha Brukman5a2a3822005-05-10 22:02:28 +0000645 // 1. If at least one of the globals is uninitialized or
Chris Lattnera09000d2004-07-14 23:03:46 +0000646 // 2. If both initializers have the same value.
647 //
648 if (!EGV->hasInitializer() || !Initializer ||
649 EGV->getInitializer() == Initializer) {
650
651 // Make sure the existing global version gets the initializer! Make
652 // sure that it also gets marked const if the new version is.
653 if (Initializer && !EGV->hasInitializer())
654 EGV->setInitializer(Initializer);
655 if (isConstantGlobal)
656 EGV->setConstant(true);
657 EGV->setLinkage(Linkage);
Chris Lattnerb2b96672005-11-12 18:21:21 +0000658 return EGV;
Chris Lattnera09000d2004-07-14 23:03:46 +0000659 }
660
Reid Spencer61c83e02006-08-18 08:43:06 +0000661 GenerateError("Redefinition of global variable named '" + Name +
Chris Lattnera09000d2004-07-14 23:03:46 +0000662 "' in the '" + Ty->getDescription() + "' type plane!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000663 return 0;
Chris Lattnera09000d2004-07-14 23:03:46 +0000664 }
665 }
666
667 // Otherwise there is no existing GV to use, create one now.
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000668 GlobalVariable *GV =
Misha Brukman5a2a3822005-05-10 22:02:28 +0000669 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000670 CurModule.CurrentModule);
671 InsertValue(GV, CurModule.Values);
Chris Lattnerb2b96672005-11-12 18:21:21 +0000672 return GV;
Chris Lattnerd88998d2004-07-14 08:23:52 +0000673}
Chris Lattner8ab406d2004-07-13 07:59:27 +0000674
Reid Spencer75719bf2004-05-26 21:48:31 +0000675// setTypeName - Set the specified type to the name given. The name may be
676// null potentially, in which case this is a noop. The string passed in is
677// assumed to be a malloc'd string buffer, and is freed by this function.
678//
679// This function returns true if the type has already been defined, but is
680// allowed to be redefined in the specified context. If the name is a new name
681// for the type plane, it is inserted and false is returned.
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000682static bool setTypeName(const Type *T, char *NameStr) {
Chris Lattnera09000d2004-07-14 23:03:46 +0000683 assert(!inFunctionScope() && "Can't give types function-local names!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000684 if (NameStr == 0) return false;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000685
Reid Spencer75719bf2004-05-26 21:48:31 +0000686 std::string Name(NameStr); // Copy string
687 free(NameStr); // Free old string
688
689 // We don't allow assigning names to void type
Reid Spencer5b7e7532006-09-28 19:28:24 +0000690 if (T == Type::VoidTy) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000691 GenerateError("Can't assign name '" + Name + "' to the void type!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000692 return false;
693 }
Reid Spencer75719bf2004-05-26 21:48:31 +0000694
Chris Lattnera09000d2004-07-14 23:03:46 +0000695 // Set the type name, checking for conflicts as we do so.
696 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000697
Chris Lattnera09000d2004-07-14 23:03:46 +0000698 if (AlreadyExists) { // Inserting a name that is already defined???
699 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
700 assert(Existing && "Conflict but no matching type?");
701
Reid Spencer75719bf2004-05-26 21:48:31 +0000702 // There is only one case where this is allowed: when we are refining an
703 // opaque type. In this case, Existing will be an opaque type.
704 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
705 // We ARE replacing an opaque type!
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000706 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000707 return true;
708 }
709
710 // Otherwise, this is an attempt to redefine a type. That's okay if
711 // the redefinition is identical to the original. This will be so if
712 // Existing and T point to the same Type object. In this one case we
713 // allow the equivalent redefinition.
714 if (Existing == T) return true; // Yes, it's equal.
715
716 // Any other kind of (non-equivalent) redefinition is an error.
Reid Spencer61c83e02006-08-18 08:43:06 +0000717 GenerateError("Redefinition of type named '" + Name + "' in the '" +
Reid Spencer3271ed52004-07-18 00:08:11 +0000718 T->getDescription() + "' type plane!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000719 }
720
Reid Spencer75719bf2004-05-26 21:48:31 +0000721 return false;
722}
Chris Lattner8896eda2001-07-09 19:38:36 +0000723
Chris Lattner30c89792001-09-07 16:35:17 +0000724//===----------------------------------------------------------------------===//
725// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000726//
Chris Lattner8896eda2001-07-09 19:38:36 +0000727
Chris Lattner3b073862004-02-09 03:19:29 +0000728// TypeContains - Returns true if Ty directly contains E in it.
Chris Lattner30c89792001-09-07 16:35:17 +0000729//
730static bool TypeContains(const Type *Ty, const Type *E) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000731 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
Chris Lattner6d8dbec2004-12-08 16:13:53 +0000732 E) != Ty->subtype_end();
Chris Lattner3b073862004-02-09 03:19:29 +0000733}
734
735namespace {
736 struct UpRefRecord {
737 // NestingLevel - The number of nesting levels that need to be popped before
738 // this type is resolved.
739 unsigned NestingLevel;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000740
Chris Lattner3b073862004-02-09 03:19:29 +0000741 // LastContainedTy - This is the type at the current binding level for the
742 // type. Every time we reduce the nesting level, this gets updated.
743 const Type *LastContainedTy;
744
745 // UpRefTy - This is the actual opaque type that the upreference is
746 // represented with.
747 OpaqueType *UpRefTy;
748
749 UpRefRecord(unsigned NL, OpaqueType *URTy)
750 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
751 };
Chris Lattner30c89792001-09-07 16:35:17 +0000752}
Chris Lattner698b56e2001-07-20 19:15:08 +0000753
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000754// UpRefs - A list of the outstanding upreferences that need to be resolved.
Chris Lattner3b073862004-02-09 03:19:29 +0000755static std::vector<UpRefRecord> UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000756
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000757/// HandleUpRefs - Every time we finish a new layer of types, this function is
758/// called. It loops through the UpRefs vector, which is a list of the
759/// currently active types. For each type, if the up reference is contained in
760/// the newly completed type, we decrement the level count. When the level
761/// count reaches zero, the upreferenced type is the type that is passed in:
762/// thus we can complete the cycle.
763///
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000764static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner703e92f2006-08-18 17:34:24 +0000765 // If Ty isn't abstract, or if there are no up-references in it, then there is
766 // nothing to resolve here.
767 if (!ty->isAbstract() || UpRefs.empty()) return ty;
768
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000769 PATypeHolder Ty(ty);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000770 UR_OUT("Type '" << Ty->getDescription() <<
Chris Lattner5084d032001-11-02 07:46:26 +0000771 "' newly formed. Resolving upreferences.\n" <<
772 UpRefs.size() << " upreferences active!\n");
Chris Lattner026a8ce2004-02-09 18:53:54 +0000773
774 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
775 // to zero), we resolve them all together before we resolve them to Ty. At
776 // the end of the loop, if there is anything to resolve to Ty, it will be in
777 // this variable.
778 OpaqueType *TypeToResolve = 0;
779
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000780 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000781 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
782 << UpRefs[i].second->getDescription() << ") = "
Reid Spencer3271ed52004-07-18 00:08:11 +0000783 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner3b073862004-02-09 03:19:29 +0000784 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
785 // Decrement level of upreference
786 unsigned Level = --UpRefs[i].NestingLevel;
787 UpRefs[i].LastContainedTy = Ty;
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000788 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Misha Brukman5a2a3822005-05-10 22:02:28 +0000789 if (Level == 0) { // Upreference should be resolved!
Chris Lattner026a8ce2004-02-09 18:53:54 +0000790 if (!TypeToResolve) {
791 TypeToResolve = UpRefs[i].UpRefTy;
792 } else {
793 UR_OUT(" * Resolving upreference for "
794 << UpRefs[i].second->getDescription() << "\n";
795 std::string OldName = UpRefs[i].UpRefTy->getDescription());
796 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
797 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
798 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
799 }
Reid Spencer3271ed52004-07-18 00:08:11 +0000800 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000801 --i; // Do not skip the next element...
Chris Lattner30c89792001-09-07 16:35:17 +0000802 }
803 }
Chris Lattner8896eda2001-07-09 19:38:36 +0000804 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000805
Chris Lattner026a8ce2004-02-09 18:53:54 +0000806 if (TypeToResolve) {
807 UR_OUT(" * Resolving upreference for "
808 << UpRefs[i].second->getDescription() << "\n";
Chris Lattner16af11d2004-02-09 21:03:38 +0000809 std::string OldName = TypeToResolve->getDescription());
Chris Lattner026a8ce2004-02-09 18:53:54 +0000810 TypeToResolve->refineAbstractTypeTo(Ty);
811 }
812
Chris Lattner8896eda2001-07-09 19:38:36 +0000813 return Ty;
814}
815
Chris Lattner30c89792001-09-07 16:35:17 +0000816
Chris Lattner6184feb2005-05-20 03:25:47 +0000817// common code from the two 'RunVMAsmParser' functions
Reid Spencer5b7e7532006-09-28 19:28:24 +0000818static Module* RunParser(Module * M) {
Chris Lattner6184feb2005-05-20 03:25:47 +0000819
820 llvmAsmlineno = 1; // Reset the current line number...
Andrew Lenharth558bc882005-06-18 18:34:52 +0000821 ObsoleteVarArgs = false;
Andrew Lenharthe78f50d2005-06-19 03:53:56 +0000822 NewVarArgs = false;
Chris Lattner6184feb2005-05-20 03:25:47 +0000823 CurModule.CurrentModule = M;
Reid Spencerf63697d2006-10-09 17:36:59 +0000824
825 // Check to make sure the parser succeeded
826 if (yyparse()) {
827 if (ParserResult)
828 delete ParserResult;
829 return 0;
830 }
831
832 // Check to make sure that parsing produced a result
Reid Spencer61c83e02006-08-18 08:43:06 +0000833 if (!ParserResult)
834 return 0;
Chris Lattner6184feb2005-05-20 03:25:47 +0000835
Reid Spencerf63697d2006-10-09 17:36:59 +0000836 // Reset ParserResult variable while saving its value for the result.
Chris Lattner6184feb2005-05-20 03:25:47 +0000837 Module *Result = ParserResult;
838 ParserResult = 0;
839
Andrew Lenharth31c98bf2005-06-20 15:41:37 +0000840 //Not all functions use vaarg, so make a second check for ObsoleteVarArgs
841 {
842 Function* F;
843 if ((F = Result->getNamedFunction("llvm.va_start"))
844 && F->getFunctionType()->getNumParams() == 0)
845 ObsoleteVarArgs = true;
846 if((F = Result->getNamedFunction("llvm.va_copy"))
847 && F->getFunctionType()->getNumParams() == 1)
848 ObsoleteVarArgs = true;
849 }
850
Reid Spencer5b7e7532006-09-28 19:28:24 +0000851 if (ObsoleteVarArgs && NewVarArgs) {
852 GenerateError(
853 "This file is corrupt: it uses both new and old style varargs");
854 return 0;
855 }
Andrew Lenharthe78f50d2005-06-19 03:53:56 +0000856
Andrew Lenharth558bc882005-06-18 18:34:52 +0000857 if(ObsoleteVarArgs) {
858 if(Function* F = Result->getNamedFunction("llvm.va_start")) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000859 if (F->arg_size() != 0) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000860 GenerateError("Obsolete va_start takes 0 argument!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000861 return 0;
862 }
Andrew Lenharth558bc882005-06-18 18:34:52 +0000863
864 //foo = va_start()
865 // ->
866 //bar = alloca typeof(foo)
867 //va_start(bar)
868 //foo = load bar
869
870 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
871 const Type* ArgTy = F->getFunctionType()->getReturnType();
872 const Type* ArgTyPtr = PointerType::get(ArgTy);
873 Function* NF = Result->getOrInsertFunction("llvm.va_start",
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000874 RetTy, ArgTyPtr, (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000875
876 while (!F->use_empty()) {
877 CallInst* CI = cast<CallInst>(F->use_back());
878 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vastart.fix.1", CI);
879 new CallInst(NF, bar, "", CI);
880 Value* foo = new LoadInst(bar, "vastart.fix.2", CI);
881 CI->replaceAllUsesWith(foo);
882 CI->getParent()->getInstList().erase(CI);
883 }
884 Result->getFunctionList().erase(F);
885 }
886
887 if(Function* F = Result->getNamedFunction("llvm.va_end")) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000888 if(F->arg_size() != 1) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000889 GenerateError("Obsolete va_end takes 1 argument!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000890 return 0;
891 }
Andrew Lenharth213e5572005-06-22 21:04:42 +0000892
Andrew Lenharth558bc882005-06-18 18:34:52 +0000893 //vaend foo
894 // ->
895 //bar = alloca 1 of typeof(foo)
896 //vaend bar
897 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
898 const Type* ArgTy = F->getFunctionType()->getParamType(0);
899 const Type* ArgTyPtr = PointerType::get(ArgTy);
900 Function* NF = Result->getOrInsertFunction("llvm.va_end",
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000901 RetTy, ArgTyPtr, (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000902
903 while (!F->use_empty()) {
904 CallInst* CI = cast<CallInst>(F->use_back());
905 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vaend.fix.1", CI);
Andrew Lenharth017fba92005-06-19 14:04:55 +0000906 new StoreInst(CI->getOperand(1), bar, CI);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000907 new CallInst(NF, bar, "", CI);
908 CI->getParent()->getInstList().erase(CI);
909 }
910 Result->getFunctionList().erase(F);
911 }
912
913 if(Function* F = Result->getNamedFunction("llvm.va_copy")) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000914 if(F->arg_size() != 1) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000915 GenerateError("Obsolete va_copy takes 1 argument!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000916 return 0;
917 }
Andrew Lenharth558bc882005-06-18 18:34:52 +0000918 //foo = vacopy(bar)
919 // ->
920 //a = alloca 1 of typeof(foo)
Andrew Lenharth213e5572005-06-22 21:04:42 +0000921 //b = alloca 1 of typeof(foo)
922 //store bar -> b
923 //vacopy(a, b)
Andrew Lenharth558bc882005-06-18 18:34:52 +0000924 //foo = load a
925
926 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
927 const Type* ArgTy = F->getFunctionType()->getReturnType();
928 const Type* ArgTyPtr = PointerType::get(ArgTy);
929 Function* NF = Result->getOrInsertFunction("llvm.va_copy",
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000930 RetTy, ArgTyPtr, ArgTyPtr,
931 (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000932
933 while (!F->use_empty()) {
934 CallInst* CI = cast<CallInst>(F->use_back());
935 AllocaInst* a = new AllocaInst(ArgTy, 0, "vacopy.fix.1", CI);
Andrew Lenharth213e5572005-06-22 21:04:42 +0000936 AllocaInst* b = new AllocaInst(ArgTy, 0, "vacopy.fix.2", CI);
937 new StoreInst(CI->getOperand(1), b, CI);
938 new CallInst(NF, a, b, "", CI);
939 Value* foo = new LoadInst(a, "vacopy.fix.3", CI);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000940 CI->replaceAllUsesWith(foo);
941 CI->getParent()->getInstList().erase(CI);
942 }
943 Result->getFunctionList().erase(F);
944 }
945 }
946
Chris Lattner6184feb2005-05-20 03:25:47 +0000947 return Result;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000948}
Chris Lattner6184feb2005-05-20 03:25:47 +0000949
Chris Lattner00950542001-06-06 20:29:01 +0000950//===----------------------------------------------------------------------===//
951// RunVMAsmParser - Define an interface to this parser
952//===----------------------------------------------------------------------===//
953//
Chris Lattner86427632004-07-14 06:39:48 +0000954Module *llvm::RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner6184feb2005-05-20 03:25:47 +0000955 set_scan_file(F);
956
Chris Lattnera2850432001-07-22 18:36:00 +0000957 CurFilename = Filename;
Chris Lattner6184feb2005-05-20 03:25:47 +0000958 return RunParser(new Module(CurFilename));
959}
Chris Lattner00950542001-06-06 20:29:01 +0000960
Chris Lattner6184feb2005-05-20 03:25:47 +0000961Module *llvm::RunVMAsmParser(const char * AsmString, Module * M) {
962 set_scan_string(AsmString);
Chris Lattner42570982003-11-26 07:24:58 +0000963
Chris Lattner6184feb2005-05-20 03:25:47 +0000964 CurFilename = "from_memory";
965 if (M == NULL) {
966 return RunParser(new Module (CurFilename));
967 } else {
968 return RunParser(M);
969 }
Chris Lattner00950542001-06-06 20:29:01 +0000970}
971
972%}
973
974%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000975 llvm::Module *ModuleVal;
976 llvm::Function *FunctionVal;
977 std::pair<llvm::PATypeHolder*, char*> *ArgVal;
978 llvm::BasicBlock *BasicBlockVal;
979 llvm::TerminatorInst *TermInstVal;
980 llvm::Instruction *InstVal;
981 llvm::Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000982
Brian Gaeked0fde302003-11-11 22:41:34 +0000983 const llvm::Type *PrimType;
984 llvm::PATypeHolder *TypeVal;
985 llvm::Value *ValueVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000986
Brian Gaeked0fde302003-11-11 22:41:34 +0000987 std::vector<std::pair<llvm::PATypeHolder*,char*> > *ArgList;
988 std::vector<llvm::Value*> *ValueList;
989 std::list<llvm::PATypeHolder> *TypeList;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000990 // Represent the RHS of PHI node
Brian Gaeked0fde302003-11-11 22:41:34 +0000991 std::list<std::pair<llvm::Value*,
Misha Brukman5a2a3822005-05-10 22:02:28 +0000992 llvm::BasicBlock*> > *PHIList;
Brian Gaeked0fde302003-11-11 22:41:34 +0000993 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
994 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000995
Brian Gaeked0fde302003-11-11 22:41:34 +0000996 llvm::GlobalValue::LinkageTypes Linkage;
Chris Lattner30c89792001-09-07 16:35:17 +0000997 int64_t SInt64Val;
998 uint64_t UInt64Val;
999 int SIntVal;
1000 unsigned UIntVal;
1001 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +00001002 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +00001003
Chris Lattner30c89792001-09-07 16:35:17 +00001004 char *StrVal; // This memory is strdup'd!
Brian Gaeked0fde302003-11-11 22:41:34 +00001005 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +00001006
Brian Gaeked0fde302003-11-11 22:41:34 +00001007 llvm::Instruction::BinaryOps BinaryOpVal;
1008 llvm::Instruction::TermOps TermOpVal;
1009 llvm::Instruction::MemoryOps MemOpVal;
1010 llvm::Instruction::OtherOps OtherOpVal;
1011 llvm::Module::Endianness Endianness;
Chris Lattner00950542001-06-06 20:29:01 +00001012}
1013
Chris Lattner79df7c02002-03-26 18:01:55 +00001014%type <ModuleVal> Module FunctionList
1015%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +00001016%type <BasicBlockVal> BasicBlock InstructionList
1017%type <TermInstVal> BBTerminatorInst
1018%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001019%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +00001020%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +00001021%type <ArgList> ArgList ArgListH
1022%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +00001023%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001024%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +00001025%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +00001026%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +00001027%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +00001028%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner15c9c032003-09-08 18:20:29 +00001029%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattnerddb6db42005-05-06 05:51:46 +00001030%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
Chris Lattneraa2c8532006-01-25 22:26:43 +00001031%type <BoolVal> OptSideEffect // 'sideeffect' or not.
Chris Lattner4ad02e72003-04-16 20:28:45 +00001032%type <Linkage> OptLinkage
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001033%type <Endianness> BigOrLittle
Chris Lattner00950542001-06-06 20:29:01 +00001034
Chris Lattner2079fde2001-10-13 06:41:08 +00001035// ValueRef - Unresolved reference to a definition or BB
1036%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001037%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +00001038// Tokens and types for handling constant integer values
1039//
1040// ESINT64VAL - A negative number within long long range
1041%token <SInt64Val> ESINT64VAL
1042
1043// EUINT64VAL - A positive number within uns. long long range
1044%token <UInt64Val> EUINT64VAL
1045%type <SInt64Val> EINT64VAL
1046
1047%token <SIntVal> SINTVAL // Signed 32 bit ints...
1048%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
1049%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001050%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +00001051
1052// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +00001053%type <TypeVal> Types TypesV UpRTypes UpRTypesV
1054%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +00001055%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
1056%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +00001057
Chris Lattner6c23f572003-08-22 05:42:10 +00001058%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
1059%type <StrVal> Name OptName OptAssign
Chris Lattner87ac9722005-11-06 06:46:28 +00001060%type <UIntVal> OptAlign OptCAlign
Chris Lattnerb2b96672005-11-12 18:21:21 +00001061%type <StrVal> OptSection SectionString
Chris Lattner00950542001-06-06 20:29:01 +00001062
Chris Lattner91ef4602004-03-31 03:49:47 +00001063%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Chris Lattner164c3782005-11-12 00:11:10 +00001064%token DECLARE GLOBAL CONSTANT SECTION VOLATILE
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001065%token TO DOTDOTDOT NULL_TOK UNDEF CONST INTERNAL LINKONCE WEAK APPENDING
1066%token DLLIMPORT DLLEXPORT EXTERN_WEAK
Nate Begeman14b05292005-11-05 09:21:28 +00001067%token OPAQUE NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG ALIGN
Chris Lattneraa2c8532006-01-25 22:26:43 +00001068%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
Chris Lattner515906d2006-05-19 21:28:34 +00001069%token CC_TOK CCC_TOK CSRETCC_TOK FASTCC_TOK COLDCC_TOK
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001070%token X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
Chris Lattner10b27112006-10-22 06:07:41 +00001071%token DATALAYOUT
Chris Lattnera8e8f162005-05-06 20:27:19 +00001072%type <UIntVal> OptCallingConv
Chris Lattner00950542001-06-06 20:29:01 +00001073
Misha Brukman5a2a3822005-05-10 22:02:28 +00001074// Basic Block Terminating Operators
Chris Lattner16710e92004-10-16 18:17:13 +00001075%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
Chris Lattner00950542001-06-06 20:29:01 +00001076
Misha Brukman5a2a3822005-05-10 22:02:28 +00001077// Binary Operators
Chris Lattner4a6482b2002-09-10 19:57:26 +00001078%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattner42c9e772001-10-20 09:32:59 +00001079%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +00001080%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +00001081
1082// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001083%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +00001084
Chris Lattner027dcc52001-07-08 21:10:27 +00001085// Other Operators
1086%type <OtherOpVal> ShiftOps
Robert Bocchino2def1b32006-01-17 20:06:25 +00001087%token <OtherOpVal> PHI_TOK CAST SELECT SHL SHR VAARG
Chris Lattner4c949082006-04-08 01:18:35 +00001088%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Andrew Lenharth558bc882005-06-18 18:34:52 +00001089%token VAARG_old VANEXT_old //OBSOLETE
Chris Lattnerddb6db42005-05-06 05:51:46 +00001090
Chris Lattner027dcc52001-07-08 21:10:27 +00001091
Chris Lattner00950542001-06-06 20:29:01 +00001092%start Module
1093%%
1094
1095// Handle constant integer size restriction and conversion...
1096//
Chris Lattner51727be2002-06-04 21:58:56 +00001097INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +00001098INTVAL : UINTVAL {
1099 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
Reid Spencer61c83e02006-08-18 08:43:06 +00001100 GEN_ERROR("Value too large for type!");
Chris Lattner00950542001-06-06 20:29:01 +00001101 $$ = (int32_t)$1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001102 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001103};
Chris Lattner00950542001-06-06 20:29:01 +00001104
1105
Chris Lattner51727be2002-06-04 21:58:56 +00001106EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +00001107EINT64VAL : EUINT64VAL {
1108 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
Reid Spencer61c83e02006-08-18 08:43:06 +00001109 GEN_ERROR("Value too large for type!");
Chris Lattner00950542001-06-06 20:29:01 +00001110 $$ = (int64_t)$1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001111 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001112};
Chris Lattner00950542001-06-06 20:29:01 +00001113
Misha Brukman5a2a3822005-05-10 22:02:28 +00001114// Operations that are notably excluded from this list include:
Chris Lattner00950542001-06-06 20:29:01 +00001115// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1116//
Chris Lattner4a6482b2002-09-10 19:57:26 +00001117ArithmeticOps: ADD | SUB | MUL | DIV | REM;
1118LogicalOps : AND | OR | XOR;
1119SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
Chris Lattner4a6482b2002-09-10 19:57:26 +00001120
Chris Lattner51727be2002-06-04 21:58:56 +00001121ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +00001122
Chris Lattnere98dda62001-07-14 06:10:16 +00001123// These are some types that allow classification if we only want a particular
1124// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +00001125SIntType : LONG | INT | SHORT | SBYTE;
1126UIntType : ULONG | UINT | USHORT | UBYTE;
1127IntType : SIntType | UIntType;
1128FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +00001129
Chris Lattnere98dda62001-07-14 06:10:16 +00001130// OptAssign - Value producing statements have an optional assignment component
Chris Lattner6c23f572003-08-22 05:42:10 +00001131OptAssign : Name '=' {
Chris Lattner00950542001-06-06 20:29:01 +00001132 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001133 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001134 }
Misha Brukman5a2a3822005-05-10 22:02:28 +00001135 | /*empty*/ {
1136 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00001137 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001138 };
Chris Lattner00950542001-06-06 20:29:01 +00001139
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001140OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
1141 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
1142 WEAK { $$ = GlobalValue::WeakLinkage; } |
1143 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
1144 DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; } |
1145 DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; } |
1146 EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; } |
1147 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattner30c89792001-09-07 16:35:17 +00001148
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001149OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
1150 CCC_TOK { $$ = CallingConv::C; } |
1151 CSRETCC_TOK { $$ = CallingConv::CSRet; } |
1152 FASTCC_TOK { $$ = CallingConv::Fast; } |
1153 COLDCC_TOK { $$ = CallingConv::Cold; } |
1154 X86_STDCALLCC_TOK { $$ = CallingConv::X86_StdCall; } |
1155 X86_FASTCALLCC_TOK { $$ = CallingConv::X86_FastCall; } |
1156 CC_TOK EUINT64VAL {
Chris Lattnera8e8f162005-05-06 20:27:19 +00001157 if ((unsigned)$2 != $2)
Reid Spencer61c83e02006-08-18 08:43:06 +00001158 GEN_ERROR("Calling conv too large!");
Chris Lattnera8e8f162005-05-06 20:27:19 +00001159 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001160 CHECK_FOR_ERROR
Chris Lattnera8e8f162005-05-06 20:27:19 +00001161 };
1162
Chris Lattner66db8e42005-11-06 06:34:12 +00001163// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1164// a comma before it.
Chris Lattner87ac9722005-11-06 06:46:28 +00001165OptAlign : /*empty*/ { $$ = 0; } |
Chris Lattnerb2b96672005-11-12 18:21:21 +00001166 ALIGN EUINT64VAL {
1167 $$ = $2;
1168 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencer61c83e02006-08-18 08:43:06 +00001169 GEN_ERROR("Alignment must be a power of two!");
1170 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001171};
Chris Lattner66db8e42005-11-06 06:34:12 +00001172OptCAlign : /*empty*/ { $$ = 0; } |
Chris Lattnerb2b96672005-11-12 18:21:21 +00001173 ',' ALIGN EUINT64VAL {
1174 $$ = $3;
1175 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencer61c83e02006-08-18 08:43:06 +00001176 GEN_ERROR("Alignment must be a power of two!");
1177 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001178};
1179
Chris Lattner66db8e42005-11-06 06:34:12 +00001180
Chris Lattner164c3782005-11-12 00:11:10 +00001181SectionString : SECTION STRINGCONSTANT {
1182 for (unsigned i = 0, e = strlen($2); i != e; ++i)
1183 if ($2[i] == '"' || $2[i] == '\\')
Reid Spencer61c83e02006-08-18 08:43:06 +00001184 GEN_ERROR("Invalid character in section name!");
Chris Lattner164c3782005-11-12 00:11:10 +00001185 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001186 CHECK_FOR_ERROR
Chris Lattner164c3782005-11-12 00:11:10 +00001187};
1188
1189OptSection : /*empty*/ { $$ = 0; } |
1190 SectionString { $$ = $1; };
Chris Lattner164c3782005-11-12 00:11:10 +00001191
Chris Lattnerb2b96672005-11-12 18:21:21 +00001192// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1193// is set to be the global we are processing.
1194//
1195GlobalVarAttributes : /* empty */ {} |
1196 ',' GlobalVarAttribute GlobalVarAttributes {};
1197GlobalVarAttribute : SectionString {
1198 CurGV->setSection($1);
1199 free($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001200 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001201 }
1202 | ALIGN EUINT64VAL {
1203 if ($2 != 0 && !isPowerOf2_32($2))
Reid Spencer61c83e02006-08-18 08:43:06 +00001204 GEN_ERROR("Alignment must be a power of two!");
Chris Lattnerb2b96672005-11-12 18:21:21 +00001205 CurGV->setAlignment($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001206 CHECK_FOR_ERROR
Chris Lattnerb2b96672005-11-12 18:21:21 +00001207 };
Chris Lattner164c3782005-11-12 00:11:10 +00001208
Chris Lattner30c89792001-09-07 16:35:17 +00001209//===----------------------------------------------------------------------===//
1210// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +00001211// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +00001212// access to it, a user must explicitly use TypesV.
1213//
1214
1215// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +00001216TypesV : Types | VOID { $$ = new PATypeHolder($1); };
1217UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +00001218
1219Types : UpRTypes {
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001220 if (!UpRefs.empty())
Reid Spencer61c83e02006-08-18 08:43:06 +00001221 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001222 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001223 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001224 };
Chris Lattner30c89792001-09-07 16:35:17 +00001225
1226
1227// Derived types are added later...
1228//
Chris Lattner51727be2002-06-04 21:58:56 +00001229PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
1230PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001231UpRTypes : OPAQUE {
1232 $$ = new PATypeHolder(OpaqueType::get());
Reid Spencer61c83e02006-08-18 08:43:06 +00001233 CHECK_FOR_ERROR
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001234 }
1235 | PrimType {
1236 $$ = new PATypeHolder($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001237 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001238 };
Chris Lattnerd78700d2002-08-16 21:14:40 +00001239UpRTypes : SymbolicValueRef { // Named types are also simple types...
Reid Spencer5b7e7532006-09-28 19:28:24 +00001240 const Type* tmp = getTypeVal($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001241 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00001242 $$ = new PATypeHolder(tmp);
Chris Lattner51727be2002-06-04 21:58:56 +00001243};
Chris Lattner30c89792001-09-07 16:35:17 +00001244
Chris Lattner30c89792001-09-07 16:35:17 +00001245// Include derived types in the Types production.
1246//
1247UpRTypes : '\\' EUINT64VAL { // Type UpReference
Reid Spencer61c83e02006-08-18 08:43:06 +00001248 if ($2 > (uint64_t)~0U) GEN_ERROR("Value out of range!");
Chris Lattner30c89792001-09-07 16:35:17 +00001249 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3b073862004-02-09 03:19:29 +00001250 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001251 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +00001252 UR_OUT("New Upreference!\n");
Reid Spencer61c83e02006-08-18 08:43:06 +00001253 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001254 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001255 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner9232b992003-04-22 18:42:41 +00001256 std::vector<const Type*> Params;
Chris Lattnera08976b2005-02-22 23:13:58 +00001257 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1258 E = $3->end(); I != E; ++I)
1259 Params.push_back(*I);
Chris Lattner2079fde2001-10-13 06:41:08 +00001260 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1261 if (isVarArg) Params.pop_back();
1262
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001263 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +00001264 delete $3; // Delete the argument list
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001265 delete $1; // Delete the return type handle
Reid Spencer61c83e02006-08-18 08:43:06 +00001266 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001267 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001268 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001269 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001270 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00001271 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001272 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001273 | '<' EUINT64VAL 'x' UpRTypes '>' { // Packed array type?
1274 const llvm::Type* ElemTy = $4->get();
Chris Lattner9547d7f2005-11-10 01:42:43 +00001275 if ((unsigned)$2 != $2)
Reid Spencer61c83e02006-08-18 08:43:06 +00001276 GEN_ERROR("Unsigned result not equal to signed result");
Chris Lattner9547d7f2005-11-10 01:42:43 +00001277 if (!ElemTy->isPrimitiveType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001278 GEN_ERROR("Elemental type of a PackedType must be primitive");
Chris Lattner9547d7f2005-11-10 01:42:43 +00001279 if (!isPowerOf2_32($2))
Reid Spencer61c83e02006-08-18 08:43:06 +00001280 GEN_ERROR("Vector length should be a power of 2!");
Brian Gaeke715c90b2004-08-20 06:00:58 +00001281 $$ = new PATypeHolder(HandleUpRefs(PackedType::get(*$4, (unsigned)$2)));
1282 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00001283 CHECK_FOR_ERROR
Brian Gaeke715c90b2004-08-20 06:00:58 +00001284 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001285 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001286 std::vector<const Type*> Elements;
Chris Lattnera08976b2005-02-22 23:13:58 +00001287 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
1288 E = $2->end(); I != E; ++I)
1289 Elements.push_back(*I);
Misha Brukman5a2a3822005-05-10 22:02:28 +00001290
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001291 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001292 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001293 CHECK_FOR_ERROR
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001294 }
1295 | '{' '}' { // Empty structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001296 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Reid Spencer61c83e02006-08-18 08:43:06 +00001297 CHECK_FOR_ERROR
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001298 }
1299 | UpRTypes '*' { // Pointer type?
Chris Lattner9e76ce92006-10-15 23:26:46 +00001300 if (*$1 == Type::LabelTy)
1301 GEN_ERROR("Cannot form a pointer to a basic block");
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001302 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001303 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001304 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001305 };
Chris Lattner30c89792001-09-07 16:35:17 +00001306
Chris Lattner7e708292002-06-25 16:13:24 +00001307// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +00001308// declaration type lists
1309//
1310TypeListI : UpRTypes {
Chris Lattner9232b992003-04-22 18:42:41 +00001311 $$ = new std::list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +00001312 $$->push_back(*$1); delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001313 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001314 }
1315 | TypeListI ',' UpRTypes {
1316 ($$=$1)->push_back(*$3); delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001317 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001318 };
Chris Lattner30c89792001-09-07 16:35:17 +00001319
Chris Lattner7e708292002-06-25 16:13:24 +00001320// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +00001321ArgTypeListI : TypeListI
1322 | TypeListI ',' DOTDOTDOT {
1323 ($$=$1)->push_back(Type::VoidTy);
Reid Spencer61c83e02006-08-18 08:43:06 +00001324 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001325 }
1326 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001327 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
Reid Spencer61c83e02006-08-18 08:43:06 +00001328 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001329 }
1330 | /*empty*/ {
Chris Lattner9232b992003-04-22 18:42:41 +00001331 $$ = new std::list<PATypeHolder>();
Reid Spencer61c83e02006-08-18 08:43:06 +00001332 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001333 };
Chris Lattner30c89792001-09-07 16:35:17 +00001334
Chris Lattnere98dda62001-07-14 06:10:16 +00001335// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +00001336// production is used ONLY to represent constants that show up AFTER a 'const',
1337// 'constant' or 'global' token at global scope. Constants that can be inlined
1338// into other expressions (such as integers and constexprs) are handled by the
1339// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +00001340//
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001341ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Chris Lattner949a3622003-07-23 15:30:06 +00001342 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001343 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001344 GEN_ERROR("Cannot make array constant with type: '" +
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001345 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001346 const Type *ETy = ATy->getElementType();
1347 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +00001348
Chris Lattner30c89792001-09-07 16:35:17 +00001349 // Verify that we have the correct size...
1350 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001351 GEN_ERROR("Type mismatch: constant sized array initialized with " +
Reid Spencer3271ed52004-07-18 00:08:11 +00001352 utostr($3->size()) + " arguments, but has size of " +
1353 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +00001354
Chris Lattner30c89792001-09-07 16:35:17 +00001355 // Verify all elements are correct type!
1356 for (unsigned i = 0; i < $3->size(); i++) {
1357 if (ETy != (*$3)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001358 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Reid Spencer3271ed52004-07-18 00:08:11 +00001359 ETy->getDescription() +"' as required!\nIt is of type '"+
1360 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +00001361 }
1362
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001363 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001364 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001365 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001366 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001367 | Types '[' ']' {
Chris Lattner949a3622003-07-23 15:30:06 +00001368 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001369 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001370 GEN_ERROR("Cannot make array constant with type: '" +
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001371 (*$1)->getDescription() + "'!");
1372
1373 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +00001374 if (NumElements != -1 && NumElements != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001375 GEN_ERROR("Type mismatch: constant sized array initialized with 0"
Reid Spencer3271ed52004-07-18 00:08:11 +00001376 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattner9232b992003-04-22 18:42:41 +00001377 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +00001378 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001379 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001380 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001381 | Types 'c' STRINGCONSTANT {
Chris Lattner949a3622003-07-23 15:30:06 +00001382 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001383 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001384 GEN_ERROR("Cannot make array constant with type: '" +
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001385 (*$1)->getDescription() + "'!");
1386
Chris Lattner30c89792001-09-07 16:35:17 +00001387 int NumElements = ATy->getNumElements();
1388 const Type *ETy = ATy->getElementType();
1389 char *EndStr = UnEscapeLexed($3, true);
1390 if (NumElements != -1 && NumElements != (EndStr-$3))
Reid Spencer61c83e02006-08-18 08:43:06 +00001391 GEN_ERROR("Can't build string constant of size " +
Reid Spencer3271ed52004-07-18 00:08:11 +00001392 itostr((int)(EndStr-$3)) +
1393 " when array has size " + itostr(NumElements) + "!");
Chris Lattner9232b992003-04-22 18:42:41 +00001394 std::vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +00001395 if (ETy == Type::SByteTy) {
Chris Lattneree454772006-01-23 23:05:15 +00001396 for (signed char *C = (signed char *)$3; C != (signed char *)EndStr; ++C)
Reid Spencerb83eb642006-10-20 07:07:24 +00001397 Vals.push_back(ConstantInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +00001398 } else if (ETy == Type::UByteTy) {
Chris Lattneree454772006-01-23 23:05:15 +00001399 for (unsigned char *C = (unsigned char *)$3;
1400 C != (unsigned char*)EndStr; ++C)
Reid Spencerb83eb642006-10-20 07:07:24 +00001401 Vals.push_back(ConstantInt::get(ETy, *C));
Chris Lattner93750fa2001-07-28 17:48:55 +00001402 } else {
Chris Lattner30c89792001-09-07 16:35:17 +00001403 free($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00001404 GEN_ERROR("Cannot build string arrays of non byte sized elements!");
Chris Lattner93750fa2001-07-28 17:48:55 +00001405 }
Chris Lattner30c89792001-09-07 16:35:17 +00001406 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001407 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +00001408 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001409 CHECK_FOR_ERROR
Chris Lattner93750fa2001-07-28 17:48:55 +00001410 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001411 | Types '<' ConstVector '>' { // Nonempty unsized arr
1412 const PackedType *PTy = dyn_cast<PackedType>($1->get());
1413 if (PTy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001414 GEN_ERROR("Cannot make packed constant with type: '" +
Brian Gaeke715c90b2004-08-20 06:00:58 +00001415 (*$1)->getDescription() + "'!");
1416 const Type *ETy = PTy->getElementType();
1417 int NumElements = PTy->getNumElements();
1418
1419 // Verify that we have the correct size...
1420 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001421 GEN_ERROR("Type mismatch: constant sized packed initialized with " +
Brian Gaeke715c90b2004-08-20 06:00:58 +00001422 utostr($3->size()) + " arguments, but has size of " +
1423 itostr(NumElements) + "!");
1424
1425 // Verify all elements are correct type!
1426 for (unsigned i = 0; i < $3->size(); i++) {
1427 if (ETy != (*$3)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001428 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Brian Gaeke715c90b2004-08-20 06:00:58 +00001429 ETy->getDescription() +"' as required!\nIt is of type '"+
1430 (*$3)[i]->getType()->getDescription() + "'.");
1431 }
1432
1433 $$ = ConstantPacked::get(PTy, *$3);
1434 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001435 CHECK_FOR_ERROR
Brian Gaeke715c90b2004-08-20 06:00:58 +00001436 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001437 | Types '{' ConstVector '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001438 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001439 if (STy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001440 GEN_ERROR("Cannot make struct constant with type: '" +
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001441 (*$1)->getDescription() + "'!");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001442
Chris Lattnerc6212f12003-05-21 16:06:56 +00001443 if ($3->size() != STy->getNumContainedTypes())
Reid Spencer61c83e02006-08-18 08:43:06 +00001444 GEN_ERROR("Illegal number of initializers for structure type!");
Chris Lattnerc6212f12003-05-21 16:06:56 +00001445
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001446 // Check to ensure that constants are compatible with the type initializer!
1447 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Chris Lattnerd21cd802004-02-09 04:37:31 +00001448 if ((*$3)[i]->getType() != STy->getElementType(i))
Reid Spencer61c83e02006-08-18 08:43:06 +00001449 GEN_ERROR("Expected type '" +
Chris Lattnerd21cd802004-02-09 04:37:31 +00001450 STy->getElementType(i)->getDescription() +
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001451 "' for element #" + utostr(i) +
1452 " of structure initializer!");
1453
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001454 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001455 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001456 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001457 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001458 | Types '{' '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001459 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001460 if (STy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001461 GEN_ERROR("Cannot make struct constant with type: '" +
Chris Lattnerc6212f12003-05-21 16:06:56 +00001462 (*$1)->getDescription() + "'!");
1463
1464 if (STy->getNumContainedTypes() != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001465 GEN_ERROR("Illegal number of initializers for structure type!");
Chris Lattnerc6212f12003-05-21 16:06:56 +00001466
1467 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1468 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001469 CHECK_FOR_ERROR
Chris Lattnerc6212f12003-05-21 16:06:56 +00001470 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001471 | Types NULL_TOK {
Chris Lattner949a3622003-07-23 15:30:06 +00001472 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001473 if (PTy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001474 GEN_ERROR("Cannot make null pointer constant with type: '" +
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001475 (*$1)->getDescription() + "'!");
1476
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001477 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001478 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001479 CHECK_FOR_ERROR
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001480 }
Chris Lattner16710e92004-10-16 18:17:13 +00001481 | Types UNDEF {
1482 $$ = UndefValue::get($1->get());
1483 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001484 CHECK_FOR_ERROR
Chris Lattner16710e92004-10-16 18:17:13 +00001485 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001486 | Types SymbolicValueRef {
Chris Lattner949a3622003-07-23 15:30:06 +00001487 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001488 if (Ty == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001489 GEN_ERROR("Global const reference must be a pointer type!");
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001490
Chris Lattner3101c252002-08-15 17:58:33 +00001491 // ConstExprs can exist in the body of a function, thus creating
Reid Spencer3271ed52004-07-18 00:08:11 +00001492 // GlobalValues whenever they refer to a variable. Because we are in
Chris Lattner3101c252002-08-15 17:58:33 +00001493 // the context of a function, getValNonImprovising will search the functions
1494 // symbol table instead of the module symbol table for the global symbol,
1495 // which throws things all off. To get around this, we just tell
1496 // getValNonImprovising that we are at global scope here.
1497 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001498 Function *SavedCurFn = CurFun.CurrentFunction;
1499 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001500
Chris Lattner2079fde2001-10-13 06:41:08 +00001501 Value *V = getValNonImprovising(Ty, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00001502 CHECK_FOR_ERROR
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001503
Chris Lattner394f2eb2003-10-16 20:12:13 +00001504 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001505
Chris Lattner2079fde2001-10-13 06:41:08 +00001506 // If this is an initializer for a constant pointer, which is referencing a
1507 // (currently) undefined variable, create a stub now that shall be replaced
1508 // in the future with the right type of variable.
1509 //
1510 if (V == 0) {
1511 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1512 const PointerType *PT = cast<PointerType>(Ty);
1513
1514 // First check to see if the forward references value is already created!
1515 PerModuleInfo::GlobalRefsType::iterator I =
Reid Spencer3271ed52004-07-18 00:08:11 +00001516 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001517
1518 if (I != CurModule.GlobalRefs.end()) {
Reid Spencer3271ed52004-07-18 00:08:11 +00001519 V = I->second; // Placeholder already exists, use it...
Chris Lattnera989b232003-12-23 20:05:15 +00001520 $2.destroy();
Chris Lattner2079fde2001-10-13 06:41:08 +00001521 } else {
Chris Lattner8a327842004-07-14 21:44:00 +00001522 std::string Name;
Chris Lattnera09000d2004-07-14 23:03:46 +00001523 if ($2.Type == ValID::NameVal) Name = $2.Name;
Chris Lattner8a327842004-07-14 21:44:00 +00001524
Reid Spencer3271ed52004-07-18 00:08:11 +00001525 // Create the forward referenced global.
Chris Lattnera09000d2004-07-14 23:03:46 +00001526 GlobalValue *GV;
1527 if (const FunctionType *FTy =
1528 dyn_cast<FunctionType>(PT->getElementType())) {
1529 GV = new Function(FTy, GlobalValue::ExternalLinkage, Name,
1530 CurModule.CurrentModule);
1531 } else {
Reid Spencer3271ed52004-07-18 00:08:11 +00001532 GV = new GlobalVariable(PT->getElementType(), false,
Chris Lattnera09000d2004-07-14 23:03:46 +00001533 GlobalValue::ExternalLinkage, 0,
1534 Name, CurModule.CurrentModule);
1535 }
Chris Lattner8a327842004-07-14 21:44:00 +00001536
Reid Spencer3271ed52004-07-18 00:08:11 +00001537 // Keep track of the fact that we have a forward ref to recycle it
1538 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1539 V = GV;
Chris Lattner2079fde2001-10-13 06:41:08 +00001540 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001541 }
1542
Reid Spencer3271ed52004-07-18 00:08:11 +00001543 $$ = cast<GlobalValue>(V);
Chris Lattner2079fde2001-10-13 06:41:08 +00001544 delete $1; // Free the type handle
Reid Spencer61c83e02006-08-18 08:43:06 +00001545 CHECK_FOR_ERROR
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001546 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001547 | Types ConstExpr {
1548 if ($1->get() != $2->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001549 GEN_ERROR("Mismatched types for constant expression!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001550 $$ = $2;
1551 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001552 CHECK_FOR_ERROR
Chris Lattnerf4600482003-06-28 20:01:34 +00001553 }
1554 | Types ZEROINITIALIZER {
Chris Lattner78915932004-11-28 16:45:45 +00001555 const Type *Ty = $1->get();
1556 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
Reid Spencer61c83e02006-08-18 08:43:06 +00001557 GEN_ERROR("Cannot create a null initialized value of this type!");
Chris Lattner78915932004-11-28 16:45:45 +00001558 $$ = Constant::getNullValue(Ty);
Chris Lattnerf4600482003-06-28 20:01:34 +00001559 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001560 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001561 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001562
Chris Lattnere43f40b2002-10-09 00:25:32 +00001563ConstVal : SIntType EINT64VAL { // integral constants
Reid Spencerb83eb642006-10-20 07:07:24 +00001564 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencer61c83e02006-08-18 08:43:06 +00001565 GEN_ERROR("Constant value doesn't fit in type!");
Reid Spencerb83eb642006-10-20 07:07:24 +00001566 $$ = ConstantInt::get($1, $2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001567 CHECK_FOR_ERROR
Chris Lattnere43f40b2002-10-09 00:25:32 +00001568 }
1569 | UIntType EUINT64VAL { // integral constants
Reid Spencerb83eb642006-10-20 07:07:24 +00001570 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencer61c83e02006-08-18 08:43:06 +00001571 GEN_ERROR("Constant value doesn't fit in type!");
Reid Spencerb83eb642006-10-20 07:07:24 +00001572 $$ = ConstantInt::get($1, $2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001573 CHECK_FOR_ERROR
Chris Lattnere43f40b2002-10-09 00:25:32 +00001574 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001575 | BOOL TRUETOK { // Boolean constants
Chris Lattner47811b72006-09-28 23:35:22 +00001576 $$ = ConstantBool::getTrue();
Reid Spencer61c83e02006-08-18 08:43:06 +00001577 CHECK_FOR_ERROR
Chris Lattnerd05e3592002-08-15 18:17:28 +00001578 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001579 | BOOL FALSETOK { // Boolean constants
Chris Lattner47811b72006-09-28 23:35:22 +00001580 $$ = ConstantBool::getFalse();
Reid Spencer61c83e02006-08-18 08:43:06 +00001581 CHECK_FOR_ERROR
Chris Lattnerd05e3592002-08-15 18:17:28 +00001582 }
1583 | FPType FPVAL { // Float & Double constants
Reid Spencer9f9b3ac2004-12-06 22:18:25 +00001584 if (!ConstantFP::isValueValidForType($1, $2))
Reid Spencer61c83e02006-08-18 08:43:06 +00001585 GEN_ERROR("Floating point constant invalid for type!!");
Chris Lattnerd05e3592002-08-15 18:17:28 +00001586 $$ = ConstantFP::get($1, $2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001587 CHECK_FOR_ERROR
Chris Lattnerd05e3592002-08-15 18:17:28 +00001588 };
1589
Chris Lattner00950542001-06-06 20:29:01 +00001590
Chris Lattnerd78700d2002-08-16 21:14:40 +00001591ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattnerae711a82003-11-21 20:27:35 +00001592 if (!$3->getType()->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001593 GEN_ERROR("cast constant expression from a non-primitive type: '" +
Chris Lattnerae711a82003-11-21 20:27:35 +00001594 $3->getType()->getDescription() + "'!");
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001595 if (!$5->get()->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001596 GEN_ERROR("cast constant expression to a non-primitive type: '" +
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001597 $5->get()->getDescription() + "'!");
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001598 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001599 delete $5;
Reid Spencer61c83e02006-08-18 08:43:06 +00001600 CHECK_FOR_ERROR
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001601 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001602 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1603 if (!isa<PointerType>($3->getType()))
Reid Spencer61c83e02006-08-18 08:43:06 +00001604 GEN_ERROR("GetElementPtr requires a pointer operand!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001605
Chris Lattner830b6f92004-04-05 01:30:04 +00001606 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
1607 // indices to uint struct indices for compatibility.
1608 generic_gep_type_iterator<std::vector<Value*>::iterator>
1609 GTI = gep_type_begin($3->getType(), $4->begin(), $4->end()),
1610 GTE = gep_type_end($3->getType(), $4->begin(), $4->end());
1611 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
1612 if (isa<StructType>(*GTI)) // Only change struct indices
Reid Spencerb83eb642006-10-20 07:07:24 +00001613 if (ConstantInt *CUI = dyn_cast<ConstantInt>((*$4)[i]))
Chris Lattner830b6f92004-04-05 01:30:04 +00001614 if (CUI->getType() == Type::UByteTy)
1615 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
1616
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001617 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001618 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001619 if (!IdxTy)
Reid Spencer61c83e02006-08-18 08:43:06 +00001620 GEN_ERROR("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001621
Chris Lattner9232b992003-04-22 18:42:41 +00001622 std::vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001623 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1624 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001625 IdxVec.push_back(C);
1626 else
Reid Spencer61c83e02006-08-18 08:43:06 +00001627 GEN_ERROR("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001628
Chris Lattnerd78700d2002-08-16 21:14:40 +00001629 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001630
Chris Lattnerd78700d2002-08-16 21:14:40 +00001631 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Reid Spencer61c83e02006-08-18 08:43:06 +00001632 CHECK_FOR_ERROR
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001633 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001634 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1635 if ($3->getType() != Type::BoolTy)
Reid Spencer61c83e02006-08-18 08:43:06 +00001636 GEN_ERROR("Select condition must be of boolean type!");
Chris Lattner76f0ff32004-03-12 05:51:36 +00001637 if ($5->getType() != $7->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001638 GEN_ERROR("Select operand types must match!");
Chris Lattner76f0ff32004-03-12 05:51:36 +00001639 $$ = ConstantExpr::getSelect($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001640 CHECK_FOR_ERROR
Chris Lattner76f0ff32004-03-12 05:51:36 +00001641 }
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001642 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001643 if ($3->getType() != $5->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001644 GEN_ERROR("Binary operator types must match!");
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001645 // HACK: llvm 1.3 and earlier used to emit invalid pointer constant exprs.
1646 // To retain backward compatibility with these early compilers, we emit a
1647 // cast to the appropriate integer type automatically if we are in the
1648 // broken case. See PR424 for more information.
1649 if (!isa<PointerType>($3->getType())) {
1650 $$ = ConstantExpr::get($1, $3, $5);
1651 } else {
Chris Lattner42db1a02004-08-20 18:07:39 +00001652 const Type *IntPtrTy = 0;
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001653 switch (CurModule.CurrentModule->getPointerSize()) {
1654 case Module::Pointer32: IntPtrTy = Type::IntTy; break;
1655 case Module::Pointer64: IntPtrTy = Type::LongTy; break;
Reid Spencer61c83e02006-08-18 08:43:06 +00001656 default: GEN_ERROR("invalid pointer binary constant expr!");
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001657 }
1658 $$ = ConstantExpr::get($1, ConstantExpr::getCast($3, IntPtrTy),
1659 ConstantExpr::getCast($5, IntPtrTy));
1660 $$ = ConstantExpr::getCast($$, $3->getType());
1661 }
Reid Spencer61c83e02006-08-18 08:43:06 +00001662 CHECK_FOR_ERROR
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001663 }
1664 | LogicalOps '(' ConstVal ',' ConstVal ')' {
1665 if ($3->getType() != $5->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001666 GEN_ERROR("Logical operator types must match!");
Chris Lattner0a017832005-12-21 18:31:29 +00001667 if (!$3->getType()->isIntegral()) {
1668 if (!isa<PackedType>($3->getType()) ||
1669 !cast<PackedType>($3->getType())->getElementType()->isIntegral())
Reid Spencer61c83e02006-08-18 08:43:06 +00001670 GEN_ERROR("Logical operator requires integral operands!");
Chris Lattner0a017832005-12-21 18:31:29 +00001671 }
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001672 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001673 CHECK_FOR_ERROR
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001674 }
1675 | SetCondOps '(' ConstVal ',' ConstVal ')' {
1676 if ($3->getType() != $5->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001677 GEN_ERROR("setcc operand types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001678 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001679 CHECK_FOR_ERROR
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001680 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001681 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001682 if ($5->getType() != Type::UByteTy)
Reid Spencer61c83e02006-08-18 08:43:06 +00001683 GEN_ERROR("Shift count for shift constant must be unsigned byte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001684 if (!$3->getType()->isInteger())
Reid Spencer61c83e02006-08-18 08:43:06 +00001685 GEN_ERROR("Shift constant expression requires integer operand!");
Chris Lattnerb16689b2004-01-12 19:08:43 +00001686 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001687 CHECK_FOR_ERROR
Robert Bocchino9c62b562006-01-10 19:04:32 +00001688 }
1689 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Chris Lattner2d7349a2006-04-08 04:08:32 +00001690 if (!ExtractElementInst::isValidOperands($3, $5))
Reid Spencer61c83e02006-08-18 08:43:06 +00001691 GEN_ERROR("Invalid extractelement operands!");
Robert Bocchino9c62b562006-01-10 19:04:32 +00001692 $$ = ConstantExpr::getExtractElement($3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001693 CHECK_FOR_ERROR
Chris Lattnerca73cd82006-04-08 03:53:34 +00001694 }
1695 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Chris Lattner2d7349a2006-04-08 04:08:32 +00001696 if (!InsertElementInst::isValidOperands($3, $5, $7))
Reid Spencer61c83e02006-08-18 08:43:06 +00001697 GEN_ERROR("Invalid insertelement operands!");
Chris Lattnerca73cd82006-04-08 03:53:34 +00001698 $$ = ConstantExpr::getInsertElement($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001699 CHECK_FOR_ERROR
Chris Lattnerca73cd82006-04-08 03:53:34 +00001700 }
1701 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1702 if (!ShuffleVectorInst::isValidOperands($3, $5, $7))
Reid Spencer61c83e02006-08-18 08:43:06 +00001703 GEN_ERROR("Invalid shufflevector operands!");
Chris Lattnerca73cd82006-04-08 03:53:34 +00001704 $$ = ConstantExpr::getShuffleVector($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001705 CHECK_FOR_ERROR
Chris Lattner699f1eb2002-08-14 17:12:33 +00001706 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001707
Chris Lattnerca73cd82006-04-08 03:53:34 +00001708
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001709// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001710ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001711 ($$ = $1)->push_back($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00001712 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001713 }
1714 | ConstVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001715 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001716 $$->push_back($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001717 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001718 };
Chris Lattner00950542001-06-06 20:29:01 +00001719
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001720
Chris Lattner1781aca2001-09-18 04:00:54 +00001721// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001722GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001723
Chris Lattner00950542001-06-06 20:29:01 +00001724
Chris Lattner0e73ce62002-05-02 19:11:13 +00001725//===----------------------------------------------------------------------===//
1726// Rules to match Modules
1727//===----------------------------------------------------------------------===//
1728
1729// Module rule: Capture the result of parsing the whole file into a result
1730// variable...
1731//
1732Module : FunctionList {
1733 $$ = ParserResult = $1;
1734 CurModule.ModuleDone();
Reid Spencerf63697d2006-10-09 17:36:59 +00001735 CHECK_FOR_ERROR;
Chris Lattner51727be2002-06-04 21:58:56 +00001736};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001737
Chris Lattner7e708292002-06-25 16:13:24 +00001738// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001739//
1740FunctionList : FunctionList Function {
1741 $$ = $1;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001742 CurFun.FunctionDone();
Reid Spencer61c83e02006-08-18 08:43:06 +00001743 CHECK_FOR_ERROR
Chris Lattner0e73ce62002-05-02 19:11:13 +00001744 }
1745 | FunctionList FunctionProto {
1746 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001747 CHECK_FOR_ERROR
Chris Lattner0e73ce62002-05-02 19:11:13 +00001748 }
Chris Lattner71cdba32006-01-24 00:40:17 +00001749 | FunctionList MODULE ASM_TOK AsmBlock {
Chris Lattneree454772006-01-23 23:05:15 +00001750 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001751 CHECK_FOR_ERROR
Chris Lattneree454772006-01-23 23:05:15 +00001752 }
Chris Lattner0e73ce62002-05-02 19:11:13 +00001753 | FunctionList IMPLEMENTATION {
1754 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001755 CHECK_FOR_ERROR
Chris Lattner0e73ce62002-05-02 19:11:13 +00001756 }
1757 | ConstPool {
1758 $$ = CurModule.CurrentModule;
Chris Lattner355ad1f2005-03-21 06:27:42 +00001759 // Emit an error if there are any unresolved types left.
1760 if (!CurModule.LateResolveTypes.empty()) {
1761 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
Reid Spencer61c83e02006-08-18 08:43:06 +00001762 if (DID.Type == ValID::NameVal) {
1763 GEN_ERROR("Reference to an undefined type: '"+DID.getName() + "'");
1764 } else {
1765 GEN_ERROR("Reference to an undefined type: #" + itostr(DID.Num));
1766 }
Chris Lattner355ad1f2005-03-21 06:27:42 +00001767 }
Reid Spencer61c83e02006-08-18 08:43:06 +00001768 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001769 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001770
Chris Lattnere98dda62001-07-14 06:10:16 +00001771// ConstPool - Constants with optional names assigned to them.
Chris Lattner355ad1f2005-03-21 06:27:42 +00001772ConstPool : ConstPool OptAssign TYPE TypesV {
Chris Lattner4a42e902001-10-22 05:56:09 +00001773 // Eagerly resolve types. This is not an optimization, this is a
1774 // requirement that is due to the fact that we could have this:
1775 //
1776 // %list = type { %list * }
1777 // %list = type { %list * } ; repeated type decl
1778 //
1779 // If types are not resolved eagerly, then the two types will not be
1780 // determined to be the same type!
1781 //
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001782 ResolveTypeTo($2, *$4);
Chris Lattner4a42e902001-10-22 05:56:09 +00001783
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001784 if (!setTypeName(*$4, $2) && !$2) {
Reid Spencer5b7e7532006-09-28 19:28:24 +00001785 CHECK_FOR_ERROR
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001786 // If this is a named type that is not a redefinition, add it to the slot
1787 // table.
Chris Lattner355ad1f2005-03-21 06:27:42 +00001788 CurModule.Types.push_back(*$4);
Chris Lattner30c89792001-09-07 16:35:17 +00001789 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001790
1791 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00001792 CHECK_FOR_ERROR
Chris Lattner30c89792001-09-07 16:35:17 +00001793 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001794 | ConstPool FunctionProto { // Function prototypes can be in const pool
Reid Spencer61c83e02006-08-18 08:43:06 +00001795 CHECK_FOR_ERROR
Chris Lattner93750fa2001-07-28 17:48:55 +00001796 }
Chris Lattner71cdba32006-01-24 00:40:17 +00001797 | ConstPool MODULE ASM_TOK AsmBlock { // Asm blocks can be in the const pool
Reid Spencer61c83e02006-08-18 08:43:06 +00001798 CHECK_FOR_ERROR
Chris Lattneree454772006-01-23 23:05:15 +00001799 }
Chris Lattnerb2b96672005-11-12 18:21:21 +00001800 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Reid Spencer5b7e7532006-09-28 19:28:24 +00001801 if ($5 == 0)
1802 GEN_ERROR("Global value initializer is not a constant!");
Chris Lattnerb2b96672005-11-12 18:21:21 +00001803 CurGV = ParseGlobalVariable($2, $3, $4, $5->getType(), $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001804 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00001805 } GlobalVarAttributes {
1806 CurGV = 0;
Chris Lattner1781aca2001-09-18 04:00:54 +00001807 }
Chris Lattnerb2b96672005-11-12 18:21:21 +00001808 | ConstPool OptAssign EXTERNAL GlobalType Types {
Reid Spencer5b7e7532006-09-28 19:28:24 +00001809 CurGV = ParseGlobalVariable($2, GlobalValue::ExternalLinkage, $4, *$5, 0);
1810 CHECK_FOR_ERROR
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001811 delete $5;
Reid Spencer5b7e7532006-09-28 19:28:24 +00001812 } GlobalVarAttributes {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001813 CurGV = 0;
1814 CHECK_FOR_ERROR
1815 }
1816 | ConstPool OptAssign DLLIMPORT GlobalType Types {
Reid Spencer5b7e7532006-09-28 19:28:24 +00001817 CurGV = ParseGlobalVariable($2, GlobalValue::DLLImportLinkage, $4, *$5, 0);
1818 CHECK_FOR_ERROR
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001819 delete $5;
Reid Spencer5b7e7532006-09-28 19:28:24 +00001820 } GlobalVarAttributes {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001821 CurGV = 0;
1822 CHECK_FOR_ERROR
1823 }
1824 | ConstPool OptAssign EXTERN_WEAK GlobalType Types {
Reid Spencer5b7e7532006-09-28 19:28:24 +00001825 CurGV =
1826 ParseGlobalVariable($2, GlobalValue::ExternalWeakLinkage, $4, *$5, 0);
1827 CHECK_FOR_ERROR
Chris Lattner1f862af2003-04-16 18:13:57 +00001828 delete $5;
Reid Spencer5b7e7532006-09-28 19:28:24 +00001829 } GlobalVarAttributes {
Chris Lattnerb2b96672005-11-12 18:21:21 +00001830 CurGV = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00001831 CHECK_FOR_ERROR
Chris Lattnere98dda62001-07-14 06:10:16 +00001832 }
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001833 | ConstPool TARGET TargetDefinition {
Reid Spencer61c83e02006-08-18 08:43:06 +00001834 CHECK_FOR_ERROR
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001835 }
Reid Spencer0be13e72004-07-25 17:58:28 +00001836 | ConstPool DEPLIBS '=' LibrariesDefinition {
Reid Spencer61c83e02006-08-18 08:43:06 +00001837 CHECK_FOR_ERROR
Reid Spencer0be13e72004-07-25 17:58:28 +00001838 }
Chris Lattner00950542001-06-06 20:29:01 +00001839 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001840 };
Chris Lattner00950542001-06-06 20:29:01 +00001841
1842
Chris Lattneree454772006-01-23 23:05:15 +00001843AsmBlock : STRINGCONSTANT {
Chris Lattner66316012006-01-24 04:14:29 +00001844 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
Chris Lattneree454772006-01-23 23:05:15 +00001845 char *EndStr = UnEscapeLexed($1, true);
1846 std::string NewAsm($1, EndStr);
1847 free($1);
1848
1849 if (AsmSoFar.empty())
Chris Lattner66316012006-01-24 04:14:29 +00001850 CurModule.CurrentModule->setModuleInlineAsm(NewAsm);
Chris Lattneree454772006-01-23 23:05:15 +00001851 else
Chris Lattner66316012006-01-24 04:14:29 +00001852 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+NewAsm);
Reid Spencer61c83e02006-08-18 08:43:06 +00001853 CHECK_FOR_ERROR
Chris Lattneree454772006-01-23 23:05:15 +00001854};
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001855
1856BigOrLittle : BIG { $$ = Module::BigEndian; };
1857BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1858
1859TargetDefinition : ENDIAN '=' BigOrLittle {
1860 CurModule.CurrentModule->setEndianness($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00001861 CHECK_FOR_ERROR
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001862 }
1863 | POINTERSIZE '=' EUINT64VAL {
1864 if ($3 == 32)
1865 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1866 else if ($3 == 64)
1867 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1868 else
Reid Spencer61c83e02006-08-18 08:43:06 +00001869 GEN_ERROR("Invalid pointer size: '" + utostr($3) + "'!");
1870 CHECK_FOR_ERROR
Reid Spencer0be13e72004-07-25 17:58:28 +00001871 }
1872 | TRIPLE '=' STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001873 CurModule.CurrentModule->setTargetTriple($3);
1874 free($3);
John Criswell2f6a8b12006-10-24 19:09:48 +00001875 }
Chris Lattner10b27112006-10-22 06:07:41 +00001876 | DATALAYOUT '=' STRINGCONSTANT {
Owen Andersoncf7ff2b2006-10-18 02:21:12 +00001877 CurModule.CurrentModule->setDataLayout($3);
1878 free($3);
Owen Andersoncf7ff2b2006-10-18 02:21:12 +00001879 };
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001880
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001881LibrariesDefinition : '[' LibList ']';
Reid Spencer0be13e72004-07-25 17:58:28 +00001882
1883LibList : LibList ',' STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001884 CurModule.CurrentModule->addLibrary($3);
1885 free($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00001886 CHECK_FOR_ERROR
Reid Spencer0be13e72004-07-25 17:58:28 +00001887 }
1888 | STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001889 CurModule.CurrentModule->addLibrary($1);
1890 free($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001891 CHECK_FOR_ERROR
Reid Spencer0be13e72004-07-25 17:58:28 +00001892 }
1893 | /* empty: end of list */ {
Reid Spencer61c83e02006-08-18 08:43:06 +00001894 CHECK_FOR_ERROR
Reid Spencer0be13e72004-07-25 17:58:28 +00001895 }
1896 ;
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001897
Chris Lattner00950542001-06-06 20:29:01 +00001898//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001899// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001900//===----------------------------------------------------------------------===//
1901
Chris Lattner6c23f572003-08-22 05:42:10 +00001902Name : VAR_ID | STRINGCONSTANT;
1903OptName : Name | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001904
Chris Lattner6c23f572003-08-22 05:42:10 +00001905ArgVal : Types OptName {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001906 if (*$1 == Type::VoidTy)
Reid Spencer61c83e02006-08-18 08:43:06 +00001907 GEN_ERROR("void typed arguments are invalid!");
Chris Lattner9232b992003-04-22 18:42:41 +00001908 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001909 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001910};
Chris Lattner00950542001-06-06 20:29:01 +00001911
Chris Lattner69da5cf2002-10-13 20:57:00 +00001912ArgListH : ArgListH ',' ArgVal {
1913 $$ = $1;
1914 $1->push_back(*$3);
1915 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001916 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001917 }
1918 | ArgVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001919 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
Chris Lattner69da5cf2002-10-13 20:57:00 +00001920 $$->push_back(*$1);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001921 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001922 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001923 };
Chris Lattner00950542001-06-06 20:29:01 +00001924
1925ArgList : ArgListH {
1926 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001927 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00001928 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001929 | ArgListH ',' DOTDOTDOT {
1930 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001931 $$->push_back(std::pair<PATypeHolder*,
1932 char*>(new PATypeHolder(Type::VoidTy), 0));
Reid Spencer61c83e02006-08-18 08:43:06 +00001933 CHECK_FOR_ERROR
Chris Lattner69da5cf2002-10-13 20:57:00 +00001934 }
1935 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001936 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1937 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
Reid Spencer61c83e02006-08-18 08:43:06 +00001938 CHECK_FOR_ERROR
Chris Lattner69da5cf2002-10-13 20:57:00 +00001939 }
Chris Lattner00950542001-06-06 20:29:01 +00001940 | /* empty */ {
1941 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00001942 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00001943 };
Chris Lattner00950542001-06-06 20:29:01 +00001944
Chris Lattner164c3782005-11-12 00:11:10 +00001945FunctionHeaderH : OptCallingConv TypesV Name '(' ArgList ')'
1946 OptSection OptAlign {
Chris Lattnera8e8f162005-05-06 20:27:19 +00001947 UnEscapeLexed($3);
1948 std::string FunctionName($3);
1949 free($3); // Free strdup'd memory!
Chris Lattnerdda71962001-11-26 18:54:16 +00001950
Chris Lattnera8e8f162005-05-06 20:27:19 +00001951 if (!(*$2)->isFirstClassType() && *$2 != Type::VoidTy)
Reid Spencer61c83e02006-08-18 08:43:06 +00001952 GEN_ERROR("LLVM functions cannot return aggregate types!");
Chris Lattnerc282f5a2003-11-21 22:32:23 +00001953
Chris Lattner9232b992003-04-22 18:42:41 +00001954 std::vector<const Type*> ParamTypeList;
Chris Lattnera8e8f162005-05-06 20:27:19 +00001955 if ($5) { // If there are arguments...
1956 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $5->begin();
1957 I != $5->end(); ++I)
Chris Lattner69da5cf2002-10-13 20:57:00 +00001958 ParamTypeList.push_back(I->first->get());
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001959 }
Chris Lattner00950542001-06-06 20:29:01 +00001960
Chris Lattner2079fde2001-10-13 06:41:08 +00001961 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1962 if (isVarArg) ParamTypeList.pop_back();
1963
Chris Lattnera8e8f162005-05-06 20:27:19 +00001964 const FunctionType *FT = FunctionType::get(*$2, ParamTypeList, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001965 const PointerType *PFT = PointerType::get(FT);
Chris Lattnera8e8f162005-05-06 20:27:19 +00001966 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001967
Chris Lattnera09000d2004-07-14 23:03:46 +00001968 ValID ID;
1969 if (!FunctionName.empty()) {
1970 ID = ValID::create((char*)FunctionName.c_str());
1971 } else {
1972 ID = ValID::create((int)CurModule.Values[PFT].size());
1973 }
1974
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001975 Function *Fn = 0;
Chris Lattnera09000d2004-07-14 23:03:46 +00001976 // See if this function was forward referenced. If so, recycle the object.
1977 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
1978 // Move the function to the end of the list, from whereever it was
1979 // previously inserted.
1980 Fn = cast<Function>(FWRef);
1981 CurModule.CurrentModule->getFunctionList().remove(Fn);
1982 CurModule.CurrentModule->getFunctionList().push_back(Fn);
1983 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
1984 (Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1985 // If this is the case, either we need to be a forward decl, or it needs
1986 // to be.
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001987 if (!CurFun.isDeclare && !Fn->isExternal())
Reid Spencer61c83e02006-08-18 08:43:06 +00001988 GEN_ERROR("Redefinition of function '" + FunctionName + "'!");
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001989
Chris Lattnera09000d2004-07-14 23:03:46 +00001990 // Make sure to strip off any argument names so we can't get conflicts.
1991 if (Fn->isExternal())
Chris Lattnere4d5c442005-03-15 04:54:21 +00001992 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
Chris Lattnera09000d2004-07-14 23:03:46 +00001993 AI != AE; ++AI)
1994 AI->setName("");
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001995 } else { // Not already defined?
1996 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
1997 CurModule.CurrentModule);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001998
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001999 InsertValue(Fn, CurModule.Values);
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00002000 }
Chris Lattner00950542001-06-06 20:29:01 +00002001
Chris Lattner394f2eb2003-10-16 20:12:13 +00002002 CurFun.FunctionStart(Fn);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00002003
2004 if (CurFun.isDeclare) {
2005 // If we have declaration, always overwrite linkage. This will allow us to
2006 // correctly handle cases, when pointer to function is passed as argument to
2007 // another function.
2008 Fn->setLinkage(CurFun.Linkage);
2009 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00002010 Fn->setCallingConv($1);
Chris Lattner164c3782005-11-12 00:11:10 +00002011 Fn->setAlignment($8);
2012 if ($7) {
2013 Fn->setSection($7);
2014 free($7);
2015 }
Chris Lattner00950542001-06-06 20:29:01 +00002016
Chris Lattner7e708292002-06-25 16:13:24 +00002017 // Add all of the arguments we parsed to the function...
Chris Lattnera8e8f162005-05-06 20:27:19 +00002018 if ($5) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00002019 if (isVarArg) { // Nuke the last entry
Chris Lattnera8e8f162005-05-06 20:27:19 +00002020 assert($5->back().first->get() == Type::VoidTy && $5->back().second == 0&&
Chris Lattner69da5cf2002-10-13 20:57:00 +00002021 "Not a varargs marker!");
Chris Lattnera8e8f162005-05-06 20:27:19 +00002022 delete $5->back().first;
2023 $5->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00002024 }
Chris Lattnere4d5c442005-03-15 04:54:21 +00002025 Function::arg_iterator ArgIt = Fn->arg_begin();
Chris Lattnera8e8f162005-05-06 20:27:19 +00002026 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $5->begin();
2027 I != $5->end(); ++I, ++ArgIt) {
Chris Lattner69da5cf2002-10-13 20:57:00 +00002028 delete I->first; // Delete the typeholder...
2029
Chris Lattner8ab406d2004-07-13 07:59:27 +00002030 setValueName(ArgIt, I->second); // Insert arg into symtab...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002031 CHECK_FOR_ERROR
Chris Lattner69da5cf2002-10-13 20:57:00 +00002032 InsertValue(ArgIt);
Chris Lattner00950542001-06-06 20:29:01 +00002033 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00002034
Chris Lattnera8e8f162005-05-06 20:27:19 +00002035 delete $5; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00002036 }
Reid Spencer61c83e02006-08-18 08:43:06 +00002037 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002038};
Chris Lattner00950542001-06-06 20:29:01 +00002039
Chris Lattner9b02cc32002-05-03 18:23:48 +00002040BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
2041
Chris Lattner4ad02e72003-04-16 20:28:45 +00002042FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00002043 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00002044
Chris Lattner4ad02e72003-04-16 20:28:45 +00002045 // Make sure that we keep track of the linkage type even if there was a
2046 // previous "declare".
2047 $$->setLinkage($1);
Chris Lattner51727be2002-06-04 21:58:56 +00002048};
Chris Lattner00950542001-06-06 20:29:01 +00002049
Chris Lattner9b02cc32002-05-03 18:23:48 +00002050END : ENDTOK | '}'; // Allow end of '}' to end a function
2051
Chris Lattner79df7c02002-03-26 18:01:55 +00002052Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00002053 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002054 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002055};
Chris Lattner00950542001-06-06 20:29:01 +00002056
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002057FnDeclareLinkage: /*default*/ |
2058 DLLIMPORT { CurFun.Linkage = GlobalValue::DLLImportLinkage } |
2059 EXTERN_WEAK { CurFun.Linkage = GlobalValue::DLLImportLinkage };
2060
2061FunctionProto : DECLARE { CurFun.isDeclare = true; } FnDeclareLinkage FunctionHeaderH {
2062 $$ = CurFun.CurrentFunction;
2063 CurFun.FunctionDone();
2064 CHECK_FOR_ERROR
2065 };
Chris Lattner00950542001-06-06 20:29:01 +00002066
2067//===----------------------------------------------------------------------===//
2068// Rules to match Basic Blocks
2069//===----------------------------------------------------------------------===//
2070
Chris Lattneraa2c8532006-01-25 22:26:43 +00002071OptSideEffect : /* empty */ {
2072 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002073 CHECK_FOR_ERROR
Chris Lattneraa2c8532006-01-25 22:26:43 +00002074 }
2075 | SIDEEFFECT {
2076 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002077 CHECK_FOR_ERROR
Chris Lattneraa2c8532006-01-25 22:26:43 +00002078 };
2079
Chris Lattner00950542001-06-06 20:29:01 +00002080ConstValueRef : ESINT64VAL { // A reference to a direct constant
2081 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002082 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002083 }
2084 | EUINT64VAL {
2085 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002086 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002087 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00002088 | FPVAL { // Perhaps it's an FP constant?
2089 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002090 CHECK_FOR_ERROR
Chris Lattner3d52b2f2001-07-15 00:17:01 +00002091 }
Chris Lattner91ef4602004-03-31 03:49:47 +00002092 | TRUETOK {
Chris Lattner47811b72006-09-28 23:35:22 +00002093 $$ = ValID::create(ConstantBool::getTrue());
Reid Spencer61c83e02006-08-18 08:43:06 +00002094 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002095 }
Chris Lattner91ef4602004-03-31 03:49:47 +00002096 | FALSETOK {
Chris Lattner47811b72006-09-28 23:35:22 +00002097 $$ = ValID::create(ConstantBool::getFalse());
Reid Spencer61c83e02006-08-18 08:43:06 +00002098 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002099 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00002100 | NULL_TOK {
2101 $$ = ValID::createNull();
Reid Spencer61c83e02006-08-18 08:43:06 +00002102 CHECK_FOR_ERROR
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002103 }
Chris Lattner16710e92004-10-16 18:17:13 +00002104 | UNDEF {
2105 $$ = ValID::createUndef();
Reid Spencer61c83e02006-08-18 08:43:06 +00002106 CHECK_FOR_ERROR
Chris Lattner16710e92004-10-16 18:17:13 +00002107 }
Chris Lattnerf1f03df2005-12-21 17:53:02 +00002108 | ZEROINITIALIZER { // A vector zero constant.
2109 $$ = ValID::createZeroInit();
Reid Spencer61c83e02006-08-18 08:43:06 +00002110 CHECK_FOR_ERROR
Chris Lattnerf1f03df2005-12-21 17:53:02 +00002111 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00002112 | '<' ConstVector '>' { // Nonempty unsized packed vector
2113 const Type *ETy = (*$2)[0]->getType();
2114 int NumElements = $2->size();
2115
2116 PackedType* pt = PackedType::get(ETy, NumElements);
2117 PATypeHolder* PTy = new PATypeHolder(
2118 HandleUpRefs(
2119 PackedType::get(
2120 ETy,
2121 NumElements)
2122 )
2123 );
2124
2125 // Verify all elements are correct type!
2126 for (unsigned i = 0; i < $2->size(); i++) {
2127 if (ETy != (*$2)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002128 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Brian Gaeke715c90b2004-08-20 06:00:58 +00002129 ETy->getDescription() +"' as required!\nIt is of type '" +
2130 (*$2)[i]->getType()->getDescription() + "'.");
2131 }
2132
2133 $$ = ValID::create(ConstantPacked::get(pt, *$2));
2134 delete PTy; delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002135 CHECK_FOR_ERROR
Brian Gaeke715c90b2004-08-20 06:00:58 +00002136 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00002137 | ConstExpr {
2138 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002139 CHECK_FOR_ERROR
Chris Lattneraa2c8532006-01-25 22:26:43 +00002140 }
2141 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
2142 char *End = UnEscapeLexed($3, true);
2143 std::string AsmStr = std::string($3, End);
2144 End = UnEscapeLexed($5, true);
2145 std::string Constraints = std::string($5, End);
2146 $$ = ValID::createInlineAsm(AsmStr, Constraints, $2);
2147 free($3);
2148 free($5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002149 CHECK_FOR_ERROR
Chris Lattnerd78700d2002-08-16 21:14:40 +00002150 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00002151
Chris Lattner2079fde2001-10-13 06:41:08 +00002152// SymbolicValueRef - Reference to one of two ways of symbolically refering to
2153// another value.
2154//
2155SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00002156 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002157 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002158 }
Chris Lattner6c23f572003-08-22 05:42:10 +00002159 | Name { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00002160 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002161 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002162 };
Chris Lattner2079fde2001-10-13 06:41:08 +00002163
2164// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00002165ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00002166
Chris Lattner00950542001-06-06 20:29:01 +00002167
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002168// ResolvedVal - a <type> <value> pair. This is used only in cases where the
2169// type immediately preceeds the value reference, and allows complex constant
2170// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00002171ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00002172 $$ = getVal(*$1, $2); delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002173 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002174 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00002175
Chris Lattner00950542001-06-06 20:29:01 +00002176BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner8ab406d2004-07-13 07:59:27 +00002177 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002178 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002179 }
Chris Lattner7e708292002-06-25 16:13:24 +00002180 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
Chris Lattner8ab406d2004-07-13 07:59:27 +00002181 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002182 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002183 };
Chris Lattner00950542001-06-06 20:29:01 +00002184
2185
2186// Basic blocks are terminated by branching instructions:
2187// br, br/cc, switch, ret
2188//
Chris Lattner2079fde2001-10-13 06:41:08 +00002189BasicBlock : InstructionList OptAssign BBTerminatorInst {
Chris Lattner8ab406d2004-07-13 07:59:27 +00002190 setValueName($3, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002191 CHECK_FOR_ERROR
Chris Lattner2079fde2001-10-13 06:41:08 +00002192 InsertValue($3);
2193
2194 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00002195 InsertValue($1);
2196 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002197 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002198 };
Chris Lattner00950542001-06-06 20:29:01 +00002199
2200InstructionList : InstructionList Inst {
2201 $1->getInstList().push_back($2);
2202 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002203 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002204 }
2205 | /* empty */ {
Andrew Lenharth558bc882005-06-18 18:34:52 +00002206 $$ = CurBB = getBBVal(ValID::create((int)CurFun.NextBBNum++), true);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002207 CHECK_FOR_ERROR
Chris Lattner8d65a062004-07-26 01:40:20 +00002208
2209 // Make sure to move the basic block to the correct location in the
2210 // function, instead of leaving it inserted wherever it was first
2211 // referenced.
Chris Lattnerf924a4c2005-05-06 19:58:35 +00002212 Function::BasicBlockListType &BBL =
2213 CurFun.CurrentFunction->getBasicBlockList();
2214 BBL.splice(BBL.end(), BBL, $$);
Reid Spencer61c83e02006-08-18 08:43:06 +00002215 CHECK_FOR_ERROR
Chris Lattner22ae2322004-07-13 08:39:15 +00002216 }
2217 | LABELSTR {
Andrew Lenharth558bc882005-06-18 18:34:52 +00002218 $$ = CurBB = getBBVal(ValID::create($1), true);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002219 CHECK_FOR_ERROR
Chris Lattner8d65a062004-07-26 01:40:20 +00002220
2221 // Make sure to move the basic block to the correct location in the
2222 // function, instead of leaving it inserted wherever it was first
2223 // referenced.
Chris Lattnerf924a4c2005-05-06 19:58:35 +00002224 Function::BasicBlockListType &BBL =
2225 CurFun.CurrentFunction->getBasicBlockList();
2226 BBL.splice(BBL.end(), BBL, $$);
Reid Spencer61c83e02006-08-18 08:43:06 +00002227 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002228 };
Chris Lattner00950542001-06-06 20:29:01 +00002229
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002230BBTerminatorInst : RET ResolvedVal { // Return with a result...
2231 $$ = new ReturnInst($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00002232 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002233 }
2234 | RET VOID { // Return with no result...
2235 $$ = new ReturnInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002236 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002237 }
2238 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002239 BasicBlock* tmpBB = getBBVal($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002240 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002241 $$ = new BranchInst(tmpBB);
Chris Lattner00950542001-06-06 20:29:01 +00002242 } // Conditional Branch...
2243 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Reid Spencer5b7e7532006-09-28 19:28:24 +00002244 BasicBlock* tmpBBA = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002245 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002246 BasicBlock* tmpBBB = getBBVal($9);
2247 CHECK_FOR_ERROR
2248 Value* tmpVal = getVal(Type::BoolTy, $3);
2249 CHECK_FOR_ERROR
2250 $$ = new BranchInst(tmpBBA, tmpBBB, tmpVal);
Chris Lattner00950542001-06-06 20:29:01 +00002251 }
2252 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencer5b7e7532006-09-28 19:28:24 +00002253 Value* tmpVal = getVal($2, $3);
2254 CHECK_FOR_ERROR
2255 BasicBlock* tmpBB = getBBVal($6);
2256 CHECK_FOR_ERROR
2257 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, $8->size());
Chris Lattner00950542001-06-06 20:29:01 +00002258 $$ = S;
2259
Chris Lattner9232b992003-04-22 18:42:41 +00002260 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00002261 E = $8->end();
Chris Lattner69331f52005-02-24 05:25:17 +00002262 for (; I != E; ++I) {
2263 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
2264 S->addCase(CI, I->second);
2265 else
Reid Spencer61c83e02006-08-18 08:43:06 +00002266 GEN_ERROR("Switch case is constant, but not a simple integer!");
Chris Lattner69331f52005-02-24 05:25:17 +00002267 }
Chris Lattnerf57a43d2004-04-17 23:49:15 +00002268 delete $8;
Reid Spencer61c83e02006-08-18 08:43:06 +00002269 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002270 }
Chris Lattner196850c2003-05-15 21:30:00 +00002271 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencer5b7e7532006-09-28 19:28:24 +00002272 Value* tmpVal = getVal($2, $3);
2273 CHECK_FOR_ERROR
2274 BasicBlock* tmpBB = getBBVal($6);
2275 CHECK_FOR_ERROR
2276 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, 0);
Chris Lattner196850c2003-05-15 21:30:00 +00002277 $$ = S;
Reid Spencer61c83e02006-08-18 08:43:06 +00002278 CHECK_FOR_ERROR
Chris Lattner196850c2003-05-15 21:30:00 +00002279 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00002280 | INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
2281 TO LABEL ValueRef UNWIND LABEL ValueRef {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002282 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00002283 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00002284
Chris Lattnera8e8f162005-05-06 20:27:19 +00002285 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002286 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00002287 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00002288 std::vector<const Type*> ParamTypes;
Chris Lattnera8e8f162005-05-06 20:27:19 +00002289 if ($6) {
2290 for (std::vector<Value*>::iterator I = $6->begin(), E = $6->end();
Chris Lattner9232b992003-04-22 18:42:41 +00002291 I != E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00002292 ParamTypes.push_back((*I)->getType());
2293 }
2294
2295 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
2296 if (isVarArg) ParamTypes.pop_back();
2297
Chris Lattnera8e8f162005-05-06 20:27:19 +00002298 Ty = FunctionType::get($3->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002299 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00002300 }
Chris Lattner2079fde2001-10-13 06:41:08 +00002301
Chris Lattnera8e8f162005-05-06 20:27:19 +00002302 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002303 CHECK_FOR_ERROR
Chris Lattnera8e8f162005-05-06 20:27:19 +00002304 BasicBlock *Normal = getBBVal($10);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002305 CHECK_FOR_ERROR
Chris Lattnera8e8f162005-05-06 20:27:19 +00002306 BasicBlock *Except = getBBVal($13);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002307 CHECK_FOR_ERROR
Chris Lattner2079fde2001-10-13 06:41:08 +00002308
2309 // Create the call node...
Chris Lattnera8e8f162005-05-06 20:27:19 +00002310 if (!$6) { // Has no arguments?
Chris Lattner9232b992003-04-22 18:42:41 +00002311 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00002312 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00002313 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00002314 // correctly!
2315 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00002316 FunctionType::param_iterator I = Ty->param_begin();
2317 FunctionType::param_iterator E = Ty->param_end();
Chris Lattnera8e8f162005-05-06 20:27:19 +00002318 std::vector<Value*>::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00002319
2320 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
Reid Spencer3271ed52004-07-18 00:08:11 +00002321 if ((*ArgI)->getType() != *I)
Reid Spencer61c83e02006-08-18 08:43:06 +00002322 GEN_ERROR("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Reid Spencer3271ed52004-07-18 00:08:11 +00002323 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00002324
2325 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002326 GEN_ERROR("Invalid number of parameters detected!");
Chris Lattner2079fde2001-10-13 06:41:08 +00002327
Chris Lattnera8e8f162005-05-06 20:27:19 +00002328 $$ = new InvokeInst(V, Normal, Except, *$6);
Chris Lattner2079fde2001-10-13 06:41:08 +00002329 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00002330 cast<InvokeInst>($$)->setCallingConv($2);
2331
2332 delete $3;
2333 delete $6;
Reid Spencer61c83e02006-08-18 08:43:06 +00002334 CHECK_FOR_ERROR
Chris Lattner36143fc2003-09-08 18:54:55 +00002335 }
2336 | UNWIND {
2337 $$ = new UnwindInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002338 CHECK_FOR_ERROR
Chris Lattner16710e92004-10-16 18:17:13 +00002339 }
2340 | UNREACHABLE {
2341 $$ = new UnreachableInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002342 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002343 };
Chris Lattner2079fde2001-10-13 06:41:08 +00002344
2345
Chris Lattner00950542001-06-06 20:29:01 +00002346
2347JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
2348 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00002349 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Reid Spencer5b7e7532006-09-28 19:28:24 +00002350 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002351 if (V == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002352 GEN_ERROR("May only switch on a constant pool value!");
Chris Lattner00950542001-06-06 20:29:01 +00002353
Reid Spencer5b7e7532006-09-28 19:28:24 +00002354 BasicBlock* tmpBB = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002355 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002356 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner00950542001-06-06 20:29:01 +00002357 }
2358 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00002359 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00002360 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Reid Spencer5b7e7532006-09-28 19:28:24 +00002361 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002362
2363 if (V == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002364 GEN_ERROR("May only switch on a constant pool value!");
Chris Lattner00950542001-06-06 20:29:01 +00002365
Reid Spencer5b7e7532006-09-28 19:28:24 +00002366 BasicBlock* tmpBB = getBBVal($5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002367 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002368 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner51727be2002-06-04 21:58:56 +00002369 };
Chris Lattner00950542001-06-06 20:29:01 +00002370
2371Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00002372 // Is this definition named?? if so, assign the name...
Chris Lattner8ab406d2004-07-13 07:59:27 +00002373 setValueName($2, $1);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002374 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002375 InsertValue($2);
2376 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002377 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002378};
Chris Lattner00950542001-06-06 20:29:01 +00002379
Chris Lattnerc24d2082001-06-11 15:04:20 +00002380PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Chris Lattner9232b992003-04-22 18:42:41 +00002381 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Reid Spencer5b7e7532006-09-28 19:28:24 +00002382 Value* tmpVal = getVal(*$1, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002383 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002384 BasicBlock* tmpBB = getBBVal($5);
2385 CHECK_FOR_ERROR
2386 $$->push_back(std::make_pair(tmpVal, tmpBB));
2387 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00002388 }
2389 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
2390 $$ = $1;
Reid Spencer5b7e7532006-09-28 19:28:24 +00002391 Value* tmpVal = getVal($1->front().first->getType(), $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002392 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002393 BasicBlock* tmpBB = getBBVal($6);
2394 CHECK_FOR_ERROR
2395 $1->push_back(std::make_pair(tmpVal, tmpBB));
Chris Lattner51727be2002-06-04 21:58:56 +00002396 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00002397
2398
Chris Lattner30c89792001-09-07 16:35:17 +00002399ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner9232b992003-04-22 18:42:41 +00002400 $$ = new std::vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002401 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00002402 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002403 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00002404 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002405 $1->push_back($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002406 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002407 };
Chris Lattner00950542001-06-06 20:29:01 +00002408
2409// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00002410ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00002411
Chris Lattnerddb6db42005-05-06 05:51:46 +00002412OptTailCall : TAIL CALL {
2413 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002414 CHECK_FOR_ERROR
Chris Lattnerddb6db42005-05-06 05:51:46 +00002415 }
2416 | CALL {
2417 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002418 CHECK_FOR_ERROR
Chris Lattnerddb6db42005-05-06 05:51:46 +00002419 };
2420
Chris Lattner4a6482b2002-09-10 19:57:26 +00002421InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Brian Gaeke715c90b2004-08-20 06:00:58 +00002422 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
2423 !isa<PackedType>((*$2).get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002424 GEN_ERROR(
Brian Gaeke715c90b2004-08-20 06:00:58 +00002425 "Arithmetic operator requires integer, FP, or packed operands!");
Misha Brukman5a2a3822005-05-10 22:02:28 +00002426 if (isa<PackedType>((*$2).get()) && $1 == Instruction::Rem)
Reid Spencer61c83e02006-08-18 08:43:06 +00002427 GEN_ERROR("Rem not supported on packed types!");
Reid Spencer5b7e7532006-09-28 19:28:24 +00002428 Value* val1 = getVal(*$2, $3);
2429 CHECK_FOR_ERROR
2430 Value* val2 = getVal(*$2, $5);
2431 CHECK_FOR_ERROR
2432 $$ = BinaryOperator::create($1, val1, val2);
Chris Lattner4a6482b2002-09-10 19:57:26 +00002433 if ($$ == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002434 GEN_ERROR("binary operator returned null!");
Chris Lattner4a6482b2002-09-10 19:57:26 +00002435 delete $2;
2436 }
2437 | LogicalOps Types ValueRef ',' ValueRef {
Chris Lattner0a017832005-12-21 18:31:29 +00002438 if (!(*$2)->isIntegral()) {
2439 if (!isa<PackedType>($2->get()) ||
2440 !cast<PackedType>($2->get())->getElementType()->isIntegral())
Reid Spencer61c83e02006-08-18 08:43:06 +00002441 GEN_ERROR("Logical operator requires integral operands!");
Chris Lattner0a017832005-12-21 18:31:29 +00002442 }
Reid Spencer5b7e7532006-09-28 19:28:24 +00002443 Value* tmpVal1 = getVal(*$2, $3);
2444 CHECK_FOR_ERROR
2445 Value* tmpVal2 = getVal(*$2, $5);
2446 CHECK_FOR_ERROR
2447 $$ = BinaryOperator::create($1, tmpVal1, tmpVal2);
Chris Lattner4a6482b2002-09-10 19:57:26 +00002448 if ($$ == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002449 GEN_ERROR("binary operator returned null!");
Chris Lattner4a6482b2002-09-10 19:57:26 +00002450 delete $2;
2451 }
2452 | SetCondOps Types ValueRef ',' ValueRef {
Reid Spencer57b6eec2004-08-21 16:11:02 +00002453 if(isa<PackedType>((*$2).get())) {
Reid Spencer61c83e02006-08-18 08:43:06 +00002454 GEN_ERROR(
Reid Spencer57b6eec2004-08-21 16:11:02 +00002455 "PackedTypes currently not supported in setcc instructions!");
2456 }
Reid Spencer5b7e7532006-09-28 19:28:24 +00002457 Value* tmpVal1 = getVal(*$2, $3);
2458 CHECK_FOR_ERROR
2459 Value* tmpVal2 = getVal(*$2, $5);
2460 CHECK_FOR_ERROR
2461 $$ = new SetCondInst($1, tmpVal1, tmpVal2);
Chris Lattner00950542001-06-06 20:29:01 +00002462 if ($$ == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002463 GEN_ERROR("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00002464 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002465 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00002466 | NOT ResolvedVal {
2467 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
2468 << " Replacing with 'xor'.\n";
2469
2470 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
2471 if (Ones == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002472 GEN_ERROR("Expected integral type for not instruction!");
Chris Lattner699f1eb2002-08-14 17:12:33 +00002473
2474 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00002475 if ($$ == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002476 GEN_ERROR("Could not create a xor instruction!");
2477 CHECK_FOR_ERROR
Chris Lattner09083092001-07-08 04:57:15 +00002478 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002479 | ShiftOps ResolvedVal ',' ResolvedVal {
2480 if ($4->getType() != Type::UByteTy)
Reid Spencer61c83e02006-08-18 08:43:06 +00002481 GEN_ERROR("Shift amount must be ubyte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00002482 if (!$2->getType()->isInteger())
Reid Spencer61c83e02006-08-18 08:43:06 +00002483 GEN_ERROR("Shift constant expression requires integer operand!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002484 $$ = new ShiftInst($1, $2, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002485 CHECK_FOR_ERROR
Chris Lattner027dcc52001-07-08 21:10:27 +00002486 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002487 | CAST ResolvedVal TO Types {
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00002488 if (!$4->get()->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002489 GEN_ERROR("cast instruction to a non-primitive type: '" +
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00002490 $4->get()->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00002491 $$ = new CastInst($2, *$4);
2492 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00002493 CHECK_FOR_ERROR
Chris Lattner09083092001-07-08 04:57:15 +00002494 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00002495 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
2496 if ($2->getType() != Type::BoolTy)
Reid Spencer61c83e02006-08-18 08:43:06 +00002497 GEN_ERROR("select condition must be boolean!");
Chris Lattner76f0ff32004-03-12 05:51:36 +00002498 if ($4->getType() != $6->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002499 GEN_ERROR("select value types should match!");
Chris Lattner76f0ff32004-03-12 05:51:36 +00002500 $$ = new SelectInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002501 CHECK_FOR_ERROR
Chris Lattner76f0ff32004-03-12 05:51:36 +00002502 }
Chris Lattner99e7ab72003-10-18 05:53:13 +00002503 | VAARG ResolvedVal ',' Types {
Andrew Lenharthe78f50d2005-06-19 03:53:56 +00002504 NewVarArgs = true;
Chris Lattner99e7ab72003-10-18 05:53:13 +00002505 $$ = new VAArgInst($2, *$4);
2506 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00002507 CHECK_FOR_ERROR
Chris Lattner99e7ab72003-10-18 05:53:13 +00002508 }
Andrew Lenharth558bc882005-06-18 18:34:52 +00002509 | VAARG_old ResolvedVal ',' Types {
2510 ObsoleteVarArgs = true;
2511 const Type* ArgTy = $2->getType();
2512 Function* NF = CurModule.CurrentModule->
Jeff Cohen66c5fd62005-10-23 04:37:20 +00002513 getOrInsertFunction("llvm.va_copy", ArgTy, ArgTy, (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +00002514
2515 //b = vaarg a, t ->
2516 //foo = alloca 1 of t
2517 //bar = vacopy a
2518 //store bar -> foo
2519 //b = vaarg foo, t
2520 AllocaInst* foo = new AllocaInst(ArgTy, 0, "vaarg.fix");
2521 CurBB->getInstList().push_back(foo);
2522 CallInst* bar = new CallInst(NF, $2);
2523 CurBB->getInstList().push_back(bar);
2524 CurBB->getInstList().push_back(new StoreInst(bar, foo));
2525 $$ = new VAArgInst(foo, *$4);
2526 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00002527 CHECK_FOR_ERROR
Andrew Lenharth558bc882005-06-18 18:34:52 +00002528 }
2529 | VANEXT_old ResolvedVal ',' Types {
2530 ObsoleteVarArgs = true;
2531 const Type* ArgTy = $2->getType();
2532 Function* NF = CurModule.CurrentModule->
Jeff Cohen66c5fd62005-10-23 04:37:20 +00002533 getOrInsertFunction("llvm.va_copy", ArgTy, ArgTy, (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +00002534
2535 //b = vanext a, t ->
2536 //foo = alloca 1 of t
2537 //bar = vacopy a
2538 //store bar -> foo
2539 //tmp = vaarg foo, t
2540 //b = load foo
2541 AllocaInst* foo = new AllocaInst(ArgTy, 0, "vanext.fix");
2542 CurBB->getInstList().push_back(foo);
2543 CallInst* bar = new CallInst(NF, $2);
2544 CurBB->getInstList().push_back(bar);
2545 CurBB->getInstList().push_back(new StoreInst(bar, foo));
2546 Instruction* tmp = new VAArgInst(foo, *$4);
2547 CurBB->getInstList().push_back(tmp);
2548 $$ = new LoadInst(foo);
Chris Lattner8f77dae2003-05-08 02:44:12 +00002549 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00002550 CHECK_FOR_ERROR
Chris Lattner8f77dae2003-05-08 02:44:12 +00002551 }
Robert Bocchino9c62b562006-01-10 19:04:32 +00002552 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Chris Lattner2d7349a2006-04-08 04:08:32 +00002553 if (!ExtractElementInst::isValidOperands($2, $4))
Reid Spencer61c83e02006-08-18 08:43:06 +00002554 GEN_ERROR("Invalid extractelement operands!");
Robert Bocchino9c62b562006-01-10 19:04:32 +00002555 $$ = new ExtractElementInst($2, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002556 CHECK_FOR_ERROR
Robert Bocchino9c62b562006-01-10 19:04:32 +00002557 }
Robert Bocchino2def1b32006-01-17 20:06:25 +00002558 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Chris Lattner2d7349a2006-04-08 04:08:32 +00002559 if (!InsertElementInst::isValidOperands($2, $4, $6))
Reid Spencer61c83e02006-08-18 08:43:06 +00002560 GEN_ERROR("Invalid insertelement operands!");
Robert Bocchino2def1b32006-01-17 20:06:25 +00002561 $$ = new InsertElementInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002562 CHECK_FOR_ERROR
Robert Bocchino2def1b32006-01-17 20:06:25 +00002563 }
Chris Lattner4c949082006-04-08 01:18:35 +00002564 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
2565 if (!ShuffleVectorInst::isValidOperands($2, $4, $6))
Reid Spencer61c83e02006-08-18 08:43:06 +00002566 GEN_ERROR("Invalid shufflevector operands!");
Chris Lattner4c949082006-04-08 01:18:35 +00002567 $$ = new ShuffleVectorInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002568 CHECK_FOR_ERROR
Chris Lattner4c949082006-04-08 01:18:35 +00002569 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00002570 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00002571 const Type *Ty = $2->front().first->getType();
Chris Lattner8ea8f362003-10-30 01:38:18 +00002572 if (!Ty->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002573 GEN_ERROR("PHI node operands must be of first class type!");
Chris Lattnerc24d2082001-06-11 15:04:20 +00002574 $$ = new PHINode(Ty);
Chris Lattnerce1df9e2005-01-29 00:35:55 +00002575 ((PHINode*)$$)->reserveOperandSpace($2->size());
Chris Lattner00950542001-06-06 20:29:01 +00002576 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00002577 if ($2->front().first->getType() != Ty)
Reid Spencer61c83e02006-08-18 08:43:06 +00002578 GEN_ERROR("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00002579 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00002580 $2->pop_front();
2581 }
2582 delete $2; // Free the list...
Reid Spencer61c83e02006-08-18 08:43:06 +00002583 CHECK_FOR_ERROR
Chris Lattnerddb6db42005-05-06 05:51:46 +00002584 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00002585 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002586 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00002587 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00002588
Chris Lattnera8e8f162005-05-06 20:27:19 +00002589 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002590 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00002591 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00002592 std::vector<const Type*> ParamTypes;
Chris Lattnera8e8f162005-05-06 20:27:19 +00002593 if ($6) {
2594 for (std::vector<Value*>::iterator I = $6->begin(), E = $6->end();
Chris Lattner9232b992003-04-22 18:42:41 +00002595 I != E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00002596 ParamTypes.push_back((*I)->getType());
2597 }
Chris Lattner2079fde2001-10-13 06:41:08 +00002598
2599 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
2600 if (isVarArg) ParamTypes.pop_back();
2601
Chris Lattnera8e8f162005-05-06 20:27:19 +00002602 if (!(*$3)->isFirstClassType() && *$3 != Type::VoidTy)
Reid Spencer61c83e02006-08-18 08:43:06 +00002603 GEN_ERROR("LLVM functions cannot return aggregate types!");
Chris Lattner595f06c2005-02-01 01:47:42 +00002604
Chris Lattnera8e8f162005-05-06 20:27:19 +00002605 Ty = FunctionType::get($3->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002606 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00002607 }
Chris Lattner00950542001-06-06 20:29:01 +00002608
Chris Lattnera8e8f162005-05-06 20:27:19 +00002609 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002610 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002611
Chris Lattner8b81bf52001-07-25 22:47:46 +00002612 // Create the call node...
Chris Lattnera8e8f162005-05-06 20:27:19 +00002613 if (!$6) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00002614 // Make sure no arguments is a good thing!
2615 if (Ty->getNumParams() != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002616 GEN_ERROR("No arguments passed to a function that "
Chris Lattnera4e25182002-07-25 20:52:56 +00002617 "expects arguments!");
2618
Chris Lattner9232b992003-04-22 18:42:41 +00002619 $$ = new CallInst(V, std::vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00002620 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00002621 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00002622 // correctly!
2623 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00002624 FunctionType::param_iterator I = Ty->param_begin();
2625 FunctionType::param_iterator E = Ty->param_end();
Chris Lattnera8e8f162005-05-06 20:27:19 +00002626 std::vector<Value*>::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00002627
2628 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
Reid Spencer3271ed52004-07-18 00:08:11 +00002629 if ((*ArgI)->getType() != *I)
Reid Spencer61c83e02006-08-18 08:43:06 +00002630 GEN_ERROR("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Reid Spencer3271ed52004-07-18 00:08:11 +00002631 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00002632
Chris Lattner8b81bf52001-07-25 22:47:46 +00002633 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002634 GEN_ERROR("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00002635
Chris Lattnera8e8f162005-05-06 20:27:19 +00002636 $$ = new CallInst(V, *$6);
Chris Lattner8b81bf52001-07-25 22:47:46 +00002637 }
Chris Lattnerddb6db42005-05-06 05:51:46 +00002638 cast<CallInst>($$)->setTailCall($1);
Chris Lattnera8e8f162005-05-06 20:27:19 +00002639 cast<CallInst>($$)->setCallingConv($2);
2640 delete $3;
2641 delete $6;
Reid Spencer61c83e02006-08-18 08:43:06 +00002642 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002643 }
2644 | MemoryInst {
2645 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002646 CHECK_FOR_ERROR
Chris Lattner51727be2002-06-04 21:58:56 +00002647 };
Chris Lattner00950542001-06-06 20:29:01 +00002648
Chris Lattner6cdb0112001-11-26 16:54:11 +00002649
2650// IndexList - List of indices for GEP based instructions...
2651IndexList : ',' ValueRefList {
Chris Lattner15c9c032003-09-08 18:20:29 +00002652 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002653 CHECK_FOR_ERROR
Chris Lattner15c9c032003-09-08 18:20:29 +00002654 } | /* empty */ {
2655 $$ = new std::vector<Value*>();
Reid Spencer61c83e02006-08-18 08:43:06 +00002656 CHECK_FOR_ERROR
Chris Lattner15c9c032003-09-08 18:20:29 +00002657 };
2658
2659OptVolatile : VOLATILE {
2660 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002661 CHECK_FOR_ERROR
Chris Lattner15c9c032003-09-08 18:20:29 +00002662 }
2663 | /* empty */ {
2664 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002665 CHECK_FOR_ERROR
Chris Lattner15c9c032003-09-08 18:20:29 +00002666 };
2667
Chris Lattner027dcc52001-07-08 21:10:27 +00002668
Chris Lattnerddb6db42005-05-06 05:51:46 +00002669
Chris Lattner66db8e42005-11-06 06:34:12 +00002670MemoryInst : MALLOC Types OptCAlign {
Chris Lattner66db8e42005-11-06 06:34:12 +00002671 $$ = new MallocInst(*$2, 0, $3);
Chris Lattner30c89792001-09-07 16:35:17 +00002672 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002673 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002674 }
Chris Lattner66db8e42005-11-06 06:34:12 +00002675 | MALLOC Types ',' UINT ValueRef OptCAlign {
Reid Spencer5b7e7532006-09-28 19:28:24 +00002676 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002677 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002678 $$ = new MallocInst(*$2, tmpVal, $6);
2679 delete $2;
Nate Begeman14b05292005-11-05 09:21:28 +00002680 }
Chris Lattner66db8e42005-11-06 06:34:12 +00002681 | ALLOCA Types OptCAlign {
Chris Lattner66db8e42005-11-06 06:34:12 +00002682 $$ = new AllocaInst(*$2, 0, $3);
Nate Begeman14b05292005-11-05 09:21:28 +00002683 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002684 CHECK_FOR_ERROR
Nate Begeman14b05292005-11-05 09:21:28 +00002685 }
Chris Lattner66db8e42005-11-06 06:34:12 +00002686 | ALLOCA Types ',' UINT ValueRef OptCAlign {
Reid Spencer5b7e7532006-09-28 19:28:24 +00002687 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002688 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002689 $$ = new AllocaInst(*$2, tmpVal, $6);
2690 delete $2;
Nate Begeman14b05292005-11-05 09:21:28 +00002691 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002692 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00002693 if (!isa<PointerType>($2->getType()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002694 GEN_ERROR("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00002695 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002696 $$ = new FreeInst($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00002697 CHECK_FOR_ERROR
Chris Lattner00950542001-06-06 20:29:01 +00002698 }
2699
Chris Lattner15c9c032003-09-08 18:20:29 +00002700 | OptVolatile LOAD Types ValueRef {
2701 if (!isa<PointerType>($3->get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002702 GEN_ERROR("Can't load from nonpointer type: " +
Reid Spencer3271ed52004-07-18 00:08:11 +00002703 (*$3)->getDescription());
Chris Lattner1c1bb332004-10-09 02:18:58 +00002704 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002705 GEN_ERROR("Can't load from pointer of non-first-class type: " +
Chris Lattner1c1bb332004-10-09 02:18:58 +00002706 (*$3)->getDescription());
Reid Spencer5b7e7532006-09-28 19:28:24 +00002707 Value* tmpVal = getVal(*$3, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002708 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002709 $$ = new LoadInst(tmpVal, "", $1);
2710 delete $3;
Chris Lattner027dcc52001-07-08 21:10:27 +00002711 }
Chris Lattner15c9c032003-09-08 18:20:29 +00002712 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
2713 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002714 if (!PT)
Reid Spencer61c83e02006-08-18 08:43:06 +00002715 GEN_ERROR("Can't store to a nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00002716 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002717 const Type *ElTy = PT->getElementType();
Chris Lattner15c9c032003-09-08 18:20:29 +00002718 if (ElTy != $3->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002719 GEN_ERROR("Can't store '" + $3->getType()->getDescription() +
Chris Lattner72e00252001-12-14 16:28:42 +00002720 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner0383cc42002-08-21 23:51:21 +00002721
Reid Spencer5b7e7532006-09-28 19:28:24 +00002722 Value* tmpVal = getVal(*$5, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002723 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002724 $$ = new StoreInst($3, tmpVal, $1);
2725 delete $5;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002726 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00002727 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00002728 if (!isa<PointerType>($2->get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002729 GEN_ERROR("getelementptr insn requires pointer operand!");
Chris Lattner830b6f92004-04-05 01:30:04 +00002730
2731 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
2732 // indices to uint struct indices for compatibility.
2733 generic_gep_type_iterator<std::vector<Value*>::iterator>
2734 GTI = gep_type_begin($2->get(), $4->begin(), $4->end()),
2735 GTE = gep_type_end($2->get(), $4->begin(), $4->end());
2736 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
2737 if (isa<StructType>(*GTI)) // Only change struct indices
Reid Spencerb83eb642006-10-20 07:07:24 +00002738 if (ConstantInt *CUI = dyn_cast<ConstantInt>((*$4)[i]))
Chris Lattner830b6f92004-04-05 01:30:04 +00002739 if (CUI->getType() == Type::UByteTy)
2740 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
2741
Chris Lattner30c89792001-09-07 16:35:17 +00002742 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Reid Spencer61c83e02006-08-18 08:43:06 +00002743 GEN_ERROR("Invalid getelementptr indices for type '" +
Chris Lattner830b6f92004-04-05 01:30:04 +00002744 (*$2)->getDescription()+ "'!");
Reid Spencer5b7e7532006-09-28 19:28:24 +00002745 Value* tmpVal = getVal(*$2, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002746 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002747 $$ = new GetElementPtrInst(tmpVal, *$4);
2748 delete $2;
2749 delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00002750 };
Chris Lattner027dcc52001-07-08 21:10:27 +00002751
Brian Gaeked0fde302003-11-11 22:41:34 +00002752
Chris Lattner00950542001-06-06 20:29:01 +00002753%%
Reid Spencer61c83e02006-08-18 08:43:06 +00002754
2755void llvm::GenerateError(const std::string &message, int LineNo) {
2756 if (LineNo == -1) LineNo = llvmAsmlineno;
2757 // TODO: column number in exception
2758 if (TheParseError)
2759 TheParseError->setError(CurFilename, message, LineNo);
2760 TriggerError = 1;
2761}
2762
Chris Lattner09083092001-07-08 04:57:15 +00002763int yyerror(const char *ErrorMsg) {
Chris Lattner9232b992003-04-22 18:42:41 +00002764 std::string where
2765 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002766 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Chris Lattner9232b992003-04-22 18:42:41 +00002767 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
Chris Lattnerbc280ab2004-03-30 20:58:25 +00002768 if (yychar == YYEMPTY || yychar == 0)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002769 errMsg += "end-of-file.";
2770 else
Chris Lattner9232b992003-04-22 18:42:41 +00002771 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Reid Spencer61c83e02006-08-18 08:43:06 +00002772 GenerateError(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00002773 return 0;
2774}