blob: 88e75e3da2b255abc8c18d1b8d0e9a8e79386aff [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 *,
Chris Lattnerbc207592004-03-08 06:09:57 +000076 ValID>, GlobalValue*> 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()) {
Chris Lattnerbc207592004-03-08 06:09:57 +0000117 GlobalValue *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.
Chris Lattnerbc207592004-03-08 06:09:57 +0000128 CPR->replaceUsesOfWithOnConstant(OldGV, GV);
Chris Lattner9e932bd2002-10-14 03:28:42 +0000129 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 Lattnerbc207592004-03-08 06:09:57 +0000132 if (GlobalVariable *GVar = dyn_cast<GlobalVariable>(OldGV))
133 CurrentModule->getGlobalList().erase(GVar);
134 else
135 CurrentModule->getFunctionList().erase(cast<Function>(OldGV));
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000136
Chris Lattner2079fde2001-10-13 06:41:08 +0000137 // Remove the map entry for the global now that it has been created...
138 GlobalRefs.erase(I);
139 }
140 }
141
Chris Lattner00950542001-06-06 20:29:01 +0000142} CurModule;
143
Chris Lattner79df7c02002-03-26 18:01:55 +0000144static struct PerFunctionInfo {
Chris Lattner7e708292002-06-25 16:13:24 +0000145 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner00950542001-06-06 20:29:01 +0000146
Chris Lattner16af11d2004-02-09 21:03:38 +0000147 std::map<unsigned,ValueList> Values; // Keep track of numbered definitions
148 std::map<unsigned,ValueList> LateResolveValues;
Chris Lattner9232b992003-04-22 18:42:41 +0000149 std::vector<PATypeHolder> Types;
150 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner15b69722003-11-12 04:40:30 +0000151 SymbolTable LocalSymtab;
Chris Lattner7e708292002-06-25 16:13:24 +0000152 bool isDeclare; // Is this function a forward declararation?
Chris Lattner00950542001-06-06 20:29:01 +0000153
Chris Lattner79df7c02002-03-26 18:01:55 +0000154 inline PerFunctionInfo() {
155 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000156 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000157 }
158
Chris Lattner79df7c02002-03-26 18:01:55 +0000159 inline void FunctionStart(Function *M) {
160 CurrentFunction = M;
Chris Lattner00950542001-06-06 20:29:01 +0000161 }
162
Chris Lattner79df7c02002-03-26 18:01:55 +0000163 void FunctionDone() {
Chris Lattner00950542001-06-06 20:29:01 +0000164 // If we could not resolve some blocks at parsing time (forward branches)
165 // resolve the branches now...
Chris Lattner386a3b72001-10-16 19:54:17 +0000166 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000167
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000168 // Make sure to resolve any constant expr references that might exist within
169 // the function we just declared itself.
170 ValID FID;
171 if (CurrentFunction->hasName()) {
172 FID = ValID::create((char*)CurrentFunction->getName().c_str());
173 } else {
174 unsigned Slot = CurrentFunction->getType()->getUniqueID();
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000175 // Figure out which slot number if is...
Chris Lattner16af11d2004-02-09 21:03:38 +0000176 ValueList &List = CurModule.Values[Slot];
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000177 for (unsigned i = 0; ; ++i) {
Chris Lattner16af11d2004-02-09 21:03:38 +0000178 assert(i < List.size() && "Function not found!");
179 if (List[i] == CurrentFunction) {
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000180 FID = ValID::create((int)i);
181 break;
182 }
183 }
184 }
185 CurModule.DeclareNewGlobalValue(CurrentFunction, FID);
186
Chris Lattner7e708292002-06-25 16:13:24 +0000187 Values.clear(); // Clear out function local definitions
Chris Lattner15b69722003-11-12 04:40:30 +0000188 Types.clear(); // Clear out function local types
189 LocalSymtab.clear(); // Clear out function local symbol table
Chris Lattner79df7c02002-03-26 18:01:55 +0000190 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000191 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000192 }
Chris Lattner394f2eb2003-10-16 20:12:13 +0000193} CurFun; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000194
Chris Lattner394f2eb2003-10-16 20:12:13 +0000195static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000196
Chris Lattner00950542001-06-06 20:29:01 +0000197
198//===----------------------------------------------------------------------===//
199// Code to handle definitions of all the types
200//===----------------------------------------------------------------------===//
201
Chris Lattner9232b992003-04-22 18:42:41 +0000202static int InsertValue(Value *D,
Chris Lattner16af11d2004-02-09 21:03:38 +0000203 std::map<unsigned,ValueList> &ValueTab = CurFun.Values) {
Chris Lattner2079fde2001-10-13 06:41:08 +0000204 if (D->hasName()) return -1; // Is this a numbered definition?
205
206 // Yes, insert the value into the value table...
207 unsigned type = D->getType()->getUniqueID();
Chris Lattner2079fde2001-10-13 06:41:08 +0000208 //printf("Values[%d][%d] = %d\n", type, ValueTab[type].size(), D);
Chris Lattner16af11d2004-02-09 21:03:38 +0000209 ValueList &List = ValueTab[type];
210 List.push_back(D);
211 return List.size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000212}
213
Chris Lattner30c89792001-09-07 16:35:17 +0000214// TODO: FIXME when Type are not const
Chris Lattner9232b992003-04-22 18:42:41 +0000215static void InsertType(const Type *Ty, std::vector<PATypeHolder> &Types) {
Chris Lattner30c89792001-09-07 16:35:17 +0000216 Types.push_back(Ty);
217}
218
219static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000220 switch (D.Type) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000221 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000222 unsigned Num = (unsigned)D.Num;
223
224 // Module constants occupy the lowest numbered slots...
225 if (Num < CurModule.Types.size())
226 return CurModule.Types[Num];
227
228 Num -= CurModule.Types.size();
229
230 // Check that the number is within bounds...
Chris Lattner394f2eb2003-10-16 20:12:13 +0000231 if (Num <= CurFun.Types.size())
232 return CurFun.Types[Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000233 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000234 }
Chris Lattnerf8dff732002-07-18 05:18:37 +0000235 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000236 std::string Name(D.Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000237 SymbolTable *SymTab = 0;
Chris Lattner6e6026b2002-11-20 18:36:02 +0000238 Value *N = 0;
239 if (inFunctionScope()) {
Chris Lattner394f2eb2003-10-16 20:12:13 +0000240 SymTab = &CurFun.CurrentFunction->getSymbolTable();
Chris Lattner6e6026b2002-11-20 18:36:02 +0000241 N = SymTab->lookup(Type::TypeTy, Name);
242 }
Chris Lattner30c89792001-09-07 16:35:17 +0000243
244 if (N == 0) {
Chris Lattner7e708292002-06-25 16:13:24 +0000245 // Symbol table doesn't automatically chain yet... because the function
Chris Lattner30c89792001-09-07 16:35:17 +0000246 // hasn't been added to the module...
247 //
Chris Lattner6e6026b2002-11-20 18:36:02 +0000248 SymTab = &CurModule.CurrentModule->getSymbolTable();
249 N = SymTab->lookup(Type::TypeTy, Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000250 if (N == 0) break;
251 }
252
253 D.destroy(); // Free old strdup'd memory...
Chris Lattner949a3622003-07-23 15:30:06 +0000254 return cast<Type>(N);
Chris Lattner30c89792001-09-07 16:35:17 +0000255 }
256 default:
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000257 ThrowException("Internal parser error: Invalid symbol type reference!");
Chris Lattner30c89792001-09-07 16:35:17 +0000258 }
259
260 // If we reached here, we referenced either a symbol that we don't know about
261 // or an id number that hasn't been read yet. We may be referencing something
262 // forward, so just create an entry to be resolved later and get to it...
263 //
264 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
265
Chris Lattner9232b992003-04-22 18:42:41 +0000266 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000267 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
Chris Lattner4a42e902001-10-22 05:56:09 +0000268
Chris Lattner9232b992003-04-22 18:42:41 +0000269 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000270 if (I != LateResolver.end()) {
271 return I->second;
272 }
Chris Lattner30c89792001-09-07 16:35:17 +0000273
Chris Lattner82269592001-10-22 06:01:08 +0000274 Type *Typ = OpaqueType::get();
Chris Lattner9232b992003-04-22 18:42:41 +0000275 LateResolver.insert(std::make_pair(D, Typ));
Chris Lattner30c89792001-09-07 16:35:17 +0000276 return Typ;
277}
278
Chris Lattner9232b992003-04-22 18:42:41 +0000279static Value *lookupInSymbolTable(const Type *Ty, const std::string &Name) {
Chris Lattner6e6026b2002-11-20 18:36:02 +0000280 SymbolTable &SymTab =
Chris Lattner394f2eb2003-10-16 20:12:13 +0000281 inFunctionScope() ? CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner9705a152002-05-02 19:27:42 +0000282 CurModule.CurrentModule->getSymbolTable();
Chris Lattner6e6026b2002-11-20 18:36:02 +0000283 return SymTab.lookup(Ty, Name);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000284}
285
Chris Lattner2079fde2001-10-13 06:41:08 +0000286// getValNonImprovising - Look up the value specified by the provided type and
287// the provided ValID. If the value exists and has already been defined, return
288// it. Otherwise return null.
289//
290static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000291 if (isa<FunctionType>(Ty))
292 ThrowException("Functions are not values and "
293 "must be referenced as pointers");
Chris Lattner386a3b72001-10-16 19:54:17 +0000294
Chris Lattner30c89792001-09-07 16:35:17 +0000295 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000296 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000297 unsigned type = Ty->getUniqueID();
Chris Lattner00950542001-06-06 20:29:01 +0000298 unsigned Num = (unsigned)D.Num;
299
300 // Module constants occupy the lowest numbered slots...
Chris Lattner16af11d2004-02-09 21:03:38 +0000301 std::map<unsigned,ValueList>::iterator VI = CurModule.Values.find(type);
302 if (VI != CurModule.Values.end()) {
303 if (Num < VI->second.size())
304 return VI->second[Num];
305 Num -= VI->second.size();
Chris Lattner00950542001-06-06 20:29:01 +0000306 }
307
308 // Make sure that our type is within bounds
Chris Lattner16af11d2004-02-09 21:03:38 +0000309 VI = CurFun.Values.find(type);
310 if (VI == CurFun.Values.end()) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000311
312 // Check that the number is within bounds...
Chris Lattner16af11d2004-02-09 21:03:38 +0000313 if (VI->second.size() <= Num) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000314
Chris Lattner16af11d2004-02-09 21:03:38 +0000315 return VI->second[Num];
Chris Lattner00950542001-06-06 20:29:01 +0000316 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000317
Chris Lattner1a1cb112001-09-30 22:46:54 +0000318 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000319 Value *N = lookupInSymbolTable(Ty, std::string(D.Name));
Chris Lattner2079fde2001-10-13 06:41:08 +0000320 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000321
322 D.destroy(); // Free old strdup'd memory...
323 return N;
324 }
325
Chris Lattner2079fde2001-10-13 06:41:08 +0000326 // Check to make sure that "Ty" is an integral type, and that our
327 // value will fit into the specified type...
328 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattnerd78700d2002-08-16 21:14:40 +0000329 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
330 ThrowException("Signed integral constant '" +
331 itostr(D.ConstPool64) + "' is invalid for type '" +
332 Ty->getDescription() + "'!");
333 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000334
335 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000336 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
337 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000338 ThrowException("Integral constant '" + utostr(D.UConstPool64) +
339 "' is invalid or out of range!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000340 } else { // This is really a signed reference. Transmogrify.
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000341 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000342 }
343 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000344 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000345 }
346
Chris Lattner2079fde2001-10-13 06:41:08 +0000347 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000348 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner2079fde2001-10-13 06:41:08 +0000349 ThrowException("FP constant invalid for type!!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000350 return ConstantFP::get(Ty, D.ConstPoolFP);
Chris Lattner2079fde2001-10-13 06:41:08 +0000351
352 case ValID::ConstNullVal: // Is it a null value?
Chris Lattner9b625032002-05-06 16:15:30 +0000353 if (!isa<PointerType>(Ty))
Chris Lattner2079fde2001-10-13 06:41:08 +0000354 ThrowException("Cannot create a a non pointer null!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000355 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner2079fde2001-10-13 06:41:08 +0000356
Chris Lattnerd78700d2002-08-16 21:14:40 +0000357 case ValID::ConstantVal: // Fully resolved constant?
358 if (D.ConstantValue->getType() != Ty)
359 ThrowException("Constant expression type different from required type!");
360 return D.ConstantValue;
361
Chris Lattner30c89792001-09-07 16:35:17 +0000362 default:
363 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000364 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000365 } // End of switch
366
Chris Lattner2079fde2001-10-13 06:41:08 +0000367 assert(0 && "Unhandled case!");
368 return 0;
369}
370
371
372// getVal - This function is identical to getValNonImprovising, except that if a
373// value is not already defined, it "improvises" by creating a placeholder var
374// that looks and acts just like the requested variable. When the value is
375// defined later, all uses of the placeholder variable are replaced with the
376// real thing.
377//
378static Value *getVal(const Type *Ty, const ValID &D) {
379 assert(Ty != Type::TypeTy && "Should use getTypeVal for types!");
380
381 // See if the value has already been defined...
382 Value *V = getValNonImprovising(Ty, D);
383 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000384
385 // If we reached here, we referenced either a symbol that we don't know about
386 // or an id number that hasn't been read yet. We may be referencing something
387 // forward, so just create an entry to be resolved later and get to it...
388 //
Chris Lattner00950542001-06-06 20:29:01 +0000389 Value *d = 0;
Chris Lattner30c89792001-09-07 16:35:17 +0000390 switch (Ty->getPrimitiveID()) {
391 case Type::LabelTyID: d = new BBPlaceHolder(Ty, D); break;
Chris Lattner30c89792001-09-07 16:35:17 +0000392 default: d = new ValuePlaceHolder(Ty, D); break;
Chris Lattner00950542001-06-06 20:29:01 +0000393 }
394
395 assert(d != 0 && "How did we not make something?");
Chris Lattner79df7c02002-03-26 18:01:55 +0000396 if (inFunctionScope())
Chris Lattner394f2eb2003-10-16 20:12:13 +0000397 InsertValue(d, CurFun.LateResolveValues);
Chris Lattner386a3b72001-10-16 19:54:17 +0000398 else
399 InsertValue(d, CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000400 return d;
401}
402
403
404//===----------------------------------------------------------------------===//
405// Code to handle forward references in instructions
406//===----------------------------------------------------------------------===//
407//
408// This code handles the late binding needed with statements that reference
409// values not defined yet... for example, a forward branch, or the PHI node for
410// a loop body.
411//
Chris Lattner394f2eb2003-10-16 20:12:13 +0000412// This keeps a table (CurFun.LateResolveValues) of all such forward references
Chris Lattner00950542001-06-06 20:29:01 +0000413// and back patchs after we are done.
414//
415
416// ResolveDefinitions - If we could not resolve some defs at parsing
417// time (forward branches, phi functions for loops, etc...) resolve the
418// defs now...
419//
Chris Lattner16af11d2004-02-09 21:03:38 +0000420static void ResolveDefinitions(std::map<unsigned,ValueList> &LateResolvers,
421 std::map<unsigned,ValueList> *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000422 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Chris Lattner16af11d2004-02-09 21:03:38 +0000423 for (std::map<unsigned,ValueList>::iterator LRI = LateResolvers.begin(),
424 E = LateResolvers.end(); LRI != E; ++LRI) {
425 ValueList &List = LRI->second;
426 while (!List.empty()) {
427 Value *V = List.back();
428 List.pop_back();
Chris Lattner386a3b72001-10-16 19:54:17 +0000429 assert(!isa<Type>(V) && "Types should be in LateResolveTypes!");
Chris Lattner00950542001-06-06 20:29:01 +0000430 ValID &DID = getValIDFromPlaceHolder(V);
431
Chris Lattner16af11d2004-02-09 21:03:38 +0000432 Value *TheRealValue =
433 getValNonImprovising(Type::getUniqueIDType(LRI->first), DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000434 if (TheRealValue) {
435 V->replaceAllUsesWith(TheRealValue);
436 delete V;
437 } else if (FutureLateResolvers) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000438 // Functions have their unresolved items forwarded to the module late
Chris Lattner386a3b72001-10-16 19:54:17 +0000439 // resolver table
440 InsertValue(V, *FutureLateResolvers);
441 } else {
Chris Lattner9705a152002-05-02 19:27:42 +0000442 if (DID.Type == ValID::NameVal)
Chris Lattner30c89792001-09-07 16:35:17 +0000443 ThrowException("Reference to an invalid definition: '" +DID.getName()+
444 "' of type '" + V->getType()->getDescription() + "'",
445 getLineNumFromPlaceHolder(V));
446 else
447 ThrowException("Reference to an invalid definition: #" +
448 itostr(DID.Num) + " of type '" +
449 V->getType()->getDescription() + "'",
450 getLineNumFromPlaceHolder(V));
451 }
Chris Lattner00950542001-06-06 20:29:01 +0000452 }
453 }
454
455 LateResolvers.clear();
456}
457
Chris Lattner4a42e902001-10-22 05:56:09 +0000458// ResolveTypeTo - A brand new type was just declared. This means that (if
459// name is not null) things referencing Name can be resolved. Otherwise, things
460// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000461//
Chris Lattner4a42e902001-10-22 05:56:09 +0000462static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner9232b992003-04-22 18:42:41 +0000463 std::vector<PATypeHolder> &Types = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000464 CurFun.Types : CurModule.Types;
Chris Lattner00950542001-06-06 20:29:01 +0000465
Chris Lattner4a42e902001-10-22 05:56:09 +0000466 ValID D;
467 if (Name) D = ValID::create(Name);
468 else D = ValID::create((int)Types.size());
Chris Lattner30c89792001-09-07 16:35:17 +0000469
Chris Lattner9232b992003-04-22 18:42:41 +0000470 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000471 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
Chris Lattner4a42e902001-10-22 05:56:09 +0000472
Chris Lattner9232b992003-04-22 18:42:41 +0000473 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000474 if (I != LateResolver.end()) {
Chris Lattner51727be2002-06-04 21:58:56 +0000475 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner4a42e902001-10-22 05:56:09 +0000476 LateResolver.erase(I);
477 }
478}
479
480// ResolveTypes - At this point, all types should be resolved. Any that aren't
481// are errors.
482//
Chris Lattner9232b992003-04-22 18:42:41 +0000483static void ResolveTypes(std::map<ValID, PATypeHolder> &LateResolveTypes) {
Chris Lattner4a42e902001-10-22 05:56:09 +0000484 if (!LateResolveTypes.empty()) {
Chris Lattner82269592001-10-22 06:01:08 +0000485 const ValID &DID = LateResolveTypes.begin()->first;
Chris Lattner4a42e902001-10-22 05:56:09 +0000486
487 if (DID.Type == ValID::NameVal)
Chris Lattner82269592001-10-22 06:01:08 +0000488 ThrowException("Reference to an invalid type: '" +DID.getName() + "'");
Chris Lattner4a42e902001-10-22 05:56:09 +0000489 else
Chris Lattner82269592001-10-22 06:01:08 +0000490 ThrowException("Reference to an invalid type: #" + itostr(DID.Num));
Chris Lattner30c89792001-09-07 16:35:17 +0000491 }
492}
493
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000494
Chris Lattner1781aca2001-09-18 04:00:54 +0000495// setValueName - Set the specified value to the name given. The name may be
496// null potentially, in which case this is a noop. The string passed in is
497// assumed to be a malloc'd string buffer, and is freed by this function.
498//
Chris Lattnerb7474512001-10-03 15:39:04 +0000499// This function returns true if the value has already been defined, but is
500// allowed to be redefined in the specified context. If the name is a new name
501// for the typeplane, false is returned.
502//
503static bool setValueName(Value *V, char *NameStr) {
504 if (NameStr == 0) return false;
Chris Lattner386a3b72001-10-16 19:54:17 +0000505
Chris Lattner9232b992003-04-22 18:42:41 +0000506 std::string Name(NameStr); // Copy string
Chris Lattner1781aca2001-09-18 04:00:54 +0000507 free(NameStr); // Free old string
508
Chris Lattner2079fde2001-10-13 06:41:08 +0000509 if (V->getType() == Type::VoidTy)
510 ThrowException("Can't assign name '" + Name +
511 "' to a null valued instruction!");
512
Chris Lattner6e6026b2002-11-20 18:36:02 +0000513 SymbolTable &ST = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000514 CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner6e6026b2002-11-20 18:36:02 +0000515 CurModule.CurrentModule->getSymbolTable();
Chris Lattner30c89792001-09-07 16:35:17 +0000516
Chris Lattner6e6026b2002-11-20 18:36:02 +0000517 Value *Existing = ST.lookup(V->getType(), Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000518 if (Existing) { // Inserting a name that is already defined???
519 // There is only one case where this is allowed: when we are refining an
520 // opaque type. In this case, Existing will be an opaque type.
Chris Lattner949a3622003-07-23 15:30:06 +0000521 if (const Type *Ty = dyn_cast<Type>(Existing)) {
Chris Lattner51727be2002-06-04 21:58:56 +0000522 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Ty)) {
Chris Lattner30c89792001-09-07 16:35:17 +0000523 // We ARE replacing an opaque type!
Chris Lattner51727be2002-06-04 21:58:56 +0000524 ((OpaqueType*)OpTy)->refineAbstractTypeTo(cast<Type>(V));
Chris Lattnerb7474512001-10-03 15:39:04 +0000525 return true;
Chris Lattner30c89792001-09-07 16:35:17 +0000526 }
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000527 }
Chris Lattner30c89792001-09-07 16:35:17 +0000528
Chris Lattner9636a912001-10-01 16:18:37 +0000529 // Otherwise, we are a simple redefinition of a value, check to see if it
530 // is defined the same as the old one...
Chris Lattneradf99702003-03-03 23:28:55 +0000531 if (const Type *Ty = dyn_cast<Type>(Existing)) {
532 if (Ty == cast<Type>(V)) return true; // Yes, it's equal.
Chris Lattner699f1eb2002-08-14 17:12:33 +0000533 // std::cerr << "Type: " << Ty->getDescription() << " != "
Chris Lattner949a3622003-07-23 15:30:06 +0000534 // << cast<Type>(V)->getDescription() << "!\n";
Chris Lattneradf99702003-03-03 23:28:55 +0000535 } else if (const Constant *C = dyn_cast<Constant>(Existing)) {
536 if (C == V) return true; // Constants are equal to themselves
Chris Lattnerb7474512001-10-03 15:39:04 +0000537 } else if (GlobalVariable *EGV = dyn_cast<GlobalVariable>(Existing)) {
Chris Lattner43efcbf2001-10-03 19:35:57 +0000538 // We are allowed to redefine a global variable in two circumstances:
539 // 1. If at least one of the globals is uninitialized or
540 // 2. If both initializers have the same value.
541 //
Chris Lattner89219832001-10-03 19:35:04 +0000542 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000543 if (!EGV->hasInitializer() || !GV->hasInitializer() ||
544 EGV->getInitializer() == GV->getInitializer()) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000545
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000546 // Make sure the existing global version gets the initializer! Make
547 // sure that it also gets marked const if the new version is.
Chris Lattner89219832001-10-03 19:35:04 +0000548 if (GV->hasInitializer() && !EGV->hasInitializer())
549 EGV->setInitializer(GV->getInitializer());
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000550 if (GV->isConstant())
551 EGV->setConstant(true);
Chris Lattner4ad02e72003-04-16 20:28:45 +0000552 EGV->setLinkage(GV->getLinkage());
Chris Lattner89219832001-10-03 19:35:04 +0000553
Chris Lattner2079fde2001-10-13 06:41:08 +0000554 delete GV; // Destroy the duplicate!
Chris Lattner89219832001-10-03 19:35:04 +0000555 return true; // They are equivalent!
556 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000557 }
Chris Lattner9636a912001-10-01 16:18:37 +0000558 }
Chris Lattner0b268fb2003-11-25 03:54:16 +0000559
Chris Lattner2079fde2001-10-13 06:41:08 +0000560 ThrowException("Redefinition of value named '" + Name + "' in the '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000561 V->getType()->getDescription() + "' type plane!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000562 }
Chris Lattner00950542001-06-06 20:29:01 +0000563
Chris Lattner15b69722003-11-12 04:40:30 +0000564 // Set the name
Chris Lattner6e6026b2002-11-20 18:36:02 +0000565 V->setName(Name, &ST);
Chris Lattner15b69722003-11-12 04:40:30 +0000566
567 // If we're in function scope
568 if (inFunctionScope()) {
569 // Look up the symbol in the function's local symboltable
570 Existing = CurFun.LocalSymtab.lookup(V->getType(),Name);
571
572 // If it already exists
573 if (Existing) {
Chris Lattner15b69722003-11-12 04:40:30 +0000574 // Bail
575 ThrowException("Redefinition of value named '" + Name + "' in the '" +
576 V->getType()->getDescription() + "' type plane!");
577
578 // otherwise, since it doesn't exist
579 } else {
580 // Insert it.
581 CurFun.LocalSymtab.insert(V);
582 }
583 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000584 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000585}
586
Chris Lattner8896eda2001-07-09 19:38:36 +0000587
Chris Lattner30c89792001-09-07 16:35:17 +0000588//===----------------------------------------------------------------------===//
589// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000590//
Chris Lattner8896eda2001-07-09 19:38:36 +0000591
Chris Lattner3b073862004-02-09 03:19:29 +0000592// TypeContains - Returns true if Ty directly contains E in it.
Chris Lattner30c89792001-09-07 16:35:17 +0000593//
594static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3b073862004-02-09 03:19:29 +0000595 return find(Ty->subtype_begin(), Ty->subtype_end(), E) != Ty->subtype_end();
596}
597
598namespace {
599 struct UpRefRecord {
600 // NestingLevel - The number of nesting levels that need to be popped before
601 // this type is resolved.
602 unsigned NestingLevel;
603
604 // LastContainedTy - This is the type at the current binding level for the
605 // type. Every time we reduce the nesting level, this gets updated.
606 const Type *LastContainedTy;
607
608 // UpRefTy - This is the actual opaque type that the upreference is
609 // represented with.
610 OpaqueType *UpRefTy;
611
612 UpRefRecord(unsigned NL, OpaqueType *URTy)
613 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
614 };
Chris Lattner30c89792001-09-07 16:35:17 +0000615}
Chris Lattner698b56e2001-07-20 19:15:08 +0000616
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000617// UpRefs - A list of the outstanding upreferences that need to be resolved.
Chris Lattner3b073862004-02-09 03:19:29 +0000618static std::vector<UpRefRecord> UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000619
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000620/// HandleUpRefs - Every time we finish a new layer of types, this function is
621/// called. It loops through the UpRefs vector, which is a list of the
622/// currently active types. For each type, if the up reference is contained in
623/// the newly completed type, we decrement the level count. When the level
624/// count reaches zero, the upreferenced type is the type that is passed in:
625/// thus we can complete the cycle.
626///
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000627static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner2c37c182004-02-09 03:03:10 +0000628 if (!ty->isAbstract()) return ty;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000629 PATypeHolder Ty(ty);
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000630 UR_OUT("Type '" << Ty->getDescription() <<
Chris Lattner5084d032001-11-02 07:46:26 +0000631 "' newly formed. Resolving upreferences.\n" <<
632 UpRefs.size() << " upreferences active!\n");
Chris Lattner026a8ce2004-02-09 18:53:54 +0000633
634 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
635 // to zero), we resolve them all together before we resolve them to Ty. At
636 // the end of the loop, if there is anything to resolve to Ty, it will be in
637 // this variable.
638 OpaqueType *TypeToResolve = 0;
639
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000640 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Chris Lattner5084d032001-11-02 07:46:26 +0000641 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Chris Lattner30c89792001-09-07 16:35:17 +0000642 << UpRefs[i].second->getDescription() << ") = "
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000643 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner3b073862004-02-09 03:19:29 +0000644 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
645 // Decrement level of upreference
646 unsigned Level = --UpRefs[i].NestingLevel;
647 UpRefs[i].LastContainedTy = Ty;
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000648 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Chris Lattner30c89792001-09-07 16:35:17 +0000649 if (Level == 0) { // Upreference should be resolved!
Chris Lattner026a8ce2004-02-09 18:53:54 +0000650 if (!TypeToResolve) {
651 TypeToResolve = UpRefs[i].UpRefTy;
652 } else {
653 UR_OUT(" * Resolving upreference for "
654 << UpRefs[i].second->getDescription() << "\n";
655 std::string OldName = UpRefs[i].UpRefTy->getDescription());
656 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
657 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
658 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
659 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000660 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
661 --i; // Do not skip the next element...
Chris Lattner30c89792001-09-07 16:35:17 +0000662 }
663 }
Chris Lattner8896eda2001-07-09 19:38:36 +0000664 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000665
Chris Lattner026a8ce2004-02-09 18:53:54 +0000666 if (TypeToResolve) {
667 UR_OUT(" * Resolving upreference for "
668 << UpRefs[i].second->getDescription() << "\n";
Chris Lattner16af11d2004-02-09 21:03:38 +0000669 std::string OldName = TypeToResolve->getDescription());
Chris Lattner026a8ce2004-02-09 18:53:54 +0000670 TypeToResolve->refineAbstractTypeTo(Ty);
671 }
672
Chris Lattner8896eda2001-07-09 19:38:36 +0000673 return Ty;
674}
675
Chris Lattner30c89792001-09-07 16:35:17 +0000676
Chris Lattner00950542001-06-06 20:29:01 +0000677//===----------------------------------------------------------------------===//
678// RunVMAsmParser - Define an interface to this parser
679//===----------------------------------------------------------------------===//
680//
Chris Lattner9232b992003-04-22 18:42:41 +0000681Module *RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000682 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000683 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000684 llvmAsmlineno = 1; // Reset the current line number...
Chris Lattner99e7ab72003-10-18 05:53:13 +0000685 ObsoleteVarArgs = false;
Chris Lattner00950542001-06-06 20:29:01 +0000686
Chris Lattner75f20532003-04-22 18:02:52 +0000687 // Allocate a new module to read
688 CurModule.CurrentModule = new Module(Filename);
Chris Lattner42570982003-11-26 07:24:58 +0000689
690 try {
691 yyparse(); // Parse the file.
692 } catch (...) {
693 // Clear the symbol table so it doesn't complain when it
694 // gets destructed
695 CurFun.LocalSymtab.clear();
696 throw;
697 }
Chris Lattner99e7ab72003-10-18 05:53:13 +0000698
Chris Lattner00950542001-06-06 20:29:01 +0000699 Module *Result = ParserResult;
Chris Lattner99e7ab72003-10-18 05:53:13 +0000700
701 // Check to see if they called va_start but not va_arg..
702 if (!ObsoleteVarArgs)
703 if (Function *F = Result->getNamedFunction("llvm.va_start"))
704 if (F->asize() == 1) {
705 std::cerr << "WARNING: this file uses obsolete features. "
706 << "Assemble and disassemble to update it.\n";
707 ObsoleteVarArgs = true;
708 }
709
710
711 if (ObsoleteVarArgs) {
712 // If the user is making use of obsolete varargs intrinsics, adjust them for
713 // the user.
714 if (Function *F = Result->getNamedFunction("llvm.va_start")) {
715 assert(F->asize() == 1 && "Obsolete va_start takes 1 argument!");
716
717 const Type *RetTy = F->getFunctionType()->getParamType(0);
718 RetTy = cast<PointerType>(RetTy)->getElementType();
719 Function *NF = Result->getOrInsertFunction("llvm.va_start", RetTy, 0);
720
721 while (!F->use_empty()) {
722 CallInst *CI = cast<CallInst>(F->use_back());
723 Value *V = new CallInst(NF, "", CI);
724 new StoreInst(V, CI->getOperand(1), CI);
725 CI->getParent()->getInstList().erase(CI);
726 }
727 Result->getFunctionList().erase(F);
728 }
729
730 if (Function *F = Result->getNamedFunction("llvm.va_end")) {
731 assert(F->asize() == 1 && "Obsolete va_end takes 1 argument!");
732 const Type *ArgTy = F->getFunctionType()->getParamType(0);
733 ArgTy = cast<PointerType>(ArgTy)->getElementType();
734 Function *NF = Result->getOrInsertFunction("llvm.va_end", Type::VoidTy,
735 ArgTy, 0);
736
737 while (!F->use_empty()) {
738 CallInst *CI = cast<CallInst>(F->use_back());
739 Value *V = new LoadInst(CI->getOperand(1), "", CI);
740 new CallInst(NF, V, "", CI);
741 CI->getParent()->getInstList().erase(CI);
742 }
743 Result->getFunctionList().erase(F);
744 }
745
746 if (Function *F = Result->getNamedFunction("llvm.va_copy")) {
747 assert(F->asize() == 2 && "Obsolete va_copy takes 2 argument!");
748 const Type *ArgTy = F->getFunctionType()->getParamType(0);
749 ArgTy = cast<PointerType>(ArgTy)->getElementType();
750 Function *NF = Result->getOrInsertFunction("llvm.va_copy", ArgTy,
751 ArgTy, 0);
752
753 while (!F->use_empty()) {
754 CallInst *CI = cast<CallInst>(F->use_back());
755 Value *V = new CallInst(NF, CI->getOperand(2), "", CI);
756 new StoreInst(V, CI->getOperand(1), CI);
757 CI->getParent()->getInstList().erase(CI);
758 }
759 Result->getFunctionList().erase(F);
760 }
761 }
762
Chris Lattner00950542001-06-06 20:29:01 +0000763 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
764 ParserResult = 0;
765
766 return Result;
767}
768
Brian Gaeked0fde302003-11-11 22:41:34 +0000769} // End llvm namespace
770
771using namespace llvm;
772
Chris Lattner00950542001-06-06 20:29:01 +0000773%}
774
775%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000776 llvm::Module *ModuleVal;
777 llvm::Function *FunctionVal;
778 std::pair<llvm::PATypeHolder*, char*> *ArgVal;
779 llvm::BasicBlock *BasicBlockVal;
780 llvm::TerminatorInst *TermInstVal;
781 llvm::Instruction *InstVal;
782 llvm::Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000783
Brian Gaeked0fde302003-11-11 22:41:34 +0000784 const llvm::Type *PrimType;
785 llvm::PATypeHolder *TypeVal;
786 llvm::Value *ValueVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000787
Brian Gaeked0fde302003-11-11 22:41:34 +0000788 std::vector<std::pair<llvm::PATypeHolder*,char*> > *ArgList;
789 std::vector<llvm::Value*> *ValueList;
790 std::list<llvm::PATypeHolder> *TypeList;
791 std::list<std::pair<llvm::Value*,
792 llvm::BasicBlock*> > *PHIList; // Represent the RHS of PHI node
793 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
794 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000795
Brian Gaeked0fde302003-11-11 22:41:34 +0000796 llvm::GlobalValue::LinkageTypes Linkage;
Chris Lattner30c89792001-09-07 16:35:17 +0000797 int64_t SInt64Val;
798 uint64_t UInt64Val;
799 int SIntVal;
800 unsigned UIntVal;
801 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000802 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000803
Chris Lattner30c89792001-09-07 16:35:17 +0000804 char *StrVal; // This memory is strdup'd!
Brian Gaeked0fde302003-11-11 22:41:34 +0000805 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000806
Brian Gaeked0fde302003-11-11 22:41:34 +0000807 llvm::Instruction::BinaryOps BinaryOpVal;
808 llvm::Instruction::TermOps TermOpVal;
809 llvm::Instruction::MemoryOps MemOpVal;
810 llvm::Instruction::OtherOps OtherOpVal;
811 llvm::Module::Endianness Endianness;
Chris Lattner00950542001-06-06 20:29:01 +0000812}
813
Chris Lattner79df7c02002-03-26 18:01:55 +0000814%type <ModuleVal> Module FunctionList
815%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000816%type <BasicBlockVal> BasicBlock InstructionList
817%type <TermInstVal> BBTerminatorInst
818%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000819%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000820%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000821%type <ArgList> ArgList ArgListH
822%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000823%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000824%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000825%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000826%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000827%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +0000828%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner15c9c032003-09-08 18:20:29 +0000829%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattner4ad02e72003-04-16 20:28:45 +0000830%type <Linkage> OptLinkage
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000831%type <Endianness> BigOrLittle
Chris Lattner00950542001-06-06 20:29:01 +0000832
Chris Lattner2079fde2001-10-13 06:41:08 +0000833// ValueRef - Unresolved reference to a definition or BB
834%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000835%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000836// Tokens and types for handling constant integer values
837//
838// ESINT64VAL - A negative number within long long range
839%token <SInt64Val> ESINT64VAL
840
841// EUINT64VAL - A positive number within uns. long long range
842%token <UInt64Val> EUINT64VAL
843%type <SInt64Val> EINT64VAL
844
845%token <SIntVal> SINTVAL // Signed 32 bit ints...
846%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
847%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000848%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000849
850// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000851%type <TypeVal> Types TypesV UpRTypes UpRTypesV
852%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000853%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
854%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000855
Chris Lattner6c23f572003-08-22 05:42:10 +0000856%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
857%type <StrVal> Name OptName OptAssign
Chris Lattner00950542001-06-06 20:29:01 +0000858
859
Chris Lattnerf4600482003-06-28 20:01:34 +0000860%token IMPLEMENTATION ZEROINITIALIZER TRUE FALSE BEGINTOK ENDTOK
Chris Lattner15c9c032003-09-08 18:20:29 +0000861%token DECLARE GLOBAL CONSTANT VOLATILE
Chris Lattnera58e3a12004-02-08 21:48:25 +0000862%token TO DOTDOTDOT NULL_TOK CONST INTERNAL LINKONCE WEAK APPENDING
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000863%token OPAQUE NOT EXTERNAL TARGET ENDIAN POINTERSIZE LITTLE BIG
Chris Lattner00950542001-06-06 20:29:01 +0000864
865// Basic Block Terminating Operators
Chris Lattner36143fc2003-09-08 18:54:55 +0000866%token <TermOpVal> RET BR SWITCH INVOKE UNWIND
Chris Lattner00950542001-06-06 20:29:01 +0000867
Chris Lattner00950542001-06-06 20:29:01 +0000868// Binary Operators
869%type <BinaryOpVal> BinaryOps // all the binary operators
Chris Lattner4a6482b2002-09-10 19:57:26 +0000870%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattner42c9e772001-10-20 09:32:59 +0000871%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000872%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000873
874// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000875%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000876
Chris Lattner027dcc52001-07-08 21:10:27 +0000877// Other Operators
878%type <OtherOpVal> ShiftOps
Chris Lattner76f0ff32004-03-12 05:51:36 +0000879%token <OtherOpVal> PHI_TOK CALL CAST SELECT SHL SHR VAARG VANEXT
Chris Lattner99e7ab72003-10-18 05:53:13 +0000880%token VA_ARG // FIXME: OBSOLETE
Chris Lattner027dcc52001-07-08 21:10:27 +0000881
Chris Lattner00950542001-06-06 20:29:01 +0000882%start Module
883%%
884
885// Handle constant integer size restriction and conversion...
886//
Chris Lattner51727be2002-06-04 21:58:56 +0000887INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +0000888INTVAL : UINTVAL {
889 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
890 ThrowException("Value too large for type!");
891 $$ = (int32_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000892};
Chris Lattner00950542001-06-06 20:29:01 +0000893
894
Chris Lattner51727be2002-06-04 21:58:56 +0000895EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +0000896EINT64VAL : EUINT64VAL {
897 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
898 ThrowException("Value too large for type!");
899 $$ = (int64_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000900};
Chris Lattner00950542001-06-06 20:29:01 +0000901
Chris Lattner00950542001-06-06 20:29:01 +0000902// Operations that are notably excluded from this list include:
903// RET, BR, & SWITCH because they end basic blocks and are treated specially.
904//
Chris Lattner4a6482b2002-09-10 19:57:26 +0000905ArithmeticOps: ADD | SUB | MUL | DIV | REM;
906LogicalOps : AND | OR | XOR;
907SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
908BinaryOps : ArithmeticOps | LogicalOps | SetCondOps;
909
Chris Lattner51727be2002-06-04 21:58:56 +0000910ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +0000911
Chris Lattnere98dda62001-07-14 06:10:16 +0000912// These are some types that allow classification if we only want a particular
913// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +0000914SIntType : LONG | INT | SHORT | SBYTE;
915UIntType : ULONG | UINT | USHORT | UBYTE;
916IntType : SIntType | UIntType;
917FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +0000918
Chris Lattnere98dda62001-07-14 06:10:16 +0000919// OptAssign - Value producing statements have an optional assignment component
Chris Lattner6c23f572003-08-22 05:42:10 +0000920OptAssign : Name '=' {
Chris Lattner00950542001-06-06 20:29:01 +0000921 $$ = $1;
922 }
923 | /*empty*/ {
924 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +0000925 };
Chris Lattner00950542001-06-06 20:29:01 +0000926
Chris Lattner4ad02e72003-04-16 20:28:45 +0000927OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
928 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
Chris Lattner72ac148d2003-10-16 18:29:00 +0000929 WEAK { $$ = GlobalValue::WeakLinkage; } |
Chris Lattner4ad02e72003-04-16 20:28:45 +0000930 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
931 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattner30c89792001-09-07 16:35:17 +0000932
933//===----------------------------------------------------------------------===//
934// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +0000935// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +0000936// access to it, a user must explicitly use TypesV.
937//
938
939// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +0000940TypesV : Types | VOID { $$ = new PATypeHolder($1); };
941UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +0000942
943Types : UpRTypes {
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000944 if (!UpRefs.empty())
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000945 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
946 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000947 };
Chris Lattner30c89792001-09-07 16:35:17 +0000948
949
950// Derived types are added later...
951//
Chris Lattner51727be2002-06-04 21:58:56 +0000952PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
953PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000954UpRTypes : OPAQUE {
955 $$ = new PATypeHolder(OpaqueType::get());
956 }
957 | PrimType {
958 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +0000959 };
Chris Lattnerd78700d2002-08-16 21:14:40 +0000960UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000961 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +0000962};
Chris Lattner30c89792001-09-07 16:35:17 +0000963
Chris Lattner30c89792001-09-07 16:35:17 +0000964// Include derived types in the Types production.
965//
966UpRTypes : '\\' EUINT64VAL { // Type UpReference
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000967 if ($2 > (uint64_t)~0U) ThrowException("Value out of range!");
Chris Lattner30c89792001-09-07 16:35:17 +0000968 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3b073862004-02-09 03:19:29 +0000969 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000970 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +0000971 UR_OUT("New Upreference!\n");
972 }
Chris Lattner79df7c02002-03-26 18:01:55 +0000973 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner9232b992003-04-22 18:42:41 +0000974 std::vector<const Type*> Params;
Chris Lattner697954c2002-01-20 22:54:45 +0000975 mapto($3->begin(), $3->end(), std::back_inserter(Params),
Chris Lattner876dc572003-10-02 19:00:34 +0000976 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner2079fde2001-10-13 06:41:08 +0000977 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
978 if (isVarArg) Params.pop_back();
979
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000980 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +0000981 delete $3; // Delete the argument list
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000982 delete $1; // Delete the return type handle
Chris Lattner30c89792001-09-07 16:35:17 +0000983 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000984 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000985 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000986 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +0000987 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000988 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +0000989 std::vector<const Type*> Elements;
Chris Lattner697954c2002-01-20 22:54:45 +0000990 mapto($2->begin(), $2->end(), std::back_inserter(Elements),
Chris Lattner876dc572003-10-02 19:00:34 +0000991 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner30c89792001-09-07 16:35:17 +0000992
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000993 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000994 delete $2;
995 }
996 | '{' '}' { // Empty structure type?
Chris Lattner9232b992003-04-22 18:42:41 +0000997 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000998 }
999 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001000 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001001 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001002 };
Chris Lattner30c89792001-09-07 16:35:17 +00001003
Chris Lattner7e708292002-06-25 16:13:24 +00001004// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +00001005// declaration type lists
1006//
1007TypeListI : UpRTypes {
Chris Lattner9232b992003-04-22 18:42:41 +00001008 $$ = new std::list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +00001009 $$->push_back(*$1); delete $1;
1010 }
1011 | TypeListI ',' UpRTypes {
1012 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +00001013 };
Chris Lattner30c89792001-09-07 16:35:17 +00001014
Chris Lattner7e708292002-06-25 16:13:24 +00001015// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +00001016ArgTypeListI : TypeListI
1017 | TypeListI ',' DOTDOTDOT {
1018 ($$=$1)->push_back(Type::VoidTy);
1019 }
1020 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001021 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +00001022 }
1023 | /*empty*/ {
Chris Lattner9232b992003-04-22 18:42:41 +00001024 $$ = new std::list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +00001025 };
Chris Lattner30c89792001-09-07 16:35:17 +00001026
Chris Lattnere98dda62001-07-14 06:10:16 +00001027// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +00001028// production is used ONLY to represent constants that show up AFTER a 'const',
1029// 'constant' or 'global' token at global scope. Constants that can be inlined
1030// into other expressions (such as integers and constexprs) are handled by the
1031// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +00001032//
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001033ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Chris Lattner949a3622003-07-23 15:30:06 +00001034 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001035 if (ATy == 0)
1036 ThrowException("Cannot make array constant with type: '" +
1037 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001038 const Type *ETy = ATy->getElementType();
1039 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +00001040
Chris Lattner30c89792001-09-07 16:35:17 +00001041 // Verify that we have the correct size...
1042 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +00001043 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +00001044 utostr($3->size()) + " arguments, but has size of " +
1045 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +00001046
Chris Lattner30c89792001-09-07 16:35:17 +00001047 // Verify all elements are correct type!
1048 for (unsigned i = 0; i < $3->size(); i++) {
1049 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +00001050 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001051 ETy->getDescription() +"' as required!\nIt is of type '"+
1052 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +00001053 }
1054
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001055 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001056 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001057 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001058 | Types '[' ']' {
Chris Lattner949a3622003-07-23 15:30:06 +00001059 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001060 if (ATy == 0)
1061 ThrowException("Cannot make array constant with type: '" +
1062 (*$1)->getDescription() + "'!");
1063
1064 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +00001065 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +00001066 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +00001067 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattner9232b992003-04-22 18:42:41 +00001068 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +00001069 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001070 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001071 | Types 'c' STRINGCONSTANT {
Chris Lattner949a3622003-07-23 15:30:06 +00001072 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001073 if (ATy == 0)
1074 ThrowException("Cannot make array constant with type: '" +
1075 (*$1)->getDescription() + "'!");
1076
Chris Lattner30c89792001-09-07 16:35:17 +00001077 int NumElements = ATy->getNumElements();
1078 const Type *ETy = ATy->getElementType();
1079 char *EndStr = UnEscapeLexed($3, true);
1080 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +00001081 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +00001082 itostr((int)(EndStr-$3)) +
1083 " when array has size " + itostr(NumElements) + "!");
Chris Lattner9232b992003-04-22 18:42:41 +00001084 std::vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +00001085 if (ETy == Type::SByteTy) {
1086 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001087 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +00001088 } else if (ETy == Type::UByteTy) {
1089 for (char *C = $3; C != EndStr; ++C)
Chris Lattnerbae362f2003-01-30 22:24:26 +00001090 Vals.push_back(ConstantUInt::get(ETy, (unsigned char)*C));
Chris Lattner93750fa2001-07-28 17:48:55 +00001091 } else {
Chris Lattner30c89792001-09-07 16:35:17 +00001092 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +00001093 ThrowException("Cannot build string arrays of non byte sized elements!");
1094 }
Chris Lattner30c89792001-09-07 16:35:17 +00001095 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001096 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +00001097 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001098 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001099 | Types '{' ConstVector '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001100 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001101 if (STy == 0)
1102 ThrowException("Cannot make struct constant with type: '" +
1103 (*$1)->getDescription() + "'!");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001104
Chris Lattnerc6212f12003-05-21 16:06:56 +00001105 if ($3->size() != STy->getNumContainedTypes())
1106 ThrowException("Illegal number of initializers for structure type!");
1107
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001108 // Check to ensure that constants are compatible with the type initializer!
1109 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Chris Lattnerd21cd802004-02-09 04:37:31 +00001110 if ((*$3)[i]->getType() != STy->getElementType(i))
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001111 ThrowException("Expected type '" +
Chris Lattnerd21cd802004-02-09 04:37:31 +00001112 STy->getElementType(i)->getDescription() +
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001113 "' for element #" + utostr(i) +
1114 " of structure initializer!");
1115
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001116 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001117 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001118 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001119 | Types '{' '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001120 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001121 if (STy == 0)
1122 ThrowException("Cannot make struct constant with type: '" +
1123 (*$1)->getDescription() + "'!");
1124
1125 if (STy->getNumContainedTypes() != 0)
1126 ThrowException("Illegal number of initializers for structure type!");
1127
1128 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1129 delete $1;
1130 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001131 | Types NULL_TOK {
Chris Lattner949a3622003-07-23 15:30:06 +00001132 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001133 if (PTy == 0)
1134 ThrowException("Cannot make null pointer constant with type: '" +
1135 (*$1)->getDescription() + "'!");
1136
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001137 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001138 delete $1;
1139 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001140 | Types SymbolicValueRef {
Chris Lattner949a3622003-07-23 15:30:06 +00001141 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001142 if (Ty == 0)
1143 ThrowException("Global const reference must be a pointer type!");
1144
Chris Lattner3101c252002-08-15 17:58:33 +00001145 // ConstExprs can exist in the body of a function, thus creating
1146 // ConstantPointerRefs whenever they refer to a variable. Because we are in
1147 // the context of a function, getValNonImprovising will search the functions
1148 // symbol table instead of the module symbol table for the global symbol,
1149 // which throws things all off. To get around this, we just tell
1150 // getValNonImprovising that we are at global scope here.
1151 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001152 Function *SavedCurFn = CurFun.CurrentFunction;
1153 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001154
Chris Lattner2079fde2001-10-13 06:41:08 +00001155 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001156
Chris Lattner394f2eb2003-10-16 20:12:13 +00001157 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001158
Chris Lattner2079fde2001-10-13 06:41:08 +00001159 // If this is an initializer for a constant pointer, which is referencing a
1160 // (currently) undefined variable, create a stub now that shall be replaced
1161 // in the future with the right type of variable.
1162 //
1163 if (V == 0) {
1164 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1165 const PointerType *PT = cast<PointerType>(Ty);
1166
1167 // First check to see if the forward references value is already created!
1168 PerModuleInfo::GlobalRefsType::iterator I =
Chris Lattner9232b992003-04-22 18:42:41 +00001169 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001170
1171 if (I != CurModule.GlobalRefs.end()) {
1172 V = I->second; // Placeholder already exists, use it...
Chris Lattnera989b232003-12-23 20:05:15 +00001173 $2.destroy();
Chris Lattner2079fde2001-10-13 06:41:08 +00001174 } else {
Chris Lattner2079fde2001-10-13 06:41:08 +00001175 // Create a placeholder for the global variable reference...
Chris Lattner7a176752001-12-04 00:03:30 +00001176 GlobalVariable *GV = new GlobalVariable(PT->getElementType(),
Chris Lattner4ad02e72003-04-16 20:28:45 +00001177 false,
1178 GlobalValue::ExternalLinkage);
Chris Lattner2079fde2001-10-13 06:41:08 +00001179 // Keep track of the fact that we have a forward ref to recycle it
Chris Lattner9232b992003-04-22 18:42:41 +00001180 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
Chris Lattner2079fde2001-10-13 06:41:08 +00001181
1182 // Must temporarily push this value into the module table...
1183 CurModule.CurrentModule->getGlobalList().push_back(GV);
1184 V = GV;
1185 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001186 }
1187
Chris Lattner2079fde2001-10-13 06:41:08 +00001188 GlobalValue *GV = cast<GlobalValue>(V);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001189 $$ = ConstantPointerRef::get(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001190 delete $1; // Free the type handle
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001191 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001192 | Types ConstExpr {
1193 if ($1->get() != $2->getType())
1194 ThrowException("Mismatched types for constant expression!");
1195 $$ = $2;
1196 delete $1;
Chris Lattnerf4600482003-06-28 20:01:34 +00001197 }
1198 | Types ZEROINITIALIZER {
1199 $$ = Constant::getNullValue($1->get());
1200 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001201 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001202
Chris Lattnere43f40b2002-10-09 00:25:32 +00001203ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001204 if (!ConstantSInt::isValueValidForType($1, $2))
1205 ThrowException("Constant value doesn't fit in type!");
1206 $$ = ConstantSInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001207 }
1208 | UIntType EUINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001209 if (!ConstantUInt::isValueValidForType($1, $2))
1210 ThrowException("Constant value doesn't fit in type!");
1211 $$ = ConstantUInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001212 }
1213 | BOOL TRUE { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001214 $$ = ConstantBool::True;
1215 }
Chris Lattnere43f40b2002-10-09 00:25:32 +00001216 | BOOL FALSE { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001217 $$ = ConstantBool::False;
1218 }
1219 | FPType FPVAL { // Float & Double constants
1220 $$ = ConstantFP::get($1, $2);
1221 };
1222
Chris Lattner00950542001-06-06 20:29:01 +00001223
Chris Lattnerd78700d2002-08-16 21:14:40 +00001224ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattnerae711a82003-11-21 20:27:35 +00001225 if (!$3->getType()->isFirstClassType())
1226 ThrowException("cast constant expression from a non-primitive type: '" +
1227 $3->getType()->getDescription() + "'!");
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001228 if (!$5->get()->isFirstClassType())
1229 ThrowException("cast constant expression to a non-primitive type: '" +
1230 $5->get()->getDescription() + "'!");
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001231 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001232 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001233 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001234 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1235 if (!isa<PointerType>($3->getType()))
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001236 ThrowException("GetElementPtr requires a pointer operand!");
1237
1238 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001239 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001240 if (!IdxTy)
1241 ThrowException("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001242
Chris Lattner9232b992003-04-22 18:42:41 +00001243 std::vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001244 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1245 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001246 IdxVec.push_back(C);
1247 else
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001248 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001249
Chris Lattnerd78700d2002-08-16 21:14:40 +00001250 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001251
Chris Lattnerd78700d2002-08-16 21:14:40 +00001252 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001253 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001254 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1255 if ($3->getType() != Type::BoolTy)
1256 ThrowException("Select condition must be of boolean type!");
1257 if ($5->getType() != $7->getType())
1258 ThrowException("Select operand types must match!");
1259 $$ = ConstantExpr::getSelect($3, $5, $7);
1260 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001261 | BinaryOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001262 if ($3->getType() != $5->getType())
1263 ThrowException("Binary operator types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001264 $$ = ConstantExpr::get($1, $3, $5);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001265 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001266 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001267 if ($5->getType() != Type::UByteTy)
1268 ThrowException("Shift count for shift constant must be unsigned byte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001269 if (!$3->getType()->isInteger())
1270 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerb16689b2004-01-12 19:08:43 +00001271 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner699f1eb2002-08-14 17:12:33 +00001272 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001273
1274
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001275// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001276ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001277 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001278 }
1279 | ConstVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001280 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001281 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001282 };
Chris Lattner00950542001-06-06 20:29:01 +00001283
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001284
Chris Lattner1781aca2001-09-18 04:00:54 +00001285// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001286GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001287
Chris Lattner00950542001-06-06 20:29:01 +00001288
Chris Lattner0e73ce62002-05-02 19:11:13 +00001289//===----------------------------------------------------------------------===//
1290// Rules to match Modules
1291//===----------------------------------------------------------------------===//
1292
1293// Module rule: Capture the result of parsing the whole file into a result
1294// variable...
1295//
1296Module : FunctionList {
1297 $$ = ParserResult = $1;
1298 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001299};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001300
Chris Lattner7e708292002-06-25 16:13:24 +00001301// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001302//
1303FunctionList : FunctionList Function {
1304 $$ = $1;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001305 CurFun.FunctionDone();
Chris Lattner0e73ce62002-05-02 19:11:13 +00001306 }
1307 | FunctionList FunctionProto {
1308 $$ = $1;
1309 }
1310 | FunctionList IMPLEMENTATION {
1311 $$ = $1;
1312 }
1313 | ConstPool {
1314 $$ = CurModule.CurrentModule;
1315 // Resolve circular types before we parse the body of the module
1316 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001317 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001318
Chris Lattnere98dda62001-07-14 06:10:16 +00001319// ConstPool - Constants with optional names assigned to them.
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001320ConstPool : ConstPool OptAssign CONST ConstVal {
Chris Lattneradf99702003-03-03 23:28:55 +00001321 if (!setValueName($4, $2))
1322 InsertValue($4);
Chris Lattner00950542001-06-06 20:29:01 +00001323 }
Chris Lattner30c89792001-09-07 16:35:17 +00001324 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner4a42e902001-10-22 05:56:09 +00001325 // Eagerly resolve types. This is not an optimization, this is a
1326 // requirement that is due to the fact that we could have this:
1327 //
1328 // %list = type { %list * }
1329 // %list = type { %list * } ; repeated type decl
1330 //
1331 // If types are not resolved eagerly, then the two types will not be
1332 // determined to be the same type!
1333 //
1334 ResolveTypeTo($2, $4->get());
1335
Chris Lattner1781aca2001-09-18 04:00:54 +00001336 // TODO: FIXME when Type are not const
Chris Lattnerb7474512001-10-03 15:39:04 +00001337 if (!setValueName(const_cast<Type*>($4->get()), $2)) {
1338 // If this is not a redefinition of a type...
1339 if (!$2) {
1340 InsertType($4->get(),
Chris Lattner394f2eb2003-10-16 20:12:13 +00001341 inFunctionScope() ? CurFun.Types : CurModule.Types);
Chris Lattnerb7474512001-10-03 15:39:04 +00001342 }
Chris Lattner30c89792001-09-07 16:35:17 +00001343 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001344
1345 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001346 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001347 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001348 }
Chris Lattner4ad02e72003-04-16 20:28:45 +00001349 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Chris Lattnerdda71962001-11-26 18:54:16 +00001350 const Type *Ty = $5->getType();
Chris Lattner1781aca2001-09-18 04:00:54 +00001351 // Global declarations appear in Constant Pool
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001352 Constant *Initializer = $5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001353 if (Initializer == 0)
1354 ThrowException("Global value initializer is not a constant!");
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001355
Chris Lattnerdda71962001-11-26 18:54:16 +00001356 GlobalVariable *GV = new GlobalVariable(Ty, $4, $3, Initializer);
Chris Lattnerb7474512001-10-03 15:39:04 +00001357 if (!setValueName(GV, $2)) { // If not redefining...
1358 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001359 int Slot = InsertValue(GV, CurModule.Values);
1360
1361 if (Slot != -1) {
1362 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1363 } else {
1364 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1365 (char*)GV->getName().c_str()));
1366 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001367 }
Chris Lattner1781aca2001-09-18 04:00:54 +00001368 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001369 | ConstPool OptAssign EXTERNAL GlobalType Types {
1370 const Type *Ty = *$5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001371 // Global declarations appear in Constant Pool
Chris Lattner4ad02e72003-04-16 20:28:45 +00001372 GlobalVariable *GV = new GlobalVariable(Ty,$4,GlobalValue::ExternalLinkage);
Chris Lattnerb7474512001-10-03 15:39:04 +00001373 if (!setValueName(GV, $2)) { // If not redefining...
1374 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001375 int Slot = InsertValue(GV, CurModule.Values);
1376
1377 if (Slot != -1) {
1378 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1379 } else {
1380 assert(GV->hasName() && "Not named and not numbered!?");
1381 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1382 (char*)GV->getName().c_str()));
1383 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001384 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001385 delete $5;
Chris Lattnere98dda62001-07-14 06:10:16 +00001386 }
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001387 | ConstPool TARGET TargetDefinition {
1388 }
Chris Lattner00950542001-06-06 20:29:01 +00001389 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001390 };
Chris Lattner00950542001-06-06 20:29:01 +00001391
1392
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001393
1394BigOrLittle : BIG { $$ = Module::BigEndian; };
1395BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1396
1397TargetDefinition : ENDIAN '=' BigOrLittle {
1398 CurModule.CurrentModule->setEndianness($3);
1399 }
1400 | POINTERSIZE '=' EUINT64VAL {
1401 if ($3 == 32)
1402 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1403 else if ($3 == 64)
1404 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1405 else
1406 ThrowException("Invalid pointer size: '" + utostr($3) + "'!");
1407 };
1408
1409
Chris Lattner00950542001-06-06 20:29:01 +00001410//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001411// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001412//===----------------------------------------------------------------------===//
1413
Chris Lattner6c23f572003-08-22 05:42:10 +00001414Name : VAR_ID | STRINGCONSTANT;
1415OptName : Name | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001416
Chris Lattner6c23f572003-08-22 05:42:10 +00001417ArgVal : Types OptName {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001418 if (*$1 == Type::VoidTy)
1419 ThrowException("void typed arguments are invalid!");
Chris Lattner9232b992003-04-22 18:42:41 +00001420 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
Chris Lattner51727be2002-06-04 21:58:56 +00001421};
Chris Lattner00950542001-06-06 20:29:01 +00001422
Chris Lattner69da5cf2002-10-13 20:57:00 +00001423ArgListH : ArgListH ',' ArgVal {
1424 $$ = $1;
1425 $1->push_back(*$3);
1426 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001427 }
1428 | ArgVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001429 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
Chris Lattner69da5cf2002-10-13 20:57:00 +00001430 $$->push_back(*$1);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001431 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001432 };
Chris Lattner00950542001-06-06 20:29:01 +00001433
1434ArgList : ArgListH {
1435 $$ = $1;
1436 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001437 | ArgListH ',' DOTDOTDOT {
1438 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001439 $$->push_back(std::pair<PATypeHolder*,
1440 char*>(new PATypeHolder(Type::VoidTy), 0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001441 }
1442 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001443 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1444 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001445 }
Chris Lattner00950542001-06-06 20:29:01 +00001446 | /* empty */ {
1447 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001448 };
Chris Lattner00950542001-06-06 20:29:01 +00001449
Chris Lattner6c23f572003-08-22 05:42:10 +00001450FunctionHeaderH : TypesV Name '(' ArgList ')' {
Chris Lattner1f862af2003-04-16 18:13:57 +00001451 UnEscapeLexed($2);
Chris Lattner9232b992003-04-22 18:42:41 +00001452 std::string FunctionName($2);
Chris Lattnerdda71962001-11-26 18:54:16 +00001453
Chris Lattnerc282f5a2003-11-21 22:32:23 +00001454 if (!(*$1)->isFirstClassType() && *$1 != Type::VoidTy)
1455 ThrowException("LLVM functions cannot return aggregate types!");
1456
Chris Lattner9232b992003-04-22 18:42:41 +00001457 std::vector<const Type*> ParamTypeList;
Chris Lattner1f862af2003-04-16 18:13:57 +00001458 if ($4) { // If there are arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001459 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001460 I != $4->end(); ++I)
Chris Lattner69da5cf2002-10-13 20:57:00 +00001461 ParamTypeList.push_back(I->first->get());
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001462 }
Chris Lattner00950542001-06-06 20:29:01 +00001463
Chris Lattner2079fde2001-10-13 06:41:08 +00001464 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1465 if (isVarArg) ParamTypeList.pop_back();
1466
Chris Lattner1f862af2003-04-16 18:13:57 +00001467 const FunctionType *FT = FunctionType::get(*$1, ParamTypeList, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001468 const PointerType *PFT = PointerType::get(FT);
Chris Lattner1f862af2003-04-16 18:13:57 +00001469 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001470
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001471 Function *Fn = 0;
1472 // Is the function already in symtab?
1473 if ((Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1474 // Yes it is. If this is the case, either we need to be a forward decl,
1475 // or it needs to be.
Chris Lattner394f2eb2003-10-16 20:12:13 +00001476 if (!CurFun.isDeclare && !Fn->isExternal())
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001477 ThrowException("Redefinition of function '" + FunctionName + "'!");
1478
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001479 // Make sure to strip off any argument names so we can't get conflicts...
1480 for (Function::aiterator AI = Fn->abegin(), AE = Fn->aend(); AI != AE; ++AI)
1481 AI->setName("");
Chris Lattner5659dd12002-07-15 00:10:33 +00001482
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001483 } else { // Not already defined?
Chris Lattner63a22502004-03-08 16:14:19 +00001484 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
1485 CurModule.CurrentModule);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001486 InsertValue(Fn, CurModule.Values);
Chris Lattner1f862af2003-04-16 18:13:57 +00001487 CurModule.DeclareNewGlobalValue(Fn, ValID::create($2));
Chris Lattnere1815642001-07-15 06:35:53 +00001488 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001489 free($2); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001490
Chris Lattner394f2eb2003-10-16 20:12:13 +00001491 CurFun.FunctionStart(Fn);
Chris Lattner00950542001-06-06 20:29:01 +00001492
Chris Lattner7e708292002-06-25 16:13:24 +00001493 // Add all of the arguments we parsed to the function...
Chris Lattner1f862af2003-04-16 18:13:57 +00001494 if ($4) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001495 if (isVarArg) { // Nuke the last entry
Chris Lattner1f862af2003-04-16 18:13:57 +00001496 assert($4->back().first->get() == Type::VoidTy && $4->back().second == 0&&
Chris Lattner69da5cf2002-10-13 20:57:00 +00001497 "Not a varargs marker!");
Chris Lattner1f862af2003-04-16 18:13:57 +00001498 delete $4->back().first;
1499 $4->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00001500 }
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001501 Function::aiterator ArgIt = Fn->abegin();
Chris Lattner9232b992003-04-22 18:42:41 +00001502 for (std::vector<std::pair<PATypeHolder*, char*> >::iterator I =$4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001503 I != $4->end(); ++I, ++ArgIt) {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001504 delete I->first; // Delete the typeholder...
1505
1506 if (setValueName(ArgIt, I->second)) // Insert arg into symtab...
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001507 assert(0 && "No arg redef allowed!");
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001508
Chris Lattner69da5cf2002-10-13 20:57:00 +00001509 InsertValue(ArgIt);
Chris Lattner00950542001-06-06 20:29:01 +00001510 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001511
Chris Lattner1f862af2003-04-16 18:13:57 +00001512 delete $4; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00001513 }
Chris Lattner51727be2002-06-04 21:58:56 +00001514};
Chris Lattner00950542001-06-06 20:29:01 +00001515
Chris Lattner9b02cc32002-05-03 18:23:48 +00001516BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1517
Chris Lattner4ad02e72003-04-16 20:28:45 +00001518FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00001519 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001520
Chris Lattner4ad02e72003-04-16 20:28:45 +00001521 // Make sure that we keep track of the linkage type even if there was a
1522 // previous "declare".
1523 $$->setLinkage($1);
Chris Lattner1f862af2003-04-16 18:13:57 +00001524
Chris Lattner7e708292002-06-25 16:13:24 +00001525 // Resolve circular types before we parse the body of the function.
Chris Lattner394f2eb2003-10-16 20:12:13 +00001526 ResolveTypes(CurFun.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001527};
Chris Lattner00950542001-06-06 20:29:01 +00001528
Chris Lattner9b02cc32002-05-03 18:23:48 +00001529END : ENDTOK | '}'; // Allow end of '}' to end a function
1530
Chris Lattner79df7c02002-03-26 18:01:55 +00001531Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001532 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001533};
Chris Lattner00950542001-06-06 20:29:01 +00001534
Chris Lattner394f2eb2003-10-16 20:12:13 +00001535FunctionProto : DECLARE { CurFun.isDeclare = true; } FunctionHeaderH {
1536 $$ = CurFun.CurrentFunction;
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 Lattner76f0ff32004-03-12 05:51:36 +00001812 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1813 if ($2->getType() != Type::BoolTy)
1814 ThrowException("select condition must be boolean!");
1815 if ($4->getType() != $6->getType())
1816 ThrowException("select value types should match!");
1817 $$ = new SelectInst($2, $4, $6);
1818 }
Chris Lattner8f77dae2003-05-08 02:44:12 +00001819 | VA_ARG ResolvedVal ',' Types {
Chris Lattner99e7ab72003-10-18 05:53:13 +00001820 // FIXME: This is emulation code for an obsolete syntax. This should be
1821 // removed at some point.
1822 if (!ObsoleteVarArgs) {
1823 std::cerr << "WARNING: this file uses obsolete features. "
1824 << "Assemble and disassemble to update it.\n";
1825 ObsoleteVarArgs = true;
1826 }
1827
1828 // First, load the valist...
1829 Instruction *CurVAList = new LoadInst($2, "");
1830 CurBB->getInstList().push_back(CurVAList);
1831
1832 // Emit the vaarg instruction.
1833 $$ = new VAArgInst(CurVAList, *$4);
1834
1835 // Now we must advance the pointer and update it in memory.
1836 Instruction *TheVANext = new VANextInst(CurVAList, *$4);
1837 CurBB->getInstList().push_back(TheVANext);
1838
1839 CurBB->getInstList().push_back(new StoreInst(TheVANext, $2));
1840 delete $4;
1841 }
1842 | VAARG ResolvedVal ',' Types {
1843 $$ = new VAArgInst($2, *$4);
1844 delete $4;
1845 }
1846 | VANEXT ResolvedVal ',' Types {
1847 $$ = new VANextInst($2, *$4);
Chris Lattner8f77dae2003-05-08 02:44:12 +00001848 delete $4;
1849 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00001850 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001851 const Type *Ty = $2->front().first->getType();
Chris Lattner8ea8f362003-10-30 01:38:18 +00001852 if (!Ty->isFirstClassType())
1853 ThrowException("PHI node operands must be of first class type!");
Chris Lattnerc24d2082001-06-11 15:04:20 +00001854 $$ = new PHINode(Ty);
Chris Lattnerbd2531f2003-10-10 16:34:58 +00001855 $$->op_reserve($2->size()*2);
Chris Lattner00950542001-06-06 20:29:01 +00001856 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001857 if ($2->front().first->getType() != Ty)
1858 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001859 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001860 $2->pop_front();
1861 }
1862 delete $2; // Free the list...
1863 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001864 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001865 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001866 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001867
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001868 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1869 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001870 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001871 std::vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001872 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001873 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1874 I != E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00001875 ParamTypes.push_back((*I)->getType());
1876 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001877
1878 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1879 if (isVarArg) ParamTypes.pop_back();
1880
Chris Lattner79df7c02002-03-26 18:01:55 +00001881 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001882 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001883 }
Chris Lattner00950542001-06-06 20:29:01 +00001884
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001885 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001886
Chris Lattner8b81bf52001-07-25 22:47:46 +00001887 // Create the call node...
1888 if (!$5) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00001889 // Make sure no arguments is a good thing!
1890 if (Ty->getNumParams() != 0)
1891 ThrowException("No arguments passed to a function that "
1892 "expects arguments!");
1893
Chris Lattner9232b992003-04-22 18:42:41 +00001894 $$ = new CallInst(V, std::vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001895 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001896 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00001897 // correctly!
1898 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001899 FunctionType::param_iterator I = Ty->param_begin();
1900 FunctionType::param_iterator E = Ty->param_end();
Chris Lattner9232b992003-04-22 18:42:41 +00001901 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001902
1903 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1904 if ((*ArgI)->getType() != *I)
1905 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001906 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001907
Chris Lattner8b81bf52001-07-25 22:47:46 +00001908 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001909 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001910
Chris Lattner6cdb0112001-11-26 16:54:11 +00001911 $$ = new CallInst(V, *$5);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001912 }
Chris Lattnerf64d57a2003-12-23 20:39:17 +00001913 delete $2;
Chris Lattner8b81bf52001-07-25 22:47:46 +00001914 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001915 }
1916 | MemoryInst {
1917 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001918 };
Chris Lattner00950542001-06-06 20:29:01 +00001919
Chris Lattner6cdb0112001-11-26 16:54:11 +00001920
1921// IndexList - List of indices for GEP based instructions...
1922IndexList : ',' ValueRefList {
Chris Lattner15c9c032003-09-08 18:20:29 +00001923 $$ = $2;
1924 } | /* empty */ {
1925 $$ = new std::vector<Value*>();
1926 };
1927
1928OptVolatile : VOLATILE {
1929 $$ = true;
1930 }
1931 | /* empty */ {
1932 $$ = false;
1933 };
1934
Chris Lattner027dcc52001-07-08 21:10:27 +00001935
Chris Lattner00950542001-06-06 20:29:01 +00001936MemoryInst : MALLOC Types {
Chris Lattner05804b72002-09-13 22:28:45 +00001937 $$ = new MallocInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00001938 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001939 }
1940 | MALLOC Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00001941 $$ = new MallocInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001942 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001943 }
1944 | ALLOCA Types {
Chris Lattner05804b72002-09-13 22:28:45 +00001945 $$ = new AllocaInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00001946 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001947 }
1948 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00001949 $$ = new AllocaInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001950 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001951 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001952 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00001953 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001954 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00001955 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001956 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001957 }
1958
Chris Lattner15c9c032003-09-08 18:20:29 +00001959 | OptVolatile LOAD Types ValueRef {
1960 if (!isa<PointerType>($3->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00001961 ThrowException("Can't load from nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00001962 (*$3)->getDescription());
Chris Lattner79ad13802003-09-08 20:29:46 +00001963 $$ = new LoadInst(getVal(*$3, $4), "", $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00001964 delete $3;
Chris Lattner027dcc52001-07-08 21:10:27 +00001965 }
Chris Lattner15c9c032003-09-08 18:20:29 +00001966 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
1967 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00001968 if (!PT)
Chris Lattner72e00252001-12-14 16:28:42 +00001969 ThrowException("Can't store to a nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00001970 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00001971 const Type *ElTy = PT->getElementType();
Chris Lattner15c9c032003-09-08 18:20:29 +00001972 if (ElTy != $3->getType())
1973 ThrowException("Can't store '" + $3->getType()->getDescription() +
Chris Lattner72e00252001-12-14 16:28:42 +00001974 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner0383cc42002-08-21 23:51:21 +00001975
Chris Lattner79ad13802003-09-08 20:29:46 +00001976 $$ = new StoreInst($3, getVal(*$5, $6), $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00001977 delete $5;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001978 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00001979 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00001980 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001981 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner30c89792001-09-07 16:35:17 +00001982 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner72e00252001-12-14 16:28:42 +00001983 ThrowException("Can't get element ptr '" + (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001984 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
1985 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00001986 };
Chris Lattner027dcc52001-07-08 21:10:27 +00001987
Brian Gaeked0fde302003-11-11 22:41:34 +00001988
Chris Lattner00950542001-06-06 20:29:01 +00001989%%
Chris Lattner09083092001-07-08 04:57:15 +00001990int yyerror(const char *ErrorMsg) {
Chris Lattner9232b992003-04-22 18:42:41 +00001991 std::string where
1992 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001993 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Chris Lattner9232b992003-04-22 18:42:41 +00001994 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
Chris Lattnerbc280ab2004-03-30 20:58:25 +00001995 if (yychar == YYEMPTY || yychar == 0)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001996 errMsg += "end-of-file.";
1997 else
Chris Lattner9232b992003-04-22 18:42:41 +00001998 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001999 ThrowException(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00002000 return 0;
2001}