blob: 08be9f454298f564de7bcb979112d4253979250a [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files --------*- C++ -*-===//
John Criswell856ba762003-10-21 15:17:13 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This file implements the bison parser for LLVM assembly languages files.
11//
Chris Lattnercf3056d2003-10-13 03:32:08 +000012//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +000013
Chris Lattner00950542001-06-06 20:29:01 +000014%{
15#include "ParserInternals.h"
Chris Lattner00950542001-06-06 20:29:01 +000016#include "llvm/SymbolTable.h"
17#include "llvm/Module.h"
Chris Lattner00950542001-06-06 20:29:01 +000018#include "llvm/iTerminators.h"
19#include "llvm/iMemory.h"
Chris Lattner1cff96a2002-09-10 22:37:46 +000020#include "llvm/iOperators.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000021#include "llvm/iPHINode.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000022#include "Support/STLExtras.h"
Chris Lattner00950542001-06-06 20:29:01 +000023#include <list>
Chris Lattnerc188eeb2002-07-30 18:54:25 +000024#include <utility>
Chris Lattner30c89792001-09-07 16:35:17 +000025#include <algorithm>
Chris Lattner00950542001-06-06 20:29:01 +000026
Chris Lattner386a3b72001-10-16 19:54:17 +000027int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
Chris Lattner09083092001-07-08 04:57:15 +000028int yylex(); // declaration" of xxx warnings.
Chris Lattner00950542001-06-06 20:29:01 +000029int yyparse();
30
Brian Gaeked0fde302003-11-11 22:41:34 +000031namespace llvm {
32
Chris Lattner00950542001-06-06 20:29:01 +000033static Module *ParserResult;
Chris Lattner9232b992003-04-22 18:42:41 +000034std::string CurFilename;
Chris Lattner00950542001-06-06 20:29:01 +000035
Chris Lattner30c89792001-09-07 16:35:17 +000036// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
37// relating to upreferences in the input stream.
38//
39//#define DEBUG_UPREFS 1
40#ifdef DEBUG_UPREFS
Chris Lattner699f1eb2002-08-14 17:12:33 +000041#define UR_OUT(X) std::cerr << X
Chris Lattner30c89792001-09-07 16:35:17 +000042#else
43#define UR_OUT(X)
44#endif
45
Vikram S. Adved3f7eb02002-07-14 22:59:28 +000046#define YYERROR_VERBOSE 1
47
Chris Lattner99e7ab72003-10-18 05:53:13 +000048// HACK ALERT: This variable is used to implement the automatic conversion of
49// variable argument instructions from their old to new forms. When this
50// compatiblity "Feature" is removed, this should be too.
51//
52static BasicBlock *CurBB;
53static bool ObsoleteVarArgs;
54
55
Chris Lattner7e708292002-06-25 16:13:24 +000056// This contains info used when building the body of a function. It is
57// destroyed when the function is completed.
Chris Lattner00950542001-06-06 20:29:01 +000058//
Chris Lattner9232b992003-04-22 18:42:41 +000059typedef std::vector<Value *> ValueList; // Numbered defs
Chris Lattner16af11d2004-02-09 21:03:38 +000060static void ResolveDefinitions(std::map<unsigned,ValueList> &LateResolvers,
61 std::map<unsigned,ValueList> *FutureLateResolvers = 0);
Chris Lattner00950542001-06-06 20:29:01 +000062
63static struct PerModuleInfo {
64 Module *CurrentModule;
Chris Lattner16af11d2004-02-09 21:03:38 +000065 std::map<unsigned,ValueList> Values; // Module level numbered definitions
66 std::map<unsigned,ValueList> LateResolveValues;
67 std::vector<PATypeHolder> Types;
Chris Lattner9232b992003-04-22 18:42:41 +000068 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000069
Chris Lattner2079fde2001-10-13 06:41:08 +000070 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
71 // references to global values. Global values may be referenced before they
72 // are defined, and if so, the temporary object that they represent is held
Chris Lattnere9bb2df2001-12-03 22:26:30 +000073 // here. This is used for forward references of ConstantPointerRefs.
Chris Lattner2079fde2001-10-13 06:41:08 +000074 //
Chris Lattner9232b992003-04-22 18:42:41 +000075 typedef std::map<std::pair<const PointerType *,
76 ValID>, GlobalVariable*> GlobalRefsType;
Chris Lattner2079fde2001-10-13 06:41:08 +000077 GlobalRefsType GlobalRefs;
78
Chris Lattner00950542001-06-06 20:29:01 +000079 void ModuleDone() {
Chris Lattner7e708292002-06-25 16:13:24 +000080 // If we could not resolve some functions at function compilation time
81 // (calls to functions before they are defined), resolve them now... Types
82 // are resolved when the constant pool has been completely parsed.
Chris Lattner30c89792001-09-07 16:35:17 +000083 //
Chris Lattner00950542001-06-06 20:29:01 +000084 ResolveDefinitions(LateResolveValues);
85
Chris Lattner2079fde2001-10-13 06:41:08 +000086 // Check to make sure that all global value forward references have been
87 // resolved!
88 //
89 if (!GlobalRefs.empty()) {
Chris Lattner9232b992003-04-22 18:42:41 +000090 std::string UndefinedReferences = "Unresolved global references exist:\n";
Chris Lattner749ce032002-03-11 22:12:39 +000091
92 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
93 I != E; ++I) {
94 UndefinedReferences += " " + I->first.first->getDescription() + " " +
95 I->first.second.getName() + "\n";
96 }
97 ThrowException(UndefinedReferences);
Chris Lattner2079fde2001-10-13 06:41:08 +000098 }
99
Chris Lattner7e708292002-06-25 16:13:24 +0000100 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000101 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +0000102 CurrentModule = 0;
103 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000104
105
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000106 // DeclareNewGlobalValue - Called every time a new GV has been defined. This
Chris Lattner2079fde2001-10-13 06:41:08 +0000107 // is used to remove things from the forward declaration map, resolving them
108 // to the correct thing as needed.
109 //
110 void DeclareNewGlobalValue(GlobalValue *GV, ValID D) {
111 // Check to see if there is a forward reference to this global variable...
112 // if there is, eliminate it and patch the reference to use the new def'n.
Chris Lattner9232b992003-04-22 18:42:41 +0000113 GlobalRefsType::iterator I =
114 GlobalRefs.find(std::make_pair(GV->getType(), D));
Chris Lattner2079fde2001-10-13 06:41:08 +0000115
116 if (I != GlobalRefs.end()) {
117 GlobalVariable *OldGV = I->second; // Get the placeholder...
Misha Brukman5560c9d2003-08-18 14:43:39 +0000118 I->first.second.destroy(); // Free string memory if necessary
Chris Lattner2079fde2001-10-13 06:41:08 +0000119
120 // Loop over all of the uses of the GlobalValue. The only thing they are
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000121 // allowed to be is ConstantPointerRef's.
Chris Lattnerfd059242003-10-15 16:48:29 +0000122 assert(OldGV->hasOneUse() && "Only one reference should exist!");
Chris Lattner9e932bd2002-10-14 03:28:42 +0000123 User *U = OldGV->use_back(); // Must be a ConstantPointerRef...
124 ConstantPointerRef *CPR = cast<ConstantPointerRef>(U);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000125
Chris Lattner9e932bd2002-10-14 03:28:42 +0000126 // Change the const pool reference to point to the real global variable
127 // now. This should drop a use from the OldGV.
128 CPR->mutateReferences(OldGV, GV);
129 assert(OldGV->use_empty() && "All uses should be gone now!");
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000130
131 // Remove OldGV from the module...
Chris Lattner2079fde2001-10-13 06:41:08 +0000132 CurrentModule->getGlobalList().remove(OldGV);
133 delete OldGV; // Delete the old placeholder
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000134
Chris Lattner2079fde2001-10-13 06:41:08 +0000135 // Remove the map entry for the global now that it has been created...
136 GlobalRefs.erase(I);
137 }
138 }
139
Chris Lattner00950542001-06-06 20:29:01 +0000140} CurModule;
141
Chris Lattner79df7c02002-03-26 18:01:55 +0000142static struct PerFunctionInfo {
Chris Lattner7e708292002-06-25 16:13:24 +0000143 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner00950542001-06-06 20:29:01 +0000144
Chris Lattner16af11d2004-02-09 21:03:38 +0000145 std::map<unsigned,ValueList> Values; // Keep track of numbered definitions
146 std::map<unsigned,ValueList> LateResolveValues;
Chris Lattner9232b992003-04-22 18:42:41 +0000147 std::vector<PATypeHolder> Types;
148 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner15b69722003-11-12 04:40:30 +0000149 SymbolTable LocalSymtab;
Chris Lattner7e708292002-06-25 16:13:24 +0000150 bool isDeclare; // Is this function a forward declararation?
Chris Lattner00950542001-06-06 20:29:01 +0000151
Chris Lattner79df7c02002-03-26 18:01:55 +0000152 inline PerFunctionInfo() {
153 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000154 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000155 }
156
Chris Lattner79df7c02002-03-26 18:01:55 +0000157 inline void FunctionStart(Function *M) {
158 CurrentFunction = M;
Chris Lattner00950542001-06-06 20:29:01 +0000159 }
160
Chris Lattner79df7c02002-03-26 18:01:55 +0000161 void FunctionDone() {
Chris Lattner00950542001-06-06 20:29:01 +0000162 // If we could not resolve some blocks at parsing time (forward branches)
163 // resolve the branches now...
Chris Lattner386a3b72001-10-16 19:54:17 +0000164 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000165
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000166 // Make sure to resolve any constant expr references that might exist within
167 // the function we just declared itself.
168 ValID FID;
169 if (CurrentFunction->hasName()) {
170 FID = ValID::create((char*)CurrentFunction->getName().c_str());
171 } else {
172 unsigned Slot = CurrentFunction->getType()->getUniqueID();
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000173 // Figure out which slot number if is...
Chris Lattner16af11d2004-02-09 21:03:38 +0000174 ValueList &List = CurModule.Values[Slot];
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000175 for (unsigned i = 0; ; ++i) {
Chris Lattner16af11d2004-02-09 21:03:38 +0000176 assert(i < List.size() && "Function not found!");
177 if (List[i] == CurrentFunction) {
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000178 FID = ValID::create((int)i);
179 break;
180 }
181 }
182 }
183 CurModule.DeclareNewGlobalValue(CurrentFunction, FID);
184
Chris Lattner7e708292002-06-25 16:13:24 +0000185 Values.clear(); // Clear out function local definitions
Chris Lattner15b69722003-11-12 04:40:30 +0000186 Types.clear(); // Clear out function local types
187 LocalSymtab.clear(); // Clear out function local symbol table
Chris Lattner79df7c02002-03-26 18:01:55 +0000188 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000189 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000190 }
Chris Lattner394f2eb2003-10-16 20:12:13 +0000191} CurFun; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000192
Chris Lattner394f2eb2003-10-16 20:12:13 +0000193static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000194
Chris Lattner00950542001-06-06 20:29:01 +0000195
196//===----------------------------------------------------------------------===//
197// Code to handle definitions of all the types
198//===----------------------------------------------------------------------===//
199
Chris Lattner9232b992003-04-22 18:42:41 +0000200static int InsertValue(Value *D,
Chris Lattner16af11d2004-02-09 21:03:38 +0000201 std::map<unsigned,ValueList> &ValueTab = CurFun.Values) {
Chris Lattner2079fde2001-10-13 06:41:08 +0000202 if (D->hasName()) return -1; // Is this a numbered definition?
203
204 // Yes, insert the value into the value table...
205 unsigned type = D->getType()->getUniqueID();
Chris Lattner2079fde2001-10-13 06:41:08 +0000206 //printf("Values[%d][%d] = %d\n", type, ValueTab[type].size(), D);
Chris Lattner16af11d2004-02-09 21:03:38 +0000207 ValueList &List = ValueTab[type];
208 List.push_back(D);
209 return List.size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000210}
211
Chris Lattner30c89792001-09-07 16:35:17 +0000212// TODO: FIXME when Type are not const
Chris Lattner9232b992003-04-22 18:42:41 +0000213static void InsertType(const Type *Ty, std::vector<PATypeHolder> &Types) {
Chris Lattner30c89792001-09-07 16:35:17 +0000214 Types.push_back(Ty);
215}
216
217static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000218 switch (D.Type) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000219 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000220 unsigned Num = (unsigned)D.Num;
221
222 // Module constants occupy the lowest numbered slots...
223 if (Num < CurModule.Types.size())
224 return CurModule.Types[Num];
225
226 Num -= CurModule.Types.size();
227
228 // Check that the number is within bounds...
Chris Lattner394f2eb2003-10-16 20:12:13 +0000229 if (Num <= CurFun.Types.size())
230 return CurFun.Types[Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000231 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000232 }
Chris Lattnerf8dff732002-07-18 05:18:37 +0000233 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000234 std::string Name(D.Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000235 SymbolTable *SymTab = 0;
Chris Lattner6e6026b2002-11-20 18:36:02 +0000236 Value *N = 0;
237 if (inFunctionScope()) {
Chris Lattner394f2eb2003-10-16 20:12:13 +0000238 SymTab = &CurFun.CurrentFunction->getSymbolTable();
Chris Lattner6e6026b2002-11-20 18:36:02 +0000239 N = SymTab->lookup(Type::TypeTy, Name);
240 }
Chris Lattner30c89792001-09-07 16:35:17 +0000241
242 if (N == 0) {
Chris Lattner7e708292002-06-25 16:13:24 +0000243 // Symbol table doesn't automatically chain yet... because the function
Chris Lattner30c89792001-09-07 16:35:17 +0000244 // hasn't been added to the module...
245 //
Chris Lattner6e6026b2002-11-20 18:36:02 +0000246 SymTab = &CurModule.CurrentModule->getSymbolTable();
247 N = SymTab->lookup(Type::TypeTy, Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000248 if (N == 0) break;
249 }
250
251 D.destroy(); // Free old strdup'd memory...
Chris Lattner949a3622003-07-23 15:30:06 +0000252 return cast<Type>(N);
Chris Lattner30c89792001-09-07 16:35:17 +0000253 }
254 default:
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000255 ThrowException("Internal parser error: Invalid symbol type reference!");
Chris Lattner30c89792001-09-07 16:35:17 +0000256 }
257
258 // If we reached here, we referenced either a symbol that we don't know about
259 // or an id number that hasn't been read yet. We may be referencing something
260 // forward, so just create an entry to be resolved later and get to it...
261 //
262 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
263
Chris Lattner9232b992003-04-22 18:42:41 +0000264 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000265 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
Chris Lattner4a42e902001-10-22 05:56:09 +0000266
Chris Lattner9232b992003-04-22 18:42:41 +0000267 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000268 if (I != LateResolver.end()) {
269 return I->second;
270 }
Chris Lattner30c89792001-09-07 16:35:17 +0000271
Chris Lattner82269592001-10-22 06:01:08 +0000272 Type *Typ = OpaqueType::get();
Chris Lattner9232b992003-04-22 18:42:41 +0000273 LateResolver.insert(std::make_pair(D, Typ));
Chris Lattner30c89792001-09-07 16:35:17 +0000274 return Typ;
275}
276
Chris Lattner9232b992003-04-22 18:42:41 +0000277static Value *lookupInSymbolTable(const Type *Ty, const std::string &Name) {
Chris Lattner6e6026b2002-11-20 18:36:02 +0000278 SymbolTable &SymTab =
Chris Lattner394f2eb2003-10-16 20:12:13 +0000279 inFunctionScope() ? CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner9705a152002-05-02 19:27:42 +0000280 CurModule.CurrentModule->getSymbolTable();
Chris Lattner6e6026b2002-11-20 18:36:02 +0000281 return SymTab.lookup(Ty, Name);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000282}
283
Chris Lattner2079fde2001-10-13 06:41:08 +0000284// getValNonImprovising - Look up the value specified by the provided type and
285// the provided ValID. If the value exists and has already been defined, return
286// it. Otherwise return null.
287//
288static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000289 if (isa<FunctionType>(Ty))
290 ThrowException("Functions are not values and "
291 "must be referenced as pointers");
Chris Lattner386a3b72001-10-16 19:54:17 +0000292
Chris Lattner30c89792001-09-07 16:35:17 +0000293 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000294 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000295 unsigned type = Ty->getUniqueID();
Chris Lattner00950542001-06-06 20:29:01 +0000296 unsigned Num = (unsigned)D.Num;
297
298 // Module constants occupy the lowest numbered slots...
Chris Lattner16af11d2004-02-09 21:03:38 +0000299 std::map<unsigned,ValueList>::iterator VI = CurModule.Values.find(type);
300 if (VI != CurModule.Values.end()) {
301 if (Num < VI->second.size())
302 return VI->second[Num];
303 Num -= VI->second.size();
Chris Lattner00950542001-06-06 20:29:01 +0000304 }
305
306 // Make sure that our type is within bounds
Chris Lattner16af11d2004-02-09 21:03:38 +0000307 VI = CurFun.Values.find(type);
308 if (VI == CurFun.Values.end()) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000309
310 // Check that the number is within bounds...
Chris Lattner16af11d2004-02-09 21:03:38 +0000311 if (VI->second.size() <= Num) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000312
Chris Lattner16af11d2004-02-09 21:03:38 +0000313 return VI->second[Num];
Chris Lattner00950542001-06-06 20:29:01 +0000314 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000315
Chris Lattner1a1cb112001-09-30 22:46:54 +0000316 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000317 Value *N = lookupInSymbolTable(Ty, std::string(D.Name));
Chris Lattner2079fde2001-10-13 06:41:08 +0000318 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000319
320 D.destroy(); // Free old strdup'd memory...
321 return N;
322 }
323
Chris Lattner2079fde2001-10-13 06:41:08 +0000324 // Check to make sure that "Ty" is an integral type, and that our
325 // value will fit into the specified type...
326 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattnerd78700d2002-08-16 21:14:40 +0000327 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
328 ThrowException("Signed integral constant '" +
329 itostr(D.ConstPool64) + "' is invalid for type '" +
330 Ty->getDescription() + "'!");
331 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000332
333 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000334 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
335 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000336 ThrowException("Integral constant '" + utostr(D.UConstPool64) +
337 "' is invalid or out of range!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000338 } else { // This is really a signed reference. Transmogrify.
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000339 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000340 }
341 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000342 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000343 }
344
Chris Lattner2079fde2001-10-13 06:41:08 +0000345 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000346 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner2079fde2001-10-13 06:41:08 +0000347 ThrowException("FP constant invalid for type!!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000348 return ConstantFP::get(Ty, D.ConstPoolFP);
Chris Lattner2079fde2001-10-13 06:41:08 +0000349
350 case ValID::ConstNullVal: // Is it a null value?
Chris Lattner9b625032002-05-06 16:15:30 +0000351 if (!isa<PointerType>(Ty))
Chris Lattner2079fde2001-10-13 06:41:08 +0000352 ThrowException("Cannot create a a non pointer null!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000353 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner2079fde2001-10-13 06:41:08 +0000354
Chris Lattnerd78700d2002-08-16 21:14:40 +0000355 case ValID::ConstantVal: // Fully resolved constant?
356 if (D.ConstantValue->getType() != Ty)
357 ThrowException("Constant expression type different from required type!");
358 return D.ConstantValue;
359
Chris Lattner30c89792001-09-07 16:35:17 +0000360 default:
361 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000362 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000363 } // End of switch
364
Chris Lattner2079fde2001-10-13 06:41:08 +0000365 assert(0 && "Unhandled case!");
366 return 0;
367}
368
369
370// getVal - This function is identical to getValNonImprovising, except that if a
371// value is not already defined, it "improvises" by creating a placeholder var
372// that looks and acts just like the requested variable. When the value is
373// defined later, all uses of the placeholder variable are replaced with the
374// real thing.
375//
376static Value *getVal(const Type *Ty, const ValID &D) {
377 assert(Ty != Type::TypeTy && "Should use getTypeVal for types!");
378
379 // See if the value has already been defined...
380 Value *V = getValNonImprovising(Ty, D);
381 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000382
383 // If we reached here, we referenced either a symbol that we don't know about
384 // or an id number that hasn't been read yet. We may be referencing something
385 // forward, so just create an entry to be resolved later and get to it...
386 //
Chris Lattner00950542001-06-06 20:29:01 +0000387 Value *d = 0;
Chris Lattner30c89792001-09-07 16:35:17 +0000388 switch (Ty->getPrimitiveID()) {
389 case Type::LabelTyID: d = new BBPlaceHolder(Ty, D); break;
Chris Lattner30c89792001-09-07 16:35:17 +0000390 default: d = new ValuePlaceHolder(Ty, D); break;
Chris Lattner00950542001-06-06 20:29:01 +0000391 }
392
393 assert(d != 0 && "How did we not make something?");
Chris Lattner79df7c02002-03-26 18:01:55 +0000394 if (inFunctionScope())
Chris Lattner394f2eb2003-10-16 20:12:13 +0000395 InsertValue(d, CurFun.LateResolveValues);
Chris Lattner386a3b72001-10-16 19:54:17 +0000396 else
397 InsertValue(d, CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000398 return d;
399}
400
401
402//===----------------------------------------------------------------------===//
403// Code to handle forward references in instructions
404//===----------------------------------------------------------------------===//
405//
406// This code handles the late binding needed with statements that reference
407// values not defined yet... for example, a forward branch, or the PHI node for
408// a loop body.
409//
Chris Lattner394f2eb2003-10-16 20:12:13 +0000410// This keeps a table (CurFun.LateResolveValues) of all such forward references
Chris Lattner00950542001-06-06 20:29:01 +0000411// and back patchs after we are done.
412//
413
414// ResolveDefinitions - If we could not resolve some defs at parsing
415// time (forward branches, phi functions for loops, etc...) resolve the
416// defs now...
417//
Chris Lattner16af11d2004-02-09 21:03:38 +0000418static void ResolveDefinitions(std::map<unsigned,ValueList> &LateResolvers,
419 std::map<unsigned,ValueList> *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000420 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Chris Lattner16af11d2004-02-09 21:03:38 +0000421 for (std::map<unsigned,ValueList>::iterator LRI = LateResolvers.begin(),
422 E = LateResolvers.end(); LRI != E; ++LRI) {
423 ValueList &List = LRI->second;
424 while (!List.empty()) {
425 Value *V = List.back();
426 List.pop_back();
Chris Lattner386a3b72001-10-16 19:54:17 +0000427 assert(!isa<Type>(V) && "Types should be in LateResolveTypes!");
Chris Lattner00950542001-06-06 20:29:01 +0000428 ValID &DID = getValIDFromPlaceHolder(V);
429
Chris Lattner16af11d2004-02-09 21:03:38 +0000430 Value *TheRealValue =
431 getValNonImprovising(Type::getUniqueIDType(LRI->first), DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000432 if (TheRealValue) {
433 V->replaceAllUsesWith(TheRealValue);
434 delete V;
435 } else if (FutureLateResolvers) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000436 // Functions have their unresolved items forwarded to the module late
Chris Lattner386a3b72001-10-16 19:54:17 +0000437 // resolver table
438 InsertValue(V, *FutureLateResolvers);
439 } else {
Chris Lattner9705a152002-05-02 19:27:42 +0000440 if (DID.Type == ValID::NameVal)
Chris Lattner30c89792001-09-07 16:35:17 +0000441 ThrowException("Reference to an invalid definition: '" +DID.getName()+
442 "' of type '" + V->getType()->getDescription() + "'",
443 getLineNumFromPlaceHolder(V));
444 else
445 ThrowException("Reference to an invalid definition: #" +
446 itostr(DID.Num) + " of type '" +
447 V->getType()->getDescription() + "'",
448 getLineNumFromPlaceHolder(V));
449 }
Chris Lattner00950542001-06-06 20:29:01 +0000450 }
451 }
452
453 LateResolvers.clear();
454}
455
Chris Lattner4a42e902001-10-22 05:56:09 +0000456// ResolveTypeTo - A brand new type was just declared. This means that (if
457// name is not null) things referencing Name can be resolved. Otherwise, things
458// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000459//
Chris Lattner4a42e902001-10-22 05:56:09 +0000460static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner9232b992003-04-22 18:42:41 +0000461 std::vector<PATypeHolder> &Types = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000462 CurFun.Types : CurModule.Types;
Chris Lattner00950542001-06-06 20:29:01 +0000463
Chris Lattner4a42e902001-10-22 05:56:09 +0000464 ValID D;
465 if (Name) D = ValID::create(Name);
466 else D = ValID::create((int)Types.size());
Chris Lattner30c89792001-09-07 16:35:17 +0000467
Chris Lattner9232b992003-04-22 18:42:41 +0000468 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000469 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
Chris Lattner4a42e902001-10-22 05:56:09 +0000470
Chris Lattner9232b992003-04-22 18:42:41 +0000471 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000472 if (I != LateResolver.end()) {
Chris Lattner51727be2002-06-04 21:58:56 +0000473 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner4a42e902001-10-22 05:56:09 +0000474 LateResolver.erase(I);
475 }
476}
477
478// ResolveTypes - At this point, all types should be resolved. Any that aren't
479// are errors.
480//
Chris Lattner9232b992003-04-22 18:42:41 +0000481static void ResolveTypes(std::map<ValID, PATypeHolder> &LateResolveTypes) {
Chris Lattner4a42e902001-10-22 05:56:09 +0000482 if (!LateResolveTypes.empty()) {
Chris Lattner82269592001-10-22 06:01:08 +0000483 const ValID &DID = LateResolveTypes.begin()->first;
Chris Lattner4a42e902001-10-22 05:56:09 +0000484
485 if (DID.Type == ValID::NameVal)
Chris Lattner82269592001-10-22 06:01:08 +0000486 ThrowException("Reference to an invalid type: '" +DID.getName() + "'");
Chris Lattner4a42e902001-10-22 05:56:09 +0000487 else
Chris Lattner82269592001-10-22 06:01:08 +0000488 ThrowException("Reference to an invalid type: #" + itostr(DID.Num));
Chris Lattner30c89792001-09-07 16:35:17 +0000489 }
490}
491
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000492
Chris Lattner1781aca2001-09-18 04:00:54 +0000493// setValueName - Set the specified value to the name given. The name may be
494// null potentially, in which case this is a noop. The string passed in is
495// assumed to be a malloc'd string buffer, and is freed by this function.
496//
Chris Lattnerb7474512001-10-03 15:39:04 +0000497// This function returns true if the value has already been defined, but is
498// allowed to be redefined in the specified context. If the name is a new name
499// for the typeplane, false is returned.
500//
501static bool setValueName(Value *V, char *NameStr) {
502 if (NameStr == 0) return false;
Chris Lattner386a3b72001-10-16 19:54:17 +0000503
Chris Lattner9232b992003-04-22 18:42:41 +0000504 std::string Name(NameStr); // Copy string
Chris Lattner1781aca2001-09-18 04:00:54 +0000505 free(NameStr); // Free old string
506
Chris Lattner2079fde2001-10-13 06:41:08 +0000507 if (V->getType() == Type::VoidTy)
508 ThrowException("Can't assign name '" + Name +
509 "' to a null valued instruction!");
510
Chris Lattner6e6026b2002-11-20 18:36:02 +0000511 SymbolTable &ST = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000512 CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner6e6026b2002-11-20 18:36:02 +0000513 CurModule.CurrentModule->getSymbolTable();
Chris Lattner30c89792001-09-07 16:35:17 +0000514
Chris Lattner6e6026b2002-11-20 18:36:02 +0000515 Value *Existing = ST.lookup(V->getType(), Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000516 if (Existing) { // Inserting a name that is already defined???
517 // There is only one case where this is allowed: when we are refining an
518 // opaque type. In this case, Existing will be an opaque type.
Chris Lattner949a3622003-07-23 15:30:06 +0000519 if (const Type *Ty = dyn_cast<Type>(Existing)) {
Chris Lattner51727be2002-06-04 21:58:56 +0000520 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Ty)) {
Chris Lattner30c89792001-09-07 16:35:17 +0000521 // We ARE replacing an opaque type!
Chris Lattner51727be2002-06-04 21:58:56 +0000522 ((OpaqueType*)OpTy)->refineAbstractTypeTo(cast<Type>(V));
Chris Lattnerb7474512001-10-03 15:39:04 +0000523 return true;
Chris Lattner30c89792001-09-07 16:35:17 +0000524 }
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000525 }
Chris Lattner30c89792001-09-07 16:35:17 +0000526
Chris Lattner9636a912001-10-01 16:18:37 +0000527 // Otherwise, we are a simple redefinition of a value, check to see if it
528 // is defined the same as the old one...
Chris Lattneradf99702003-03-03 23:28:55 +0000529 if (const Type *Ty = dyn_cast<Type>(Existing)) {
530 if (Ty == cast<Type>(V)) return true; // Yes, it's equal.
Chris Lattner699f1eb2002-08-14 17:12:33 +0000531 // std::cerr << "Type: " << Ty->getDescription() << " != "
Chris Lattner949a3622003-07-23 15:30:06 +0000532 // << cast<Type>(V)->getDescription() << "!\n";
Chris Lattneradf99702003-03-03 23:28:55 +0000533 } else if (const Constant *C = dyn_cast<Constant>(Existing)) {
534 if (C == V) return true; // Constants are equal to themselves
Chris Lattnerb7474512001-10-03 15:39:04 +0000535 } else if (GlobalVariable *EGV = dyn_cast<GlobalVariable>(Existing)) {
Chris Lattner43efcbf2001-10-03 19:35:57 +0000536 // We are allowed to redefine a global variable in two circumstances:
537 // 1. If at least one of the globals is uninitialized or
538 // 2. If both initializers have the same value.
539 //
Chris Lattner89219832001-10-03 19:35:04 +0000540 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000541 if (!EGV->hasInitializer() || !GV->hasInitializer() ||
542 EGV->getInitializer() == GV->getInitializer()) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000543
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000544 // Make sure the existing global version gets the initializer! Make
545 // sure that it also gets marked const if the new version is.
Chris Lattner89219832001-10-03 19:35:04 +0000546 if (GV->hasInitializer() && !EGV->hasInitializer())
547 EGV->setInitializer(GV->getInitializer());
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000548 if (GV->isConstant())
549 EGV->setConstant(true);
Chris Lattner4ad02e72003-04-16 20:28:45 +0000550 EGV->setLinkage(GV->getLinkage());
Chris Lattner89219832001-10-03 19:35:04 +0000551
Chris Lattner2079fde2001-10-13 06:41:08 +0000552 delete GV; // Destroy the duplicate!
Chris Lattner89219832001-10-03 19:35:04 +0000553 return true; // They are equivalent!
554 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000555 }
Chris Lattner9636a912001-10-01 16:18:37 +0000556 }
Chris Lattner0b268fb2003-11-25 03:54:16 +0000557
Chris Lattner2079fde2001-10-13 06:41:08 +0000558 ThrowException("Redefinition of value named '" + Name + "' in the '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000559 V->getType()->getDescription() + "' type plane!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000560 }
Chris Lattner00950542001-06-06 20:29:01 +0000561
Chris Lattner15b69722003-11-12 04:40:30 +0000562 // Set the name
Chris Lattner6e6026b2002-11-20 18:36:02 +0000563 V->setName(Name, &ST);
Chris Lattner15b69722003-11-12 04:40:30 +0000564
565 // If we're in function scope
566 if (inFunctionScope()) {
567 // Look up the symbol in the function's local symboltable
568 Existing = CurFun.LocalSymtab.lookup(V->getType(),Name);
569
570 // If it already exists
571 if (Existing) {
Chris Lattner15b69722003-11-12 04:40:30 +0000572 // Bail
573 ThrowException("Redefinition of value named '" + Name + "' in the '" +
574 V->getType()->getDescription() + "' type plane!");
575
576 // otherwise, since it doesn't exist
577 } else {
578 // Insert it.
579 CurFun.LocalSymtab.insert(V);
580 }
581 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000582 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000583}
584
Chris Lattner8896eda2001-07-09 19:38:36 +0000585
Chris Lattner30c89792001-09-07 16:35:17 +0000586//===----------------------------------------------------------------------===//
587// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000588//
Chris Lattner8896eda2001-07-09 19:38:36 +0000589
Chris Lattner3b073862004-02-09 03:19:29 +0000590// TypeContains - Returns true if Ty directly contains E in it.
Chris Lattner30c89792001-09-07 16:35:17 +0000591//
592static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3b073862004-02-09 03:19:29 +0000593 return find(Ty->subtype_begin(), Ty->subtype_end(), E) != Ty->subtype_end();
594}
595
596namespace {
597 struct UpRefRecord {
598 // NestingLevel - The number of nesting levels that need to be popped before
599 // this type is resolved.
600 unsigned NestingLevel;
601
602 // LastContainedTy - This is the type at the current binding level for the
603 // type. Every time we reduce the nesting level, this gets updated.
604 const Type *LastContainedTy;
605
606 // UpRefTy - This is the actual opaque type that the upreference is
607 // represented with.
608 OpaqueType *UpRefTy;
609
610 UpRefRecord(unsigned NL, OpaqueType *URTy)
611 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
612 };
Chris Lattner30c89792001-09-07 16:35:17 +0000613}
Chris Lattner698b56e2001-07-20 19:15:08 +0000614
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000615// UpRefs - A list of the outstanding upreferences that need to be resolved.
Chris Lattner3b073862004-02-09 03:19:29 +0000616static std::vector<UpRefRecord> UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000617
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000618/// HandleUpRefs - Every time we finish a new layer of types, this function is
619/// called. It loops through the UpRefs vector, which is a list of the
620/// currently active types. For each type, if the up reference is contained in
621/// the newly completed type, we decrement the level count. When the level
622/// count reaches zero, the upreferenced type is the type that is passed in:
623/// thus we can complete the cycle.
624///
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000625static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner2c37c182004-02-09 03:03:10 +0000626 if (!ty->isAbstract()) return ty;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000627 PATypeHolder Ty(ty);
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000628 UR_OUT("Type '" << Ty->getDescription() <<
Chris Lattner5084d032001-11-02 07:46:26 +0000629 "' newly formed. Resolving upreferences.\n" <<
630 UpRefs.size() << " upreferences active!\n");
Chris Lattner026a8ce2004-02-09 18:53:54 +0000631
632 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
633 // to zero), we resolve them all together before we resolve them to Ty. At
634 // the end of the loop, if there is anything to resolve to Ty, it will be in
635 // this variable.
636 OpaqueType *TypeToResolve = 0;
637
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000638 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Chris Lattner5084d032001-11-02 07:46:26 +0000639 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Chris Lattner30c89792001-09-07 16:35:17 +0000640 << UpRefs[i].second->getDescription() << ") = "
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000641 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner3b073862004-02-09 03:19:29 +0000642 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
643 // Decrement level of upreference
644 unsigned Level = --UpRefs[i].NestingLevel;
645 UpRefs[i].LastContainedTy = Ty;
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000646 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Chris Lattner30c89792001-09-07 16:35:17 +0000647 if (Level == 0) { // Upreference should be resolved!
Chris Lattner026a8ce2004-02-09 18:53:54 +0000648 if (!TypeToResolve) {
649 TypeToResolve = UpRefs[i].UpRefTy;
650 } else {
651 UR_OUT(" * Resolving upreference for "
652 << UpRefs[i].second->getDescription() << "\n";
653 std::string OldName = UpRefs[i].UpRefTy->getDescription());
654 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
655 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
656 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
657 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000658 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
659 --i; // Do not skip the next element...
Chris Lattner30c89792001-09-07 16:35:17 +0000660 }
661 }
Chris Lattner8896eda2001-07-09 19:38:36 +0000662 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000663
Chris Lattner026a8ce2004-02-09 18:53:54 +0000664 if (TypeToResolve) {
665 UR_OUT(" * Resolving upreference for "
666 << UpRefs[i].second->getDescription() << "\n";
Chris Lattner16af11d2004-02-09 21:03:38 +0000667 std::string OldName = TypeToResolve->getDescription());
Chris Lattner026a8ce2004-02-09 18:53:54 +0000668 TypeToResolve->refineAbstractTypeTo(Ty);
669 }
670
Chris Lattner8896eda2001-07-09 19:38:36 +0000671 return Ty;
672}
673
Chris Lattner30c89792001-09-07 16:35:17 +0000674
Chris Lattner00950542001-06-06 20:29:01 +0000675//===----------------------------------------------------------------------===//
676// RunVMAsmParser - Define an interface to this parser
677//===----------------------------------------------------------------------===//
678//
Chris Lattner9232b992003-04-22 18:42:41 +0000679Module *RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000680 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000681 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000682 llvmAsmlineno = 1; // Reset the current line number...
Chris Lattner99e7ab72003-10-18 05:53:13 +0000683 ObsoleteVarArgs = false;
Chris Lattner00950542001-06-06 20:29:01 +0000684
Chris Lattner75f20532003-04-22 18:02:52 +0000685 // Allocate a new module to read
686 CurModule.CurrentModule = new Module(Filename);
Chris Lattner42570982003-11-26 07:24:58 +0000687
688 try {
689 yyparse(); // Parse the file.
690 } catch (...) {
691 // Clear the symbol table so it doesn't complain when it
692 // gets destructed
693 CurFun.LocalSymtab.clear();
694 throw;
695 }
Chris Lattner99e7ab72003-10-18 05:53:13 +0000696
Chris Lattner00950542001-06-06 20:29:01 +0000697 Module *Result = ParserResult;
Chris Lattner99e7ab72003-10-18 05:53:13 +0000698
699 // Check to see if they called va_start but not va_arg..
700 if (!ObsoleteVarArgs)
701 if (Function *F = Result->getNamedFunction("llvm.va_start"))
702 if (F->asize() == 1) {
703 std::cerr << "WARNING: this file uses obsolete features. "
704 << "Assemble and disassemble to update it.\n";
705 ObsoleteVarArgs = true;
706 }
707
708
709 if (ObsoleteVarArgs) {
710 // If the user is making use of obsolete varargs intrinsics, adjust them for
711 // the user.
712 if (Function *F = Result->getNamedFunction("llvm.va_start")) {
713 assert(F->asize() == 1 && "Obsolete va_start takes 1 argument!");
714
715 const Type *RetTy = F->getFunctionType()->getParamType(0);
716 RetTy = cast<PointerType>(RetTy)->getElementType();
717 Function *NF = Result->getOrInsertFunction("llvm.va_start", RetTy, 0);
718
719 while (!F->use_empty()) {
720 CallInst *CI = cast<CallInst>(F->use_back());
721 Value *V = new CallInst(NF, "", CI);
722 new StoreInst(V, CI->getOperand(1), CI);
723 CI->getParent()->getInstList().erase(CI);
724 }
725 Result->getFunctionList().erase(F);
726 }
727
728 if (Function *F = Result->getNamedFunction("llvm.va_end")) {
729 assert(F->asize() == 1 && "Obsolete va_end takes 1 argument!");
730 const Type *ArgTy = F->getFunctionType()->getParamType(0);
731 ArgTy = cast<PointerType>(ArgTy)->getElementType();
732 Function *NF = Result->getOrInsertFunction("llvm.va_end", Type::VoidTy,
733 ArgTy, 0);
734
735 while (!F->use_empty()) {
736 CallInst *CI = cast<CallInst>(F->use_back());
737 Value *V = new LoadInst(CI->getOperand(1), "", CI);
738 new CallInst(NF, V, "", CI);
739 CI->getParent()->getInstList().erase(CI);
740 }
741 Result->getFunctionList().erase(F);
742 }
743
744 if (Function *F = Result->getNamedFunction("llvm.va_copy")) {
745 assert(F->asize() == 2 && "Obsolete va_copy takes 2 argument!");
746 const Type *ArgTy = F->getFunctionType()->getParamType(0);
747 ArgTy = cast<PointerType>(ArgTy)->getElementType();
748 Function *NF = Result->getOrInsertFunction("llvm.va_copy", ArgTy,
749 ArgTy, 0);
750
751 while (!F->use_empty()) {
752 CallInst *CI = cast<CallInst>(F->use_back());
753 Value *V = new CallInst(NF, CI->getOperand(2), "", CI);
754 new StoreInst(V, CI->getOperand(1), CI);
755 CI->getParent()->getInstList().erase(CI);
756 }
757 Result->getFunctionList().erase(F);
758 }
759 }
760
Chris Lattner00950542001-06-06 20:29:01 +0000761 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
762 ParserResult = 0;
763
764 return Result;
765}
766
Brian Gaeked0fde302003-11-11 22:41:34 +0000767} // End llvm namespace
768
769using namespace llvm;
770
Chris Lattner00950542001-06-06 20:29:01 +0000771%}
772
773%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000774 llvm::Module *ModuleVal;
775 llvm::Function *FunctionVal;
776 std::pair<llvm::PATypeHolder*, char*> *ArgVal;
777 llvm::BasicBlock *BasicBlockVal;
778 llvm::TerminatorInst *TermInstVal;
779 llvm::Instruction *InstVal;
780 llvm::Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000781
Brian Gaeked0fde302003-11-11 22:41:34 +0000782 const llvm::Type *PrimType;
783 llvm::PATypeHolder *TypeVal;
784 llvm::Value *ValueVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000785
Brian Gaeked0fde302003-11-11 22:41:34 +0000786 std::vector<std::pair<llvm::PATypeHolder*,char*> > *ArgList;
787 std::vector<llvm::Value*> *ValueList;
788 std::list<llvm::PATypeHolder> *TypeList;
789 std::list<std::pair<llvm::Value*,
790 llvm::BasicBlock*> > *PHIList; // Represent the RHS of PHI node
791 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
792 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000793
Brian Gaeked0fde302003-11-11 22:41:34 +0000794 llvm::GlobalValue::LinkageTypes Linkage;
Chris Lattner30c89792001-09-07 16:35:17 +0000795 int64_t SInt64Val;
796 uint64_t UInt64Val;
797 int SIntVal;
798 unsigned UIntVal;
799 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000800 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000801
Chris Lattner30c89792001-09-07 16:35:17 +0000802 char *StrVal; // This memory is strdup'd!
Brian Gaeked0fde302003-11-11 22:41:34 +0000803 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000804
Brian Gaeked0fde302003-11-11 22:41:34 +0000805 llvm::Instruction::BinaryOps BinaryOpVal;
806 llvm::Instruction::TermOps TermOpVal;
807 llvm::Instruction::MemoryOps MemOpVal;
808 llvm::Instruction::OtherOps OtherOpVal;
809 llvm::Module::Endianness Endianness;
Chris Lattner00950542001-06-06 20:29:01 +0000810}
811
Chris Lattner79df7c02002-03-26 18:01:55 +0000812%type <ModuleVal> Module FunctionList
813%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000814%type <BasicBlockVal> BasicBlock InstructionList
815%type <TermInstVal> BBTerminatorInst
816%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000817%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000818%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000819%type <ArgList> ArgList ArgListH
820%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000821%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000822%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000823%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000824%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000825%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +0000826%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner15c9c032003-09-08 18:20:29 +0000827%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattner4ad02e72003-04-16 20:28:45 +0000828%type <Linkage> OptLinkage
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000829%type <Endianness> BigOrLittle
Chris Lattner00950542001-06-06 20:29:01 +0000830
Chris Lattner2079fde2001-10-13 06:41:08 +0000831// ValueRef - Unresolved reference to a definition or BB
832%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000833%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000834// Tokens and types for handling constant integer values
835//
836// ESINT64VAL - A negative number within long long range
837%token <SInt64Val> ESINT64VAL
838
839// EUINT64VAL - A positive number within uns. long long range
840%token <UInt64Val> EUINT64VAL
841%type <SInt64Val> EINT64VAL
842
843%token <SIntVal> SINTVAL // Signed 32 bit ints...
844%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
845%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000846%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000847
848// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000849%type <TypeVal> Types TypesV UpRTypes UpRTypesV
850%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000851%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
852%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000853
Chris Lattner6c23f572003-08-22 05:42:10 +0000854%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
855%type <StrVal> Name OptName OptAssign
Chris Lattner00950542001-06-06 20:29:01 +0000856
857
Chris Lattnerf4600482003-06-28 20:01:34 +0000858%token IMPLEMENTATION ZEROINITIALIZER TRUE FALSE BEGINTOK ENDTOK
Chris Lattner15c9c032003-09-08 18:20:29 +0000859%token DECLARE GLOBAL CONSTANT VOLATILE
Chris Lattnera58e3a12004-02-08 21:48:25 +0000860%token TO DOTDOTDOT NULL_TOK CONST INTERNAL LINKONCE WEAK APPENDING
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000861%token OPAQUE NOT EXTERNAL TARGET ENDIAN POINTERSIZE LITTLE BIG
Chris Lattner00950542001-06-06 20:29:01 +0000862
863// Basic Block Terminating Operators
Chris Lattner36143fc2003-09-08 18:54:55 +0000864%token <TermOpVal> RET BR SWITCH INVOKE UNWIND
Chris Lattner00950542001-06-06 20:29:01 +0000865
Chris Lattner00950542001-06-06 20:29:01 +0000866// Binary Operators
867%type <BinaryOpVal> BinaryOps // all the binary operators
Chris Lattner4a6482b2002-09-10 19:57:26 +0000868%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattner42c9e772001-10-20 09:32:59 +0000869%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000870%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000871
872// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000873%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000874
Chris Lattner027dcc52001-07-08 21:10:27 +0000875// Other Operators
876%type <OtherOpVal> ShiftOps
Chris Lattner3b237fc2003-10-19 21:34:28 +0000877%token <OtherOpVal> PHI_TOK CALL CAST SHL SHR VAARG VANEXT
Chris Lattner99e7ab72003-10-18 05:53:13 +0000878%token VA_ARG // FIXME: OBSOLETE
Chris Lattner027dcc52001-07-08 21:10:27 +0000879
Chris Lattner00950542001-06-06 20:29:01 +0000880%start Module
881%%
882
883// Handle constant integer size restriction and conversion...
884//
Chris Lattner51727be2002-06-04 21:58:56 +0000885INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +0000886INTVAL : UINTVAL {
887 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
888 ThrowException("Value too large for type!");
889 $$ = (int32_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000890};
Chris Lattner00950542001-06-06 20:29:01 +0000891
892
Chris Lattner51727be2002-06-04 21:58:56 +0000893EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +0000894EINT64VAL : EUINT64VAL {
895 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
896 ThrowException("Value too large for type!");
897 $$ = (int64_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000898};
Chris Lattner00950542001-06-06 20:29:01 +0000899
Chris Lattner00950542001-06-06 20:29:01 +0000900// Operations that are notably excluded from this list include:
901// RET, BR, & SWITCH because they end basic blocks and are treated specially.
902//
Chris Lattner4a6482b2002-09-10 19:57:26 +0000903ArithmeticOps: ADD | SUB | MUL | DIV | REM;
904LogicalOps : AND | OR | XOR;
905SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
906BinaryOps : ArithmeticOps | LogicalOps | SetCondOps;
907
Chris Lattner51727be2002-06-04 21:58:56 +0000908ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +0000909
Chris Lattnere98dda62001-07-14 06:10:16 +0000910// These are some types that allow classification if we only want a particular
911// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +0000912SIntType : LONG | INT | SHORT | SBYTE;
913UIntType : ULONG | UINT | USHORT | UBYTE;
914IntType : SIntType | UIntType;
915FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +0000916
Chris Lattnere98dda62001-07-14 06:10:16 +0000917// OptAssign - Value producing statements have an optional assignment component
Chris Lattner6c23f572003-08-22 05:42:10 +0000918OptAssign : Name '=' {
Chris Lattner00950542001-06-06 20:29:01 +0000919 $$ = $1;
920 }
921 | /*empty*/ {
922 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +0000923 };
Chris Lattner00950542001-06-06 20:29:01 +0000924
Chris Lattner4ad02e72003-04-16 20:28:45 +0000925OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
926 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
Chris Lattner72ac148d2003-10-16 18:29:00 +0000927 WEAK { $$ = GlobalValue::WeakLinkage; } |
Chris Lattner4ad02e72003-04-16 20:28:45 +0000928 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
929 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattner30c89792001-09-07 16:35:17 +0000930
931//===----------------------------------------------------------------------===//
932// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +0000933// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +0000934// access to it, a user must explicitly use TypesV.
935//
936
937// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +0000938TypesV : Types | VOID { $$ = new PATypeHolder($1); };
939UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +0000940
941Types : UpRTypes {
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000942 if (!UpRefs.empty())
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000943 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
944 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000945 };
Chris Lattner30c89792001-09-07 16:35:17 +0000946
947
948// Derived types are added later...
949//
Chris Lattner51727be2002-06-04 21:58:56 +0000950PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
951PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000952UpRTypes : OPAQUE {
953 $$ = new PATypeHolder(OpaqueType::get());
954 }
955 | PrimType {
956 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +0000957 };
Chris Lattnerd78700d2002-08-16 21:14:40 +0000958UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000959 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +0000960};
Chris Lattner30c89792001-09-07 16:35:17 +0000961
Chris Lattner30c89792001-09-07 16:35:17 +0000962// Include derived types in the Types production.
963//
964UpRTypes : '\\' EUINT64VAL { // Type UpReference
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000965 if ($2 > (uint64_t)~0U) ThrowException("Value out of range!");
Chris Lattner30c89792001-09-07 16:35:17 +0000966 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3b073862004-02-09 03:19:29 +0000967 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000968 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +0000969 UR_OUT("New Upreference!\n");
970 }
Chris Lattner79df7c02002-03-26 18:01:55 +0000971 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner9232b992003-04-22 18:42:41 +0000972 std::vector<const Type*> Params;
Chris Lattner697954c2002-01-20 22:54:45 +0000973 mapto($3->begin(), $3->end(), std::back_inserter(Params),
Chris Lattner876dc572003-10-02 19:00:34 +0000974 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner2079fde2001-10-13 06:41:08 +0000975 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
976 if (isVarArg) Params.pop_back();
977
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000978 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +0000979 delete $3; // Delete the argument list
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000980 delete $1; // Delete the return type handle
Chris Lattner30c89792001-09-07 16:35:17 +0000981 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000982 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000983 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000984 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +0000985 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000986 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +0000987 std::vector<const Type*> Elements;
Chris Lattner697954c2002-01-20 22:54:45 +0000988 mapto($2->begin(), $2->end(), std::back_inserter(Elements),
Chris Lattner876dc572003-10-02 19:00:34 +0000989 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner30c89792001-09-07 16:35:17 +0000990
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000991 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000992 delete $2;
993 }
994 | '{' '}' { // Empty structure type?
Chris Lattner9232b992003-04-22 18:42:41 +0000995 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000996 }
997 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000998 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000999 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001000 };
Chris Lattner30c89792001-09-07 16:35:17 +00001001
Chris Lattner7e708292002-06-25 16:13:24 +00001002// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +00001003// declaration type lists
1004//
1005TypeListI : UpRTypes {
Chris Lattner9232b992003-04-22 18:42:41 +00001006 $$ = new std::list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +00001007 $$->push_back(*$1); delete $1;
1008 }
1009 | TypeListI ',' UpRTypes {
1010 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +00001011 };
Chris Lattner30c89792001-09-07 16:35:17 +00001012
Chris Lattner7e708292002-06-25 16:13:24 +00001013// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +00001014ArgTypeListI : TypeListI
1015 | TypeListI ',' DOTDOTDOT {
1016 ($$=$1)->push_back(Type::VoidTy);
1017 }
1018 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001019 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +00001020 }
1021 | /*empty*/ {
Chris Lattner9232b992003-04-22 18:42:41 +00001022 $$ = new std::list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +00001023 };
Chris Lattner30c89792001-09-07 16:35:17 +00001024
Chris Lattnere98dda62001-07-14 06:10:16 +00001025// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +00001026// production is used ONLY to represent constants that show up AFTER a 'const',
1027// 'constant' or 'global' token at global scope. Constants that can be inlined
1028// into other expressions (such as integers and constexprs) are handled by the
1029// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +00001030//
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001031ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Chris Lattner949a3622003-07-23 15:30:06 +00001032 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001033 if (ATy == 0)
1034 ThrowException("Cannot make array constant with type: '" +
1035 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001036 const Type *ETy = ATy->getElementType();
1037 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +00001038
Chris Lattner30c89792001-09-07 16:35:17 +00001039 // Verify that we have the correct size...
1040 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +00001041 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +00001042 utostr($3->size()) + " arguments, but has size of " +
1043 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +00001044
Chris Lattner30c89792001-09-07 16:35:17 +00001045 // Verify all elements are correct type!
1046 for (unsigned i = 0; i < $3->size(); i++) {
1047 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +00001048 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001049 ETy->getDescription() +"' as required!\nIt is of type '"+
1050 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +00001051 }
1052
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001053 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001054 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001055 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001056 | Types '[' ']' {
Chris Lattner949a3622003-07-23 15:30:06 +00001057 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001058 if (ATy == 0)
1059 ThrowException("Cannot make array constant with type: '" +
1060 (*$1)->getDescription() + "'!");
1061
1062 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +00001063 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +00001064 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +00001065 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattner9232b992003-04-22 18:42:41 +00001066 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +00001067 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001068 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001069 | Types 'c' STRINGCONSTANT {
Chris Lattner949a3622003-07-23 15:30:06 +00001070 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001071 if (ATy == 0)
1072 ThrowException("Cannot make array constant with type: '" +
1073 (*$1)->getDescription() + "'!");
1074
Chris Lattner30c89792001-09-07 16:35:17 +00001075 int NumElements = ATy->getNumElements();
1076 const Type *ETy = ATy->getElementType();
1077 char *EndStr = UnEscapeLexed($3, true);
1078 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +00001079 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +00001080 itostr((int)(EndStr-$3)) +
1081 " when array has size " + itostr(NumElements) + "!");
Chris Lattner9232b992003-04-22 18:42:41 +00001082 std::vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +00001083 if (ETy == Type::SByteTy) {
1084 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001085 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +00001086 } else if (ETy == Type::UByteTy) {
1087 for (char *C = $3; C != EndStr; ++C)
Chris Lattnerbae362f2003-01-30 22:24:26 +00001088 Vals.push_back(ConstantUInt::get(ETy, (unsigned char)*C));
Chris Lattner93750fa2001-07-28 17:48:55 +00001089 } else {
Chris Lattner30c89792001-09-07 16:35:17 +00001090 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +00001091 ThrowException("Cannot build string arrays of non byte sized elements!");
1092 }
Chris Lattner30c89792001-09-07 16:35:17 +00001093 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001094 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +00001095 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001096 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001097 | Types '{' ConstVector '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001098 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001099 if (STy == 0)
1100 ThrowException("Cannot make struct constant with type: '" +
1101 (*$1)->getDescription() + "'!");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001102
Chris Lattnerc6212f12003-05-21 16:06:56 +00001103 if ($3->size() != STy->getNumContainedTypes())
1104 ThrowException("Illegal number of initializers for structure type!");
1105
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001106 // Check to ensure that constants are compatible with the type initializer!
1107 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Chris Lattnerd21cd802004-02-09 04:37:31 +00001108 if ((*$3)[i]->getType() != STy->getElementType(i))
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001109 ThrowException("Expected type '" +
Chris Lattnerd21cd802004-02-09 04:37:31 +00001110 STy->getElementType(i)->getDescription() +
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001111 "' for element #" + utostr(i) +
1112 " of structure initializer!");
1113
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001114 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001115 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001116 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001117 | Types '{' '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001118 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001119 if (STy == 0)
1120 ThrowException("Cannot make struct constant with type: '" +
1121 (*$1)->getDescription() + "'!");
1122
1123 if (STy->getNumContainedTypes() != 0)
1124 ThrowException("Illegal number of initializers for structure type!");
1125
1126 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1127 delete $1;
1128 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001129 | Types NULL_TOK {
Chris Lattner949a3622003-07-23 15:30:06 +00001130 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001131 if (PTy == 0)
1132 ThrowException("Cannot make null pointer constant with type: '" +
1133 (*$1)->getDescription() + "'!");
1134
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001135 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001136 delete $1;
1137 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001138 | Types SymbolicValueRef {
Chris Lattner949a3622003-07-23 15:30:06 +00001139 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001140 if (Ty == 0)
1141 ThrowException("Global const reference must be a pointer type!");
1142
Chris Lattner3101c252002-08-15 17:58:33 +00001143 // ConstExprs can exist in the body of a function, thus creating
1144 // ConstantPointerRefs whenever they refer to a variable. Because we are in
1145 // the context of a function, getValNonImprovising will search the functions
1146 // symbol table instead of the module symbol table for the global symbol,
1147 // which throws things all off. To get around this, we just tell
1148 // getValNonImprovising that we are at global scope here.
1149 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001150 Function *SavedCurFn = CurFun.CurrentFunction;
1151 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001152
Chris Lattner2079fde2001-10-13 06:41:08 +00001153 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001154
Chris Lattner394f2eb2003-10-16 20:12:13 +00001155 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001156
Chris Lattner2079fde2001-10-13 06:41:08 +00001157 // If this is an initializer for a constant pointer, which is referencing a
1158 // (currently) undefined variable, create a stub now that shall be replaced
1159 // in the future with the right type of variable.
1160 //
1161 if (V == 0) {
1162 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1163 const PointerType *PT = cast<PointerType>(Ty);
1164
1165 // First check to see if the forward references value is already created!
1166 PerModuleInfo::GlobalRefsType::iterator I =
Chris Lattner9232b992003-04-22 18:42:41 +00001167 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001168
1169 if (I != CurModule.GlobalRefs.end()) {
1170 V = I->second; // Placeholder already exists, use it...
Chris Lattnera989b232003-12-23 20:05:15 +00001171 $2.destroy();
Chris Lattner2079fde2001-10-13 06:41:08 +00001172 } else {
Chris Lattner2079fde2001-10-13 06:41:08 +00001173 // Create a placeholder for the global variable reference...
Chris Lattner7a176752001-12-04 00:03:30 +00001174 GlobalVariable *GV = new GlobalVariable(PT->getElementType(),
Chris Lattner4ad02e72003-04-16 20:28:45 +00001175 false,
1176 GlobalValue::ExternalLinkage);
Chris Lattner2079fde2001-10-13 06:41:08 +00001177 // Keep track of the fact that we have a forward ref to recycle it
Chris Lattner9232b992003-04-22 18:42:41 +00001178 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
Chris Lattner2079fde2001-10-13 06:41:08 +00001179
1180 // Must temporarily push this value into the module table...
1181 CurModule.CurrentModule->getGlobalList().push_back(GV);
1182 V = GV;
1183 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001184 }
1185
Chris Lattner2079fde2001-10-13 06:41:08 +00001186 GlobalValue *GV = cast<GlobalValue>(V);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001187 $$ = ConstantPointerRef::get(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001188 delete $1; // Free the type handle
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001189 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001190 | Types ConstExpr {
1191 if ($1->get() != $2->getType())
1192 ThrowException("Mismatched types for constant expression!");
1193 $$ = $2;
1194 delete $1;
Chris Lattnerf4600482003-06-28 20:01:34 +00001195 }
1196 | Types ZEROINITIALIZER {
1197 $$ = Constant::getNullValue($1->get());
1198 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001199 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001200
Chris Lattnere43f40b2002-10-09 00:25:32 +00001201ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001202 if (!ConstantSInt::isValueValidForType($1, $2))
1203 ThrowException("Constant value doesn't fit in type!");
1204 $$ = ConstantSInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001205 }
1206 | UIntType EUINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001207 if (!ConstantUInt::isValueValidForType($1, $2))
1208 ThrowException("Constant value doesn't fit in type!");
1209 $$ = ConstantUInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001210 }
1211 | BOOL TRUE { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001212 $$ = ConstantBool::True;
1213 }
Chris Lattnere43f40b2002-10-09 00:25:32 +00001214 | BOOL FALSE { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001215 $$ = ConstantBool::False;
1216 }
1217 | FPType FPVAL { // Float & Double constants
1218 $$ = ConstantFP::get($1, $2);
1219 };
1220
Chris Lattner00950542001-06-06 20:29:01 +00001221
Chris Lattnerd78700d2002-08-16 21:14:40 +00001222ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattnerae711a82003-11-21 20:27:35 +00001223 if (!$3->getType()->isFirstClassType())
1224 ThrowException("cast constant expression from a non-primitive type: '" +
1225 $3->getType()->getDescription() + "'!");
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001226 if (!$5->get()->isFirstClassType())
1227 ThrowException("cast constant expression to a non-primitive type: '" +
1228 $5->get()->getDescription() + "'!");
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001229 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001230 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001231 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001232 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1233 if (!isa<PointerType>($3->getType()))
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001234 ThrowException("GetElementPtr requires a pointer operand!");
1235
1236 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001237 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001238 if (!IdxTy)
1239 ThrowException("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001240
Chris Lattner9232b992003-04-22 18:42:41 +00001241 std::vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001242 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1243 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001244 IdxVec.push_back(C);
1245 else
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001246 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001247
Chris Lattnerd78700d2002-08-16 21:14:40 +00001248 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001249
Chris Lattnerd78700d2002-08-16 21:14:40 +00001250 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001251 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001252 | BinaryOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001253 if ($3->getType() != $5->getType())
1254 ThrowException("Binary operator types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001255 $$ = ConstantExpr::get($1, $3, $5);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001256 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001257 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001258 if ($5->getType() != Type::UByteTy)
1259 ThrowException("Shift count for shift constant must be unsigned byte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001260 if (!$3->getType()->isInteger())
1261 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerb16689b2004-01-12 19:08:43 +00001262 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner699f1eb2002-08-14 17:12:33 +00001263 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001264
1265
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001266// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001267ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001268 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001269 }
1270 | ConstVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001271 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001272 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001273 };
Chris Lattner00950542001-06-06 20:29:01 +00001274
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001275
Chris Lattner1781aca2001-09-18 04:00:54 +00001276// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001277GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001278
Chris Lattner00950542001-06-06 20:29:01 +00001279
Chris Lattner0e73ce62002-05-02 19:11:13 +00001280//===----------------------------------------------------------------------===//
1281// Rules to match Modules
1282//===----------------------------------------------------------------------===//
1283
1284// Module rule: Capture the result of parsing the whole file into a result
1285// variable...
1286//
1287Module : FunctionList {
1288 $$ = ParserResult = $1;
1289 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001290};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001291
Chris Lattner7e708292002-06-25 16:13:24 +00001292// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001293//
1294FunctionList : FunctionList Function {
1295 $$ = $1;
1296 assert($2->getParent() == 0 && "Function already in module!");
1297 $1->getFunctionList().push_back($2);
Chris Lattner394f2eb2003-10-16 20:12:13 +00001298 CurFun.FunctionDone();
Chris Lattner0e73ce62002-05-02 19:11:13 +00001299 }
1300 | FunctionList FunctionProto {
1301 $$ = $1;
1302 }
1303 | FunctionList IMPLEMENTATION {
1304 $$ = $1;
1305 }
1306 | ConstPool {
1307 $$ = CurModule.CurrentModule;
1308 // Resolve circular types before we parse the body of the module
1309 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001310 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001311
Chris Lattnere98dda62001-07-14 06:10:16 +00001312// ConstPool - Constants with optional names assigned to them.
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001313ConstPool : ConstPool OptAssign CONST ConstVal {
Chris Lattneradf99702003-03-03 23:28:55 +00001314 if (!setValueName($4, $2))
1315 InsertValue($4);
Chris Lattner00950542001-06-06 20:29:01 +00001316 }
Chris Lattner30c89792001-09-07 16:35:17 +00001317 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner4a42e902001-10-22 05:56:09 +00001318 // Eagerly resolve types. This is not an optimization, this is a
1319 // requirement that is due to the fact that we could have this:
1320 //
1321 // %list = type { %list * }
1322 // %list = type { %list * } ; repeated type decl
1323 //
1324 // If types are not resolved eagerly, then the two types will not be
1325 // determined to be the same type!
1326 //
1327 ResolveTypeTo($2, $4->get());
1328
Chris Lattner1781aca2001-09-18 04:00:54 +00001329 // TODO: FIXME when Type are not const
Chris Lattnerb7474512001-10-03 15:39:04 +00001330 if (!setValueName(const_cast<Type*>($4->get()), $2)) {
1331 // If this is not a redefinition of a type...
1332 if (!$2) {
1333 InsertType($4->get(),
Chris Lattner394f2eb2003-10-16 20:12:13 +00001334 inFunctionScope() ? CurFun.Types : CurModule.Types);
Chris Lattnerb7474512001-10-03 15:39:04 +00001335 }
Chris Lattner30c89792001-09-07 16:35:17 +00001336 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001337
1338 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001339 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001340 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001341 }
Chris Lattner4ad02e72003-04-16 20:28:45 +00001342 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Chris Lattnerdda71962001-11-26 18:54:16 +00001343 const Type *Ty = $5->getType();
Chris Lattner1781aca2001-09-18 04:00:54 +00001344 // Global declarations appear in Constant Pool
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001345 Constant *Initializer = $5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001346 if (Initializer == 0)
1347 ThrowException("Global value initializer is not a constant!");
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001348
Chris Lattnerdda71962001-11-26 18:54:16 +00001349 GlobalVariable *GV = new GlobalVariable(Ty, $4, $3, Initializer);
Chris Lattnerb7474512001-10-03 15:39:04 +00001350 if (!setValueName(GV, $2)) { // If not redefining...
1351 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001352 int Slot = InsertValue(GV, CurModule.Values);
1353
1354 if (Slot != -1) {
1355 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1356 } else {
1357 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1358 (char*)GV->getName().c_str()));
1359 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001360 }
Chris Lattner1781aca2001-09-18 04:00:54 +00001361 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001362 | ConstPool OptAssign EXTERNAL GlobalType Types {
1363 const Type *Ty = *$5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001364 // Global declarations appear in Constant Pool
Chris Lattner4ad02e72003-04-16 20:28:45 +00001365 GlobalVariable *GV = new GlobalVariable(Ty,$4,GlobalValue::ExternalLinkage);
Chris Lattnerb7474512001-10-03 15:39:04 +00001366 if (!setValueName(GV, $2)) { // If not redefining...
1367 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001368 int Slot = InsertValue(GV, CurModule.Values);
1369
1370 if (Slot != -1) {
1371 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1372 } else {
1373 assert(GV->hasName() && "Not named and not numbered!?");
1374 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1375 (char*)GV->getName().c_str()));
1376 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001377 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001378 delete $5;
Chris Lattnere98dda62001-07-14 06:10:16 +00001379 }
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001380 | ConstPool TARGET TargetDefinition {
1381 }
Chris Lattner00950542001-06-06 20:29:01 +00001382 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001383 };
Chris Lattner00950542001-06-06 20:29:01 +00001384
1385
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001386
1387BigOrLittle : BIG { $$ = Module::BigEndian; };
1388BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1389
1390TargetDefinition : ENDIAN '=' BigOrLittle {
1391 CurModule.CurrentModule->setEndianness($3);
1392 }
1393 | POINTERSIZE '=' EUINT64VAL {
1394 if ($3 == 32)
1395 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1396 else if ($3 == 64)
1397 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1398 else
1399 ThrowException("Invalid pointer size: '" + utostr($3) + "'!");
1400 };
1401
1402
Chris Lattner00950542001-06-06 20:29:01 +00001403//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001404// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001405//===----------------------------------------------------------------------===//
1406
Chris Lattner6c23f572003-08-22 05:42:10 +00001407Name : VAR_ID | STRINGCONSTANT;
1408OptName : Name | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001409
Chris Lattner6c23f572003-08-22 05:42:10 +00001410ArgVal : Types OptName {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001411 if (*$1 == Type::VoidTy)
1412 ThrowException("void typed arguments are invalid!");
Chris Lattner9232b992003-04-22 18:42:41 +00001413 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
Chris Lattner51727be2002-06-04 21:58:56 +00001414};
Chris Lattner00950542001-06-06 20:29:01 +00001415
Chris Lattner69da5cf2002-10-13 20:57:00 +00001416ArgListH : ArgListH ',' ArgVal {
1417 $$ = $1;
1418 $1->push_back(*$3);
1419 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001420 }
1421 | ArgVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001422 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
Chris Lattner69da5cf2002-10-13 20:57:00 +00001423 $$->push_back(*$1);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001424 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001425 };
Chris Lattner00950542001-06-06 20:29:01 +00001426
1427ArgList : ArgListH {
1428 $$ = $1;
1429 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001430 | ArgListH ',' DOTDOTDOT {
1431 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001432 $$->push_back(std::pair<PATypeHolder*,
1433 char*>(new PATypeHolder(Type::VoidTy), 0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001434 }
1435 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001436 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1437 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001438 }
Chris Lattner00950542001-06-06 20:29:01 +00001439 | /* empty */ {
1440 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001441 };
Chris Lattner00950542001-06-06 20:29:01 +00001442
Chris Lattner6c23f572003-08-22 05:42:10 +00001443FunctionHeaderH : TypesV Name '(' ArgList ')' {
Chris Lattner1f862af2003-04-16 18:13:57 +00001444 UnEscapeLexed($2);
Chris Lattner9232b992003-04-22 18:42:41 +00001445 std::string FunctionName($2);
Chris Lattnerdda71962001-11-26 18:54:16 +00001446
Chris Lattnerc282f5a2003-11-21 22:32:23 +00001447 if (!(*$1)->isFirstClassType() && *$1 != Type::VoidTy)
1448 ThrowException("LLVM functions cannot return aggregate types!");
1449
Chris Lattner9232b992003-04-22 18:42:41 +00001450 std::vector<const Type*> ParamTypeList;
Chris Lattner1f862af2003-04-16 18:13:57 +00001451 if ($4) { // If there are arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001452 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001453 I != $4->end(); ++I)
Chris Lattner69da5cf2002-10-13 20:57:00 +00001454 ParamTypeList.push_back(I->first->get());
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001455 }
Chris Lattner00950542001-06-06 20:29:01 +00001456
Chris Lattner2079fde2001-10-13 06:41:08 +00001457 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1458 if (isVarArg) ParamTypeList.pop_back();
1459
Chris Lattner1f862af2003-04-16 18:13:57 +00001460 const FunctionType *FT = FunctionType::get(*$1, ParamTypeList, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001461 const PointerType *PFT = PointerType::get(FT);
Chris Lattner1f862af2003-04-16 18:13:57 +00001462 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001463
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001464 Function *Fn = 0;
1465 // Is the function already in symtab?
1466 if ((Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1467 // Yes it is. If this is the case, either we need to be a forward decl,
1468 // or it needs to be.
Chris Lattner394f2eb2003-10-16 20:12:13 +00001469 if (!CurFun.isDeclare && !Fn->isExternal())
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001470 ThrowException("Redefinition of function '" + FunctionName + "'!");
1471
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001472 // If we found a preexisting function prototype, remove it from the
1473 // module, so that we don't get spurious conflicts with global & local
1474 // variables.
1475 //
1476 CurModule.CurrentModule->getFunctionList().remove(Fn);
Chris Lattner34538142002-03-08 19:11:42 +00001477
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001478 // Make sure to strip off any argument names so we can't get conflicts...
1479 for (Function::aiterator AI = Fn->abegin(), AE = Fn->aend(); AI != AE; ++AI)
1480 AI->setName("");
Chris Lattner5659dd12002-07-15 00:10:33 +00001481
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001482 } else { // Not already defined?
Chris Lattner4ad02e72003-04-16 20:28:45 +00001483 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001484 InsertValue(Fn, CurModule.Values);
Chris Lattner1f862af2003-04-16 18:13:57 +00001485 CurModule.DeclareNewGlobalValue(Fn, ValID::create($2));
Chris Lattnere1815642001-07-15 06:35:53 +00001486 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001487 free($2); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001488
Chris Lattner394f2eb2003-10-16 20:12:13 +00001489 CurFun.FunctionStart(Fn);
Chris Lattner00950542001-06-06 20:29:01 +00001490
Chris Lattner7e708292002-06-25 16:13:24 +00001491 // Add all of the arguments we parsed to the function...
Chris Lattner1f862af2003-04-16 18:13:57 +00001492 if ($4) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001493 if (isVarArg) { // Nuke the last entry
Chris Lattner1f862af2003-04-16 18:13:57 +00001494 assert($4->back().first->get() == Type::VoidTy && $4->back().second == 0&&
Chris Lattner69da5cf2002-10-13 20:57:00 +00001495 "Not a varargs marker!");
Chris Lattner1f862af2003-04-16 18:13:57 +00001496 delete $4->back().first;
1497 $4->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00001498 }
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001499 Function::aiterator ArgIt = Fn->abegin();
Chris Lattner9232b992003-04-22 18:42:41 +00001500 for (std::vector<std::pair<PATypeHolder*, char*> >::iterator I =$4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001501 I != $4->end(); ++I, ++ArgIt) {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001502 delete I->first; // Delete the typeholder...
1503
1504 if (setValueName(ArgIt, I->second)) // Insert arg into symtab...
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001505 assert(0 && "No arg redef allowed!");
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001506
Chris Lattner69da5cf2002-10-13 20:57:00 +00001507 InsertValue(ArgIt);
Chris Lattner00950542001-06-06 20:29:01 +00001508 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001509
Chris Lattner1f862af2003-04-16 18:13:57 +00001510 delete $4; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00001511 }
Chris Lattner51727be2002-06-04 21:58:56 +00001512};
Chris Lattner00950542001-06-06 20:29:01 +00001513
Chris Lattner9b02cc32002-05-03 18:23:48 +00001514BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1515
Chris Lattner4ad02e72003-04-16 20:28:45 +00001516FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00001517 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001518
Chris Lattner4ad02e72003-04-16 20:28:45 +00001519 // Make sure that we keep track of the linkage type even if there was a
1520 // previous "declare".
1521 $$->setLinkage($1);
Chris Lattner1f862af2003-04-16 18:13:57 +00001522
Chris Lattner7e708292002-06-25 16:13:24 +00001523 // Resolve circular types before we parse the body of the function.
Chris Lattner394f2eb2003-10-16 20:12:13 +00001524 ResolveTypes(CurFun.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001525};
Chris Lattner00950542001-06-06 20:29:01 +00001526
Chris Lattner9b02cc32002-05-03 18:23:48 +00001527END : ENDTOK | '}'; // Allow end of '}' to end a function
1528
Chris Lattner79df7c02002-03-26 18:01:55 +00001529Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001530 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001531};
Chris Lattner00950542001-06-06 20:29:01 +00001532
Chris Lattner394f2eb2003-10-16 20:12:13 +00001533FunctionProto : DECLARE { CurFun.isDeclare = true; } FunctionHeaderH {
1534 $$ = CurFun.CurrentFunction;
Chris Lattner79df7c02002-03-26 18:01:55 +00001535 assert($$->getParent() == 0 && "Function already in module!");
1536 CurModule.CurrentModule->getFunctionList().push_back($$);
Chris Lattner394f2eb2003-10-16 20:12:13 +00001537 CurFun.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001538};
Chris Lattner00950542001-06-06 20:29:01 +00001539
1540//===----------------------------------------------------------------------===//
1541// Rules to match Basic Blocks
1542//===----------------------------------------------------------------------===//
1543
1544ConstValueRef : ESINT64VAL { // A reference to a direct constant
1545 $$ = ValID::create($1);
1546 }
1547 | EUINT64VAL {
1548 $$ = ValID::create($1);
1549 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001550 | FPVAL { // Perhaps it's an FP constant?
1551 $$ = ValID::create($1);
1552 }
Chris Lattner00950542001-06-06 20:29:01 +00001553 | TRUE {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001554 $$ = ValID::create(ConstantBool::True);
Chris Lattner00950542001-06-06 20:29:01 +00001555 }
1556 | FALSE {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001557 $$ = ValID::create(ConstantBool::False);
Chris Lattner00950542001-06-06 20:29:01 +00001558 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001559 | NULL_TOK {
1560 $$ = ValID::createNull();
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001561 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001562 | ConstExpr {
1563 $$ = ValID::create($1);
1564 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00001565
Chris Lattner2079fde2001-10-13 06:41:08 +00001566// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1567// another value.
1568//
1569SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001570 $$ = ValID::create($1);
1571 }
Chris Lattner6c23f572003-08-22 05:42:10 +00001572 | Name { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001573 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001574 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001575
1576// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001577ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001578
Chris Lattner00950542001-06-06 20:29:01 +00001579
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001580// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1581// type immediately preceeds the value reference, and allows complex constant
1582// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001583ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001584 $$ = getVal(*$1, $2); delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001585 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001586
Chris Lattner00950542001-06-06 20:29:01 +00001587BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner7e708292002-06-25 16:13:24 +00001588 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner00950542001-06-06 20:29:01 +00001589 }
Chris Lattner7e708292002-06-25 16:13:24 +00001590 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
1591 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner51727be2002-06-04 21:58:56 +00001592 };
Chris Lattner00950542001-06-06 20:29:01 +00001593
1594
1595// Basic blocks are terminated by branching instructions:
1596// br, br/cc, switch, ret
1597//
Chris Lattner2079fde2001-10-13 06:41:08 +00001598BasicBlock : InstructionList OptAssign BBTerminatorInst {
1599 if (setValueName($3, $2)) { assert(0 && "No redefn allowed!"); }
1600 InsertValue($3);
1601
1602 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001603 InsertValue($1);
1604 $$ = $1;
1605 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001606 | LABELSTR InstructionList OptAssign BBTerminatorInst {
1607 if (setValueName($4, $3)) { assert(0 && "No redefn allowed!"); }
1608 InsertValue($4);
1609
1610 $2->getInstList().push_back($4);
Chris Lattnerb7474512001-10-03 15:39:04 +00001611 if (setValueName($2, $1)) { assert(0 && "No label redef allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001612
1613 InsertValue($2);
1614 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001615 };
Chris Lattner00950542001-06-06 20:29:01 +00001616
1617InstructionList : InstructionList Inst {
1618 $1->getInstList().push_back($2);
1619 $$ = $1;
1620 }
1621 | /* empty */ {
Chris Lattner99e7ab72003-10-18 05:53:13 +00001622 $$ = CurBB = new BasicBlock();
Chris Lattner51727be2002-06-04 21:58:56 +00001623 };
Chris Lattner00950542001-06-06 20:29:01 +00001624
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001625BBTerminatorInst : RET ResolvedVal { // Return with a result...
1626 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001627 }
1628 | RET VOID { // Return with no result...
1629 $$ = new ReturnInst();
1630 }
1631 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner9636a912001-10-01 16:18:37 +00001632 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
Chris Lattner00950542001-06-06 20:29:01 +00001633 } // Conditional Branch...
1634 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner9636a912001-10-01 16:18:37 +00001635 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1636 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
Chris Lattner00950542001-06-06 20:29:01 +00001637 getVal(Type::BoolTy, $3));
1638 }
1639 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1640 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001641 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
Chris Lattner00950542001-06-06 20:29:01 +00001642 $$ = S;
1643
Chris Lattner9232b992003-04-22 18:42:41 +00001644 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00001645 E = $8->end();
1646 for (; I != E; ++I)
Chris Lattner4354f562003-08-23 23:14:52 +00001647 S->addCase(I->first, I->second);
Chris Lattner00950542001-06-06 20:29:01 +00001648 }
Chris Lattner196850c2003-05-15 21:30:00 +00001649 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
1650 SwitchInst *S = new SwitchInst(getVal($2, $3),
1651 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
1652 $$ = S;
1653 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001654 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO ResolvedVal
Chris Lattnera58e3a12004-02-08 21:48:25 +00001655 UNWIND ResolvedVal {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001656 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001657 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001658
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001659 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1660 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001661 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001662 std::vector<const Type*> ParamTypes;
Chris Lattner2079fde2001-10-13 06:41:08 +00001663 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001664 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1665 I != E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001666 ParamTypes.push_back((*I)->getType());
1667 }
1668
1669 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1670 if (isVarArg) ParamTypes.pop_back();
1671
Chris Lattner79df7c02002-03-26 18:01:55 +00001672 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001673 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00001674 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001675
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001676 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00001677
1678 BasicBlock *Normal = dyn_cast<BasicBlock>($8);
1679 BasicBlock *Except = dyn_cast<BasicBlock>($10);
1680
1681 if (Normal == 0 || Except == 0)
1682 ThrowException("Invoke instruction without label destinations!");
1683
1684 // Create the call node...
1685 if (!$5) { // Has no arguments?
Chris Lattner9232b992003-04-22 18:42:41 +00001686 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001687 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001688 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00001689 // correctly!
1690 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001691 FunctionType::param_iterator I = Ty->param_begin();
1692 FunctionType::param_iterator E = Ty->param_end();
Chris Lattner9232b992003-04-22 18:42:41 +00001693 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001694
1695 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1696 if ((*ArgI)->getType() != *I)
1697 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001698 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001699
1700 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1701 ThrowException("Invalid number of parameters detected!");
1702
Chris Lattner6cdb0112001-11-26 16:54:11 +00001703 $$ = new InvokeInst(V, Normal, Except, *$5);
Chris Lattner2079fde2001-10-13 06:41:08 +00001704 }
Chris Lattner9cd42572003-12-23 22:18:36 +00001705 delete $2;
Chris Lattner2079fde2001-10-13 06:41:08 +00001706 delete $5;
Chris Lattner36143fc2003-09-08 18:54:55 +00001707 }
1708 | UNWIND {
1709 $$ = new UnwindInst();
Chris Lattner51727be2002-06-04 21:58:56 +00001710 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001711
1712
Chris Lattner00950542001-06-06 20:29:01 +00001713
1714JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1715 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001716 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001717 if (V == 0)
1718 ThrowException("May only switch on a constant pool value!");
1719
Chris Lattner9232b992003-04-22 18:42:41 +00001720 $$->push_back(std::make_pair(V, cast<BasicBlock>(getVal($5, $6))));
Chris Lattner00950542001-06-06 20:29:01 +00001721 }
1722 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00001723 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001724 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001725
1726 if (V == 0)
1727 ThrowException("May only switch on a constant pool value!");
1728
Chris Lattner9232b992003-04-22 18:42:41 +00001729 $$->push_back(std::make_pair(V, cast<BasicBlock>(getVal($4, $5))));
Chris Lattner51727be2002-06-04 21:58:56 +00001730 };
Chris Lattner00950542001-06-06 20:29:01 +00001731
1732Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001733 // Is this definition named?? if so, assign the name...
1734 if (setValueName($2, $1)) { assert(0 && "No redefin allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001735 InsertValue($2);
1736 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001737};
Chris Lattner00950542001-06-06 20:29:01 +00001738
Chris Lattnerc24d2082001-06-11 15:04:20 +00001739PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Chris Lattner9232b992003-04-22 18:42:41 +00001740 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
1741 $$->push_back(std::make_pair(getVal(*$1, $3),
1742 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
Chris Lattner30c89792001-09-07 16:35:17 +00001743 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001744 }
1745 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1746 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001747 $1->push_back(std::make_pair(getVal($1->front().first->getType(), $4),
1748 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
Chris Lattner51727be2002-06-04 21:58:56 +00001749 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00001750
1751
Chris Lattner30c89792001-09-07 16:35:17 +00001752ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner9232b992003-04-22 18:42:41 +00001753 $$ = new std::vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001754 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001755 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001756 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001757 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001758 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00001759 };
Chris Lattner00950542001-06-06 20:29:01 +00001760
1761// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00001762ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001763
Chris Lattner4a6482b2002-09-10 19:57:26 +00001764InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1765 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint())
1766 ThrowException("Arithmetic operator requires integer or FP operands!");
1767 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1768 if ($$ == 0)
1769 ThrowException("binary operator returned null!");
1770 delete $2;
1771 }
1772 | LogicalOps Types ValueRef ',' ValueRef {
1773 if (!(*$2)->isIntegral())
1774 ThrowException("Logical operator requires integral operands!");
1775 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1776 if ($$ == 0)
1777 ThrowException("binary operator returned null!");
1778 delete $2;
1779 }
1780 | SetCondOps Types ValueRef ',' ValueRef {
Chris Lattner1cff96a2002-09-10 22:37:46 +00001781 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001782 if ($$ == 0)
1783 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001784 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001785 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00001786 | NOT ResolvedVal {
1787 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
1788 << " Replacing with 'xor'.\n";
1789
1790 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
1791 if (Ones == 0)
1792 ThrowException("Expected integral type for not instruction!");
1793
1794 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00001795 if ($$ == 0)
Chris Lattner699f1eb2002-08-14 17:12:33 +00001796 ThrowException("Could not create a xor instruction!");
Chris Lattner09083092001-07-08 04:57:15 +00001797 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001798 | ShiftOps ResolvedVal ',' ResolvedVal {
1799 if ($4->getType() != Type::UByteTy)
1800 ThrowException("Shift amount must be ubyte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001801 if (!$2->getType()->isInteger())
1802 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001803 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001804 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001805 | CAST ResolvedVal TO Types {
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001806 if (!$4->get()->isFirstClassType())
1807 ThrowException("cast instruction to a non-primitive type: '" +
1808 $4->get()->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001809 $$ = new CastInst($2, *$4);
1810 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001811 }
Chris Lattner8f77dae2003-05-08 02:44:12 +00001812 | VA_ARG ResolvedVal ',' Types {
Chris Lattner99e7ab72003-10-18 05:53:13 +00001813 // FIXME: This is emulation code for an obsolete syntax. This should be
1814 // removed at some point.
1815 if (!ObsoleteVarArgs) {
1816 std::cerr << "WARNING: this file uses obsolete features. "
1817 << "Assemble and disassemble to update it.\n";
1818 ObsoleteVarArgs = true;
1819 }
1820
1821 // First, load the valist...
1822 Instruction *CurVAList = new LoadInst($2, "");
1823 CurBB->getInstList().push_back(CurVAList);
1824
1825 // Emit the vaarg instruction.
1826 $$ = new VAArgInst(CurVAList, *$4);
1827
1828 // Now we must advance the pointer and update it in memory.
1829 Instruction *TheVANext = new VANextInst(CurVAList, *$4);
1830 CurBB->getInstList().push_back(TheVANext);
1831
1832 CurBB->getInstList().push_back(new StoreInst(TheVANext, $2));
1833 delete $4;
1834 }
1835 | VAARG ResolvedVal ',' Types {
1836 $$ = new VAArgInst($2, *$4);
1837 delete $4;
1838 }
1839 | VANEXT ResolvedVal ',' Types {
1840 $$ = new VANextInst($2, *$4);
Chris Lattner8f77dae2003-05-08 02:44:12 +00001841 delete $4;
1842 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00001843 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001844 const Type *Ty = $2->front().first->getType();
Chris Lattner8ea8f362003-10-30 01:38:18 +00001845 if (!Ty->isFirstClassType())
1846 ThrowException("PHI node operands must be of first class type!");
Chris Lattnerc24d2082001-06-11 15:04:20 +00001847 $$ = new PHINode(Ty);
Chris Lattnerbd2531f2003-10-10 16:34:58 +00001848 $$->op_reserve($2->size()*2);
Chris Lattner00950542001-06-06 20:29:01 +00001849 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001850 if ($2->front().first->getType() != Ty)
1851 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001852 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001853 $2->pop_front();
1854 }
1855 delete $2; // Free the list...
1856 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001857 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001858 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001859 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001860
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001861 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1862 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001863 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001864 std::vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001865 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001866 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1867 I != E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00001868 ParamTypes.push_back((*I)->getType());
1869 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001870
1871 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1872 if (isVarArg) ParamTypes.pop_back();
1873
Chris Lattner79df7c02002-03-26 18:01:55 +00001874 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001875 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001876 }
Chris Lattner00950542001-06-06 20:29:01 +00001877
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001878 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001879
Chris Lattner8b81bf52001-07-25 22:47:46 +00001880 // Create the call node...
1881 if (!$5) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00001882 // Make sure no arguments is a good thing!
1883 if (Ty->getNumParams() != 0)
1884 ThrowException("No arguments passed to a function that "
1885 "expects arguments!");
1886
Chris Lattner9232b992003-04-22 18:42:41 +00001887 $$ = new CallInst(V, std::vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001888 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001889 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00001890 // correctly!
1891 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001892 FunctionType::param_iterator I = Ty->param_begin();
1893 FunctionType::param_iterator E = Ty->param_end();
Chris Lattner9232b992003-04-22 18:42:41 +00001894 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001895
1896 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1897 if ((*ArgI)->getType() != *I)
1898 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001899 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001900
Chris Lattner8b81bf52001-07-25 22:47:46 +00001901 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001902 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001903
Chris Lattner6cdb0112001-11-26 16:54:11 +00001904 $$ = new CallInst(V, *$5);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001905 }
Chris Lattnerf64d57a2003-12-23 20:39:17 +00001906 delete $2;
Chris Lattner8b81bf52001-07-25 22:47:46 +00001907 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001908 }
1909 | MemoryInst {
1910 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001911 };
Chris Lattner00950542001-06-06 20:29:01 +00001912
Chris Lattner6cdb0112001-11-26 16:54:11 +00001913
1914// IndexList - List of indices for GEP based instructions...
1915IndexList : ',' ValueRefList {
Chris Lattner15c9c032003-09-08 18:20:29 +00001916 $$ = $2;
1917 } | /* empty */ {
1918 $$ = new std::vector<Value*>();
1919 };
1920
1921OptVolatile : VOLATILE {
1922 $$ = true;
1923 }
1924 | /* empty */ {
1925 $$ = false;
1926 };
1927
Chris Lattner027dcc52001-07-08 21:10:27 +00001928
Chris Lattner00950542001-06-06 20:29:01 +00001929MemoryInst : MALLOC Types {
Chris Lattner05804b72002-09-13 22:28:45 +00001930 $$ = new MallocInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00001931 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001932 }
1933 | MALLOC Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00001934 $$ = new MallocInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001935 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001936 }
1937 | ALLOCA Types {
Chris Lattner05804b72002-09-13 22:28:45 +00001938 $$ = new AllocaInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00001939 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001940 }
1941 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00001942 $$ = new AllocaInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001943 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001944 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001945 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00001946 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001947 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00001948 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001949 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001950 }
1951
Chris Lattner15c9c032003-09-08 18:20:29 +00001952 | OptVolatile LOAD Types ValueRef {
1953 if (!isa<PointerType>($3->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00001954 ThrowException("Can't load from nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00001955 (*$3)->getDescription());
Chris Lattner79ad13802003-09-08 20:29:46 +00001956 $$ = new LoadInst(getVal(*$3, $4), "", $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00001957 delete $3;
Chris Lattner027dcc52001-07-08 21:10:27 +00001958 }
Chris Lattner15c9c032003-09-08 18:20:29 +00001959 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
1960 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00001961 if (!PT)
Chris Lattner72e00252001-12-14 16:28:42 +00001962 ThrowException("Can't store to a nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00001963 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00001964 const Type *ElTy = PT->getElementType();
Chris Lattner15c9c032003-09-08 18:20:29 +00001965 if (ElTy != $3->getType())
1966 ThrowException("Can't store '" + $3->getType()->getDescription() +
Chris Lattner72e00252001-12-14 16:28:42 +00001967 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner0383cc42002-08-21 23:51:21 +00001968
Chris Lattner79ad13802003-09-08 20:29:46 +00001969 $$ = new StoreInst($3, getVal(*$5, $6), $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00001970 delete $5;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001971 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00001972 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00001973 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001974 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner30c89792001-09-07 16:35:17 +00001975 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner72e00252001-12-14 16:28:42 +00001976 ThrowException("Can't get element ptr '" + (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001977 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
1978 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00001979 };
Chris Lattner027dcc52001-07-08 21:10:27 +00001980
Brian Gaeked0fde302003-11-11 22:41:34 +00001981
Chris Lattner00950542001-06-06 20:29:01 +00001982%%
Chris Lattner09083092001-07-08 04:57:15 +00001983int yyerror(const char *ErrorMsg) {
Chris Lattner9232b992003-04-22 18:42:41 +00001984 std::string where
1985 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001986 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Chris Lattner9232b992003-04-22 18:42:41 +00001987 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001988 if (yychar == YYEMPTY)
1989 errMsg += "end-of-file.";
1990 else
Chris Lattner9232b992003-04-22 18:42:41 +00001991 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001992 ThrowException(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00001993 return 0;
1994}