blob: 600ce480a35622c3529f90c8c101ca1643b788e9 [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"
23#include "Support/DepthFirstIterator.h"
Chris Lattner00950542001-06-06 20:29:01 +000024#include <list>
Chris Lattnerc188eeb2002-07-30 18:54:25 +000025#include <utility>
Chris Lattner30c89792001-09-07 16:35:17 +000026#include <algorithm>
Chris Lattner00950542001-06-06 20:29:01 +000027
Chris Lattner386a3b72001-10-16 19:54:17 +000028int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
Chris Lattner09083092001-07-08 04:57:15 +000029int yylex(); // declaration" of xxx warnings.
Chris Lattner00950542001-06-06 20:29:01 +000030int yyparse();
31
Brian Gaeked0fde302003-11-11 22:41:34 +000032namespace llvm {
33
Chris Lattner00950542001-06-06 20:29:01 +000034static Module *ParserResult;
Chris Lattner9232b992003-04-22 18:42:41 +000035std::string CurFilename;
Chris Lattner00950542001-06-06 20:29:01 +000036
Chris Lattner30c89792001-09-07 16:35:17 +000037// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
38// relating to upreferences in the input stream.
39//
40//#define DEBUG_UPREFS 1
41#ifdef DEBUG_UPREFS
Chris Lattner699f1eb2002-08-14 17:12:33 +000042#define UR_OUT(X) std::cerr << X
Chris Lattner30c89792001-09-07 16:35:17 +000043#else
44#define UR_OUT(X)
45#endif
46
Vikram S. Adved3f7eb02002-07-14 22:59:28 +000047#define YYERROR_VERBOSE 1
48
Chris Lattner99e7ab72003-10-18 05:53:13 +000049// HACK ALERT: This variable is used to implement the automatic conversion of
50// variable argument instructions from their old to new forms. When this
51// compatiblity "Feature" is removed, this should be too.
52//
53static BasicBlock *CurBB;
54static bool ObsoleteVarArgs;
55
56
Chris Lattner7e708292002-06-25 16:13:24 +000057// This contains info used when building the body of a function. It is
58// destroyed when the function is completed.
Chris Lattner00950542001-06-06 20:29:01 +000059//
Chris Lattner9232b992003-04-22 18:42:41 +000060typedef std::vector<Value *> ValueList; // Numbered defs
61static void ResolveDefinitions(std::vector<ValueList> &LateResolvers,
62 std::vector<ValueList> *FutureLateResolvers = 0);
Chris Lattner00950542001-06-06 20:29:01 +000063
64static struct PerModuleInfo {
65 Module *CurrentModule;
Chris Lattner9232b992003-04-22 18:42:41 +000066 std::vector<ValueList> Values; // Module level numbered definitions
67 std::vector<ValueList> LateResolveValues;
68 std::vector<PATypeHolder> Types;
69 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000070
Chris Lattner2079fde2001-10-13 06:41:08 +000071 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
72 // references to global values. Global values may be referenced before they
73 // are defined, and if so, the temporary object that they represent is held
Chris Lattnere9bb2df2001-12-03 22:26:30 +000074 // here. This is used for forward references of ConstantPointerRefs.
Chris Lattner2079fde2001-10-13 06:41:08 +000075 //
Chris Lattner9232b992003-04-22 18:42:41 +000076 typedef std::map<std::pair<const PointerType *,
77 ValID>, GlobalVariable*> GlobalRefsType;
Chris Lattner2079fde2001-10-13 06:41:08 +000078 GlobalRefsType GlobalRefs;
79
Chris Lattner00950542001-06-06 20:29:01 +000080 void ModuleDone() {
Chris Lattner7e708292002-06-25 16:13:24 +000081 // If we could not resolve some functions at function compilation time
82 // (calls to functions before they are defined), resolve them now... Types
83 // are resolved when the constant pool has been completely parsed.
Chris Lattner30c89792001-09-07 16:35:17 +000084 //
Chris Lattner00950542001-06-06 20:29:01 +000085 ResolveDefinitions(LateResolveValues);
86
Chris Lattner2079fde2001-10-13 06:41:08 +000087 // Check to make sure that all global value forward references have been
88 // resolved!
89 //
90 if (!GlobalRefs.empty()) {
Chris Lattner9232b992003-04-22 18:42:41 +000091 std::string UndefinedReferences = "Unresolved global references exist:\n";
Chris Lattner749ce032002-03-11 22:12:39 +000092
93 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
94 I != E; ++I) {
95 UndefinedReferences += " " + I->first.first->getDescription() + " " +
96 I->first.second.getName() + "\n";
97 }
98 ThrowException(UndefinedReferences);
Chris Lattner2079fde2001-10-13 06:41:08 +000099 }
100
Chris Lattner7e708292002-06-25 16:13:24 +0000101 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000102 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +0000103 CurrentModule = 0;
104 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000105
106
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000107 // DeclareNewGlobalValue - Called every time a new GV has been defined. This
Chris Lattner2079fde2001-10-13 06:41:08 +0000108 // is used to remove things from the forward declaration map, resolving them
109 // to the correct thing as needed.
110 //
111 void DeclareNewGlobalValue(GlobalValue *GV, ValID D) {
112 // Check to see if there is a forward reference to this global variable...
113 // if there is, eliminate it and patch the reference to use the new def'n.
Chris Lattner9232b992003-04-22 18:42:41 +0000114 GlobalRefsType::iterator I =
115 GlobalRefs.find(std::make_pair(GV->getType(), D));
Chris Lattner2079fde2001-10-13 06:41:08 +0000116
117 if (I != GlobalRefs.end()) {
118 GlobalVariable *OldGV = I->second; // Get the placeholder...
Misha Brukman5560c9d2003-08-18 14:43:39 +0000119 I->first.second.destroy(); // Free string memory if necessary
Chris Lattner2079fde2001-10-13 06:41:08 +0000120
121 // Loop over all of the uses of the GlobalValue. The only thing they are
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000122 // allowed to be is ConstantPointerRef's.
Chris Lattnerfd059242003-10-15 16:48:29 +0000123 assert(OldGV->hasOneUse() && "Only one reference should exist!");
Chris Lattner9e932bd2002-10-14 03:28:42 +0000124 User *U = OldGV->use_back(); // Must be a ConstantPointerRef...
125 ConstantPointerRef *CPR = cast<ConstantPointerRef>(U);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000126
Chris Lattner9e932bd2002-10-14 03:28:42 +0000127 // Change the const pool reference to point to the real global variable
128 // now. This should drop a use from the OldGV.
129 CPR->mutateReferences(OldGV, GV);
130 assert(OldGV->use_empty() && "All uses should be gone now!");
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000131
132 // Remove OldGV from the module...
Chris Lattner2079fde2001-10-13 06:41:08 +0000133 CurrentModule->getGlobalList().remove(OldGV);
134 delete OldGV; // Delete the old placeholder
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000135
Chris Lattner2079fde2001-10-13 06:41:08 +0000136 // Remove the map entry for the global now that it has been created...
137 GlobalRefs.erase(I);
138 }
139 }
140
Chris Lattner00950542001-06-06 20:29:01 +0000141} CurModule;
142
Chris Lattner79df7c02002-03-26 18:01:55 +0000143static struct PerFunctionInfo {
Chris Lattner7e708292002-06-25 16:13:24 +0000144 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner00950542001-06-06 20:29:01 +0000145
Chris Lattner9232b992003-04-22 18:42:41 +0000146 std::vector<ValueList> Values; // Keep track of numbered definitions
147 std::vector<ValueList> LateResolveValues;
148 std::vector<PATypeHolder> Types;
149 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner15b69722003-11-12 04:40:30 +0000150 SymbolTable LocalSymtab;
Chris Lattner7e708292002-06-25 16:13:24 +0000151 bool isDeclare; // Is this function a forward declararation?
Chris Lattner00950542001-06-06 20:29:01 +0000152
Chris Lattner79df7c02002-03-26 18:01:55 +0000153 inline PerFunctionInfo() {
154 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000155 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000156 }
157
Chris Lattner79df7c02002-03-26 18:01:55 +0000158 inline void FunctionStart(Function *M) {
159 CurrentFunction = M;
Chris Lattner00950542001-06-06 20:29:01 +0000160 }
161
Chris Lattner79df7c02002-03-26 18:01:55 +0000162 void FunctionDone() {
Chris Lattner00950542001-06-06 20:29:01 +0000163 // If we could not resolve some blocks at parsing time (forward branches)
164 // resolve the branches now...
Chris Lattner386a3b72001-10-16 19:54:17 +0000165 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000166
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000167 // Make sure to resolve any constant expr references that might exist within
168 // the function we just declared itself.
169 ValID FID;
170 if (CurrentFunction->hasName()) {
171 FID = ValID::create((char*)CurrentFunction->getName().c_str());
172 } else {
173 unsigned Slot = CurrentFunction->getType()->getUniqueID();
174 assert(CurModule.Values.size() > Slot && "Function not inserted?");
175 // Figure out which slot number if is...
176 for (unsigned i = 0; ; ++i) {
177 assert(i < CurModule.Values[Slot].size() && "Function not found!");
178 if (CurModule.Values[Slot][i] == CurrentFunction) {
179 FID = ValID::create((int)i);
180 break;
181 }
182 }
183 }
184 CurModule.DeclareNewGlobalValue(CurrentFunction, FID);
185
Chris Lattner7e708292002-06-25 16:13:24 +0000186 Values.clear(); // Clear out function local definitions
Chris Lattner15b69722003-11-12 04:40:30 +0000187 Types.clear(); // Clear out function local types
188 LocalSymtab.clear(); // Clear out function local symbol table
Chris Lattner79df7c02002-03-26 18:01:55 +0000189 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000190 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000191 }
Chris Lattner394f2eb2003-10-16 20:12:13 +0000192} CurFun; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000193
Chris Lattner394f2eb2003-10-16 20:12:13 +0000194static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000195
Chris Lattner00950542001-06-06 20:29:01 +0000196
197//===----------------------------------------------------------------------===//
198// Code to handle definitions of all the types
199//===----------------------------------------------------------------------===//
200
Chris Lattner9232b992003-04-22 18:42:41 +0000201static int InsertValue(Value *D,
Chris Lattner394f2eb2003-10-16 20:12:13 +0000202 std::vector<ValueList> &ValueTab = CurFun.Values) {
Chris Lattner2079fde2001-10-13 06:41:08 +0000203 if (D->hasName()) return -1; // Is this a numbered definition?
204
205 // Yes, insert the value into the value table...
206 unsigned type = D->getType()->getUniqueID();
207 if (ValueTab.size() <= type)
208 ValueTab.resize(type+1, ValueList());
209 //printf("Values[%d][%d] = %d\n", type, ValueTab[type].size(), D);
210 ValueTab[type].push_back(D);
211 return ValueTab[type].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...
301 if (type < CurModule.Values.size()) {
302 if (Num < CurModule.Values[type].size())
303 return CurModule.Values[type][Num];
304
305 Num -= CurModule.Values[type].size();
306 }
307
308 // Make sure that our type is within bounds
Chris Lattner394f2eb2003-10-16 20:12:13 +0000309 if (CurFun.Values.size() <= type) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000310
311 // Check that the number is within bounds...
Chris Lattner394f2eb2003-10-16 20:12:13 +0000312 if (CurFun.Values[type].size() <= Num) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000313
Chris Lattner394f2eb2003-10-16 20:12:13 +0000314 return CurFun.Values[type][Num];
Chris Lattner00950542001-06-06 20:29:01 +0000315 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000316
Chris Lattner1a1cb112001-09-30 22:46:54 +0000317 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000318 Value *N = lookupInSymbolTable(Ty, std::string(D.Name));
Chris Lattner2079fde2001-10-13 06:41:08 +0000319 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000320
321 D.destroy(); // Free old strdup'd memory...
322 return N;
323 }
324
Chris Lattner2079fde2001-10-13 06:41:08 +0000325 // Check to make sure that "Ty" is an integral type, and that our
326 // value will fit into the specified type...
327 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattnerd78700d2002-08-16 21:14:40 +0000328 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
329 ThrowException("Signed integral constant '" +
330 itostr(D.ConstPool64) + "' is invalid for type '" +
331 Ty->getDescription() + "'!");
332 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000333
334 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000335 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
336 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000337 ThrowException("Integral constant '" + utostr(D.UConstPool64) +
338 "' is invalid or out of range!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000339 } else { // This is really a signed reference. Transmogrify.
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000340 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000341 }
342 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000343 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000344 }
345
Chris Lattner2079fde2001-10-13 06:41:08 +0000346 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000347 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner2079fde2001-10-13 06:41:08 +0000348 ThrowException("FP constant invalid for type!!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000349 return ConstantFP::get(Ty, D.ConstPoolFP);
Chris Lattner2079fde2001-10-13 06:41:08 +0000350
351 case ValID::ConstNullVal: // Is it a null value?
Chris Lattner9b625032002-05-06 16:15:30 +0000352 if (!isa<PointerType>(Ty))
Chris Lattner2079fde2001-10-13 06:41:08 +0000353 ThrowException("Cannot create a a non pointer null!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000354 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner2079fde2001-10-13 06:41:08 +0000355
Chris Lattnerd78700d2002-08-16 21:14:40 +0000356 case ValID::ConstantVal: // Fully resolved constant?
357 if (D.ConstantValue->getType() != Ty)
358 ThrowException("Constant expression type different from required type!");
359 return D.ConstantValue;
360
Chris Lattner30c89792001-09-07 16:35:17 +0000361 default:
362 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000363 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000364 } // End of switch
365
Chris Lattner2079fde2001-10-13 06:41:08 +0000366 assert(0 && "Unhandled case!");
367 return 0;
368}
369
370
371// getVal - This function is identical to getValNonImprovising, except that if a
372// value is not already defined, it "improvises" by creating a placeholder var
373// that looks and acts just like the requested variable. When the value is
374// defined later, all uses of the placeholder variable are replaced with the
375// real thing.
376//
377static Value *getVal(const Type *Ty, const ValID &D) {
378 assert(Ty != Type::TypeTy && "Should use getTypeVal for types!");
379
380 // See if the value has already been defined...
381 Value *V = getValNonImprovising(Ty, D);
382 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000383
384 // If we reached here, we referenced either a symbol that we don't know about
385 // or an id number that hasn't been read yet. We may be referencing something
386 // forward, so just create an entry to be resolved later and get to it...
387 //
Chris Lattner00950542001-06-06 20:29:01 +0000388 Value *d = 0;
Chris Lattner30c89792001-09-07 16:35:17 +0000389 switch (Ty->getPrimitiveID()) {
390 case Type::LabelTyID: d = new BBPlaceHolder(Ty, D); break;
Chris Lattner30c89792001-09-07 16:35:17 +0000391 default: d = new ValuePlaceHolder(Ty, D); break;
Chris Lattner00950542001-06-06 20:29:01 +0000392 }
393
394 assert(d != 0 && "How did we not make something?");
Chris Lattner79df7c02002-03-26 18:01:55 +0000395 if (inFunctionScope())
Chris Lattner394f2eb2003-10-16 20:12:13 +0000396 InsertValue(d, CurFun.LateResolveValues);
Chris Lattner386a3b72001-10-16 19:54:17 +0000397 else
398 InsertValue(d, CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000399 return d;
400}
401
402
403//===----------------------------------------------------------------------===//
404// Code to handle forward references in instructions
405//===----------------------------------------------------------------------===//
406//
407// This code handles the late binding needed with statements that reference
408// values not defined yet... for example, a forward branch, or the PHI node for
409// a loop body.
410//
Chris Lattner394f2eb2003-10-16 20:12:13 +0000411// This keeps a table (CurFun.LateResolveValues) of all such forward references
Chris Lattner00950542001-06-06 20:29:01 +0000412// and back patchs after we are done.
413//
414
415// ResolveDefinitions - If we could not resolve some defs at parsing
416// time (forward branches, phi functions for loops, etc...) resolve the
417// defs now...
418//
Chris Lattner9232b992003-04-22 18:42:41 +0000419static void ResolveDefinitions(std::vector<ValueList> &LateResolvers,
420 std::vector<ValueList> *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000421 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
422 for (unsigned ty = 0; ty < LateResolvers.size(); ty++) {
423 while (!LateResolvers[ty].empty()) {
424 Value *V = LateResolvers[ty].back();
Chris Lattner386a3b72001-10-16 19:54:17 +0000425 assert(!isa<Type>(V) && "Types should be in LateResolveTypes!");
426
Chris Lattner00950542001-06-06 20:29:01 +0000427 LateResolvers[ty].pop_back();
428 ValID &DID = getValIDFromPlaceHolder(V);
429
Chris Lattner2079fde2001-10-13 06:41:08 +0000430 Value *TheRealValue = getValNonImprovising(Type::getUniqueIDType(ty),DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000431 if (TheRealValue) {
432 V->replaceAllUsesWith(TheRealValue);
433 delete V;
434 } else if (FutureLateResolvers) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000435 // Functions have their unresolved items forwarded to the module late
Chris Lattner386a3b72001-10-16 19:54:17 +0000436 // resolver table
437 InsertValue(V, *FutureLateResolvers);
438 } else {
Chris Lattner9705a152002-05-02 19:27:42 +0000439 if (DID.Type == ValID::NameVal)
Chris Lattner30c89792001-09-07 16:35:17 +0000440 ThrowException("Reference to an invalid definition: '" +DID.getName()+
441 "' of type '" + V->getType()->getDescription() + "'",
442 getLineNumFromPlaceHolder(V));
443 else
444 ThrowException("Reference to an invalid definition: #" +
445 itostr(DID.Num) + " of type '" +
446 V->getType()->getDescription() + "'",
447 getLineNumFromPlaceHolder(V));
448 }
Chris Lattner00950542001-06-06 20:29:01 +0000449 }
450 }
451
452 LateResolvers.clear();
453}
454
Chris Lattner4a42e902001-10-22 05:56:09 +0000455// ResolveTypeTo - A brand new type was just declared. This means that (if
456// name is not null) things referencing Name can be resolved. Otherwise, things
457// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000458//
Chris Lattner4a42e902001-10-22 05:56:09 +0000459static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner9232b992003-04-22 18:42:41 +0000460 std::vector<PATypeHolder> &Types = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000461 CurFun.Types : CurModule.Types;
Chris Lattner00950542001-06-06 20:29:01 +0000462
Chris Lattner4a42e902001-10-22 05:56:09 +0000463 ValID D;
464 if (Name) D = ValID::create(Name);
465 else D = ValID::create((int)Types.size());
Chris Lattner30c89792001-09-07 16:35:17 +0000466
Chris Lattner9232b992003-04-22 18:42:41 +0000467 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000468 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
Chris Lattner4a42e902001-10-22 05:56:09 +0000469
Chris Lattner9232b992003-04-22 18:42:41 +0000470 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000471 if (I != LateResolver.end()) {
Chris Lattner51727be2002-06-04 21:58:56 +0000472 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner4a42e902001-10-22 05:56:09 +0000473 LateResolver.erase(I);
474 }
475}
476
477// ResolveTypes - At this point, all types should be resolved. Any that aren't
478// are errors.
479//
Chris Lattner9232b992003-04-22 18:42:41 +0000480static void ResolveTypes(std::map<ValID, PATypeHolder> &LateResolveTypes) {
Chris Lattner4a42e902001-10-22 05:56:09 +0000481 if (!LateResolveTypes.empty()) {
Chris Lattner82269592001-10-22 06:01:08 +0000482 const ValID &DID = LateResolveTypes.begin()->first;
Chris Lattner4a42e902001-10-22 05:56:09 +0000483
484 if (DID.Type == ValID::NameVal)
Chris Lattner82269592001-10-22 06:01:08 +0000485 ThrowException("Reference to an invalid type: '" +DID.getName() + "'");
Chris Lattner4a42e902001-10-22 05:56:09 +0000486 else
Chris Lattner82269592001-10-22 06:01:08 +0000487 ThrowException("Reference to an invalid type: #" + itostr(DID.Num));
Chris Lattner30c89792001-09-07 16:35:17 +0000488 }
489}
490
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000491
Chris Lattner1781aca2001-09-18 04:00:54 +0000492// setValueName - Set the specified value to the name given. The name may be
493// null potentially, in which case this is a noop. The string passed in is
494// assumed to be a malloc'd string buffer, and is freed by this function.
495//
Chris Lattnerb7474512001-10-03 15:39:04 +0000496// This function returns true if the value has already been defined, but is
497// allowed to be redefined in the specified context. If the name is a new name
498// for the typeplane, false is returned.
499//
500static bool setValueName(Value *V, char *NameStr) {
501 if (NameStr == 0) return false;
Chris Lattner386a3b72001-10-16 19:54:17 +0000502
Chris Lattner9232b992003-04-22 18:42:41 +0000503 std::string Name(NameStr); // Copy string
Chris Lattner1781aca2001-09-18 04:00:54 +0000504 free(NameStr); // Free old string
505
Chris Lattner2079fde2001-10-13 06:41:08 +0000506 if (V->getType() == Type::VoidTy)
507 ThrowException("Can't assign name '" + Name +
508 "' to a null valued instruction!");
509
Chris Lattner6e6026b2002-11-20 18:36:02 +0000510 SymbolTable &ST = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000511 CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner6e6026b2002-11-20 18:36:02 +0000512 CurModule.CurrentModule->getSymbolTable();
Chris Lattner30c89792001-09-07 16:35:17 +0000513
Chris Lattner6e6026b2002-11-20 18:36:02 +0000514 Value *Existing = ST.lookup(V->getType(), Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000515 if (Existing) { // Inserting a name that is already defined???
516 // There is only one case where this is allowed: when we are refining an
517 // opaque type. In this case, Existing will be an opaque type.
Chris Lattner949a3622003-07-23 15:30:06 +0000518 if (const Type *Ty = dyn_cast<Type>(Existing)) {
Chris Lattner51727be2002-06-04 21:58:56 +0000519 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Ty)) {
Chris Lattner30c89792001-09-07 16:35:17 +0000520 // We ARE replacing an opaque type!
Chris Lattner51727be2002-06-04 21:58:56 +0000521 ((OpaqueType*)OpTy)->refineAbstractTypeTo(cast<Type>(V));
Chris Lattnerb7474512001-10-03 15:39:04 +0000522 return true;
Chris Lattner30c89792001-09-07 16:35:17 +0000523 }
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000524 }
Chris Lattner30c89792001-09-07 16:35:17 +0000525
Chris Lattner9636a912001-10-01 16:18:37 +0000526 // Otherwise, we are a simple redefinition of a value, check to see if it
527 // is defined the same as the old one...
Chris Lattneradf99702003-03-03 23:28:55 +0000528 if (const Type *Ty = dyn_cast<Type>(Existing)) {
529 if (Ty == cast<Type>(V)) return true; // Yes, it's equal.
Chris Lattner699f1eb2002-08-14 17:12:33 +0000530 // std::cerr << "Type: " << Ty->getDescription() << " != "
Chris Lattner949a3622003-07-23 15:30:06 +0000531 // << cast<Type>(V)->getDescription() << "!\n";
Chris Lattneradf99702003-03-03 23:28:55 +0000532 } else if (const Constant *C = dyn_cast<Constant>(Existing)) {
533 if (C == V) return true; // Constants are equal to themselves
Chris Lattnerb7474512001-10-03 15:39:04 +0000534 } else if (GlobalVariable *EGV = dyn_cast<GlobalVariable>(Existing)) {
Chris Lattner43efcbf2001-10-03 19:35:57 +0000535 // We are allowed to redefine a global variable in two circumstances:
536 // 1. If at least one of the globals is uninitialized or
537 // 2. If both initializers have the same value.
538 //
Chris Lattner89219832001-10-03 19:35:04 +0000539 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000540 if (!EGV->hasInitializer() || !GV->hasInitializer() ||
541 EGV->getInitializer() == GV->getInitializer()) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000542
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000543 // Make sure the existing global version gets the initializer! Make
544 // sure that it also gets marked const if the new version is.
Chris Lattner89219832001-10-03 19:35:04 +0000545 if (GV->hasInitializer() && !EGV->hasInitializer())
546 EGV->setInitializer(GV->getInitializer());
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000547 if (GV->isConstant())
548 EGV->setConstant(true);
Chris Lattner4ad02e72003-04-16 20:28:45 +0000549 EGV->setLinkage(GV->getLinkage());
Chris Lattner89219832001-10-03 19:35:04 +0000550
Chris Lattner2079fde2001-10-13 06:41:08 +0000551 delete GV; // Destroy the duplicate!
Chris Lattner89219832001-10-03 19:35:04 +0000552 return true; // They are equivalent!
553 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000554 }
Chris Lattner9636a912001-10-01 16:18:37 +0000555 }
Chris Lattner0b268fb2003-11-25 03:54:16 +0000556
Chris Lattner2079fde2001-10-13 06:41:08 +0000557 ThrowException("Redefinition of value named '" + Name + "' in the '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000558 V->getType()->getDescription() + "' type plane!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000559 }
Chris Lattner00950542001-06-06 20:29:01 +0000560
Chris Lattner15b69722003-11-12 04:40:30 +0000561 // Set the name
Chris Lattner6e6026b2002-11-20 18:36:02 +0000562 V->setName(Name, &ST);
Chris Lattner15b69722003-11-12 04:40:30 +0000563
564 // If we're in function scope
565 if (inFunctionScope()) {
566 // Look up the symbol in the function's local symboltable
567 Existing = CurFun.LocalSymtab.lookup(V->getType(),Name);
568
569 // If it already exists
570 if (Existing) {
Chris Lattner15b69722003-11-12 04:40:30 +0000571 // Bail
572 ThrowException("Redefinition of value named '" + Name + "' in the '" +
573 V->getType()->getDescription() + "' type plane!");
574
575 // otherwise, since it doesn't exist
576 } else {
577 // Insert it.
578 CurFun.LocalSymtab.insert(V);
579 }
580 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000581 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000582}
583
Chris Lattner8896eda2001-07-09 19:38:36 +0000584
Chris Lattner30c89792001-09-07 16:35:17 +0000585//===----------------------------------------------------------------------===//
586// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000587//
Chris Lattner8896eda2001-07-09 19:38:36 +0000588
Chris Lattner30c89792001-09-07 16:35:17 +0000589// TypeContains - Returns true if Ty contains E in it.
590//
591static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3ff43872001-09-28 22:56:31 +0000592 return find(df_begin(Ty), df_end(Ty), E) != df_end(Ty);
Chris Lattner30c89792001-09-07 16:35:17 +0000593}
Chris Lattner698b56e2001-07-20 19:15:08 +0000594
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000595// UpRefs - A list of the outstanding upreferences that need to be resolved.
Chris Lattner9232b992003-04-22 18:42:41 +0000596static std::vector<std::pair<unsigned, OpaqueType *> > UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000597
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000598/// HandleUpRefs - Every time we finish a new layer of types, this function is
599/// called. It loops through the UpRefs vector, which is a list of the
600/// currently active types. For each type, if the up reference is contained in
601/// the newly completed type, we decrement the level count. When the level
602/// count reaches zero, the upreferenced type is the type that is passed in:
603/// thus we can complete the cycle.
604///
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000605static PATypeHolder HandleUpRefs(const Type *ty) {
606 PATypeHolder Ty(ty);
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000607 UR_OUT("Type '" << Ty->getDescription() <<
Chris Lattner5084d032001-11-02 07:46:26 +0000608 "' newly formed. Resolving upreferences.\n" <<
609 UpRefs.size() << " upreferences active!\n");
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000610 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Chris Lattner5084d032001-11-02 07:46:26 +0000611 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Chris Lattner30c89792001-09-07 16:35:17 +0000612 << UpRefs[i].second->getDescription() << ") = "
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000613 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner30c89792001-09-07 16:35:17 +0000614 if (TypeContains(Ty, UpRefs[i].second)) {
615 unsigned Level = --UpRefs[i].first; // Decrement level of upreference
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000616 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Chris Lattner30c89792001-09-07 16:35:17 +0000617 if (Level == 0) { // Upreference should be resolved!
Chris Lattner5084d032001-11-02 07:46:26 +0000618 UR_OUT(" * Resolving upreference for "
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000619 << UpRefs[i].second->getDescription() << "\n";
Chris Lattner9232b992003-04-22 18:42:41 +0000620 std::string OldName = UpRefs[i].second->getDescription());
Chris Lattner30c89792001-09-07 16:35:17 +0000621 UpRefs[i].second->refineAbstractTypeTo(Ty);
Chris Lattner5084d032001-11-02 07:46:26 +0000622 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000623 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
624 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
625 --i; // Do not skip the next element...
Chris Lattner30c89792001-09-07 16:35:17 +0000626 }
627 }
Chris Lattner8896eda2001-07-09 19:38:36 +0000628 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000629
Chris Lattner8896eda2001-07-09 19:38:36 +0000630 return Ty;
631}
632
Chris Lattner30c89792001-09-07 16:35:17 +0000633
Chris Lattner00950542001-06-06 20:29:01 +0000634//===----------------------------------------------------------------------===//
635// RunVMAsmParser - Define an interface to this parser
636//===----------------------------------------------------------------------===//
637//
Chris Lattner9232b992003-04-22 18:42:41 +0000638Module *RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000639 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000640 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000641 llvmAsmlineno = 1; // Reset the current line number...
Chris Lattner99e7ab72003-10-18 05:53:13 +0000642 ObsoleteVarArgs = false;
Chris Lattner00950542001-06-06 20:29:01 +0000643
Chris Lattner75f20532003-04-22 18:02:52 +0000644 // Allocate a new module to read
645 CurModule.CurrentModule = new Module(Filename);
Chris Lattner42570982003-11-26 07:24:58 +0000646
647 try {
648 yyparse(); // Parse the file.
649 } catch (...) {
650 // Clear the symbol table so it doesn't complain when it
651 // gets destructed
652 CurFun.LocalSymtab.clear();
653 throw;
654 }
Chris Lattner99e7ab72003-10-18 05:53:13 +0000655
Chris Lattner00950542001-06-06 20:29:01 +0000656 Module *Result = ParserResult;
Chris Lattner99e7ab72003-10-18 05:53:13 +0000657
658 // Check to see if they called va_start but not va_arg..
659 if (!ObsoleteVarArgs)
660 if (Function *F = Result->getNamedFunction("llvm.va_start"))
661 if (F->asize() == 1) {
662 std::cerr << "WARNING: this file uses obsolete features. "
663 << "Assemble and disassemble to update it.\n";
664 ObsoleteVarArgs = true;
665 }
666
667
668 if (ObsoleteVarArgs) {
669 // If the user is making use of obsolete varargs intrinsics, adjust them for
670 // the user.
671 if (Function *F = Result->getNamedFunction("llvm.va_start")) {
672 assert(F->asize() == 1 && "Obsolete va_start takes 1 argument!");
673
674 const Type *RetTy = F->getFunctionType()->getParamType(0);
675 RetTy = cast<PointerType>(RetTy)->getElementType();
676 Function *NF = Result->getOrInsertFunction("llvm.va_start", RetTy, 0);
677
678 while (!F->use_empty()) {
679 CallInst *CI = cast<CallInst>(F->use_back());
680 Value *V = new CallInst(NF, "", CI);
681 new StoreInst(V, CI->getOperand(1), CI);
682 CI->getParent()->getInstList().erase(CI);
683 }
684 Result->getFunctionList().erase(F);
685 }
686
687 if (Function *F = Result->getNamedFunction("llvm.va_end")) {
688 assert(F->asize() == 1 && "Obsolete va_end takes 1 argument!");
689 const Type *ArgTy = F->getFunctionType()->getParamType(0);
690 ArgTy = cast<PointerType>(ArgTy)->getElementType();
691 Function *NF = Result->getOrInsertFunction("llvm.va_end", Type::VoidTy,
692 ArgTy, 0);
693
694 while (!F->use_empty()) {
695 CallInst *CI = cast<CallInst>(F->use_back());
696 Value *V = new LoadInst(CI->getOperand(1), "", CI);
697 new CallInst(NF, V, "", CI);
698 CI->getParent()->getInstList().erase(CI);
699 }
700 Result->getFunctionList().erase(F);
701 }
702
703 if (Function *F = Result->getNamedFunction("llvm.va_copy")) {
704 assert(F->asize() == 2 && "Obsolete va_copy takes 2 argument!");
705 const Type *ArgTy = F->getFunctionType()->getParamType(0);
706 ArgTy = cast<PointerType>(ArgTy)->getElementType();
707 Function *NF = Result->getOrInsertFunction("llvm.va_copy", ArgTy,
708 ArgTy, 0);
709
710 while (!F->use_empty()) {
711 CallInst *CI = cast<CallInst>(F->use_back());
712 Value *V = new CallInst(NF, CI->getOperand(2), "", CI);
713 new StoreInst(V, CI->getOperand(1), CI);
714 CI->getParent()->getInstList().erase(CI);
715 }
716 Result->getFunctionList().erase(F);
717 }
718 }
719
Chris Lattner00950542001-06-06 20:29:01 +0000720 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
721 ParserResult = 0;
722
723 return Result;
724}
725
Brian Gaeked0fde302003-11-11 22:41:34 +0000726} // End llvm namespace
727
728using namespace llvm;
729
Chris Lattner00950542001-06-06 20:29:01 +0000730%}
731
732%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000733 llvm::Module *ModuleVal;
734 llvm::Function *FunctionVal;
735 std::pair<llvm::PATypeHolder*, char*> *ArgVal;
736 llvm::BasicBlock *BasicBlockVal;
737 llvm::TerminatorInst *TermInstVal;
738 llvm::Instruction *InstVal;
739 llvm::Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000740
Brian Gaeked0fde302003-11-11 22:41:34 +0000741 const llvm::Type *PrimType;
742 llvm::PATypeHolder *TypeVal;
743 llvm::Value *ValueVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000744
Brian Gaeked0fde302003-11-11 22:41:34 +0000745 std::vector<std::pair<llvm::PATypeHolder*,char*> > *ArgList;
746 std::vector<llvm::Value*> *ValueList;
747 std::list<llvm::PATypeHolder> *TypeList;
748 std::list<std::pair<llvm::Value*,
749 llvm::BasicBlock*> > *PHIList; // Represent the RHS of PHI node
750 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
751 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000752
Brian Gaeked0fde302003-11-11 22:41:34 +0000753 llvm::GlobalValue::LinkageTypes Linkage;
Chris Lattner30c89792001-09-07 16:35:17 +0000754 int64_t SInt64Val;
755 uint64_t UInt64Val;
756 int SIntVal;
757 unsigned UIntVal;
758 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000759 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000760
Chris Lattner30c89792001-09-07 16:35:17 +0000761 char *StrVal; // This memory is strdup'd!
Brian Gaeked0fde302003-11-11 22:41:34 +0000762 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000763
Brian Gaeked0fde302003-11-11 22:41:34 +0000764 llvm::Instruction::BinaryOps BinaryOpVal;
765 llvm::Instruction::TermOps TermOpVal;
766 llvm::Instruction::MemoryOps MemOpVal;
767 llvm::Instruction::OtherOps OtherOpVal;
768 llvm::Module::Endianness Endianness;
Chris Lattner00950542001-06-06 20:29:01 +0000769}
770
Chris Lattner79df7c02002-03-26 18:01:55 +0000771%type <ModuleVal> Module FunctionList
772%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000773%type <BasicBlockVal> BasicBlock InstructionList
774%type <TermInstVal> BBTerminatorInst
775%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000776%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000777%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000778%type <ArgList> ArgList ArgListH
779%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000780%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000781%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000782%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000783%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000784%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +0000785%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner15c9c032003-09-08 18:20:29 +0000786%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattner4ad02e72003-04-16 20:28:45 +0000787%type <Linkage> OptLinkage
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000788%type <Endianness> BigOrLittle
Chris Lattner00950542001-06-06 20:29:01 +0000789
Chris Lattner2079fde2001-10-13 06:41:08 +0000790// ValueRef - Unresolved reference to a definition or BB
791%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000792%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000793// Tokens and types for handling constant integer values
794//
795// ESINT64VAL - A negative number within long long range
796%token <SInt64Val> ESINT64VAL
797
798// EUINT64VAL - A positive number within uns. long long range
799%token <UInt64Val> EUINT64VAL
800%type <SInt64Val> EINT64VAL
801
802%token <SIntVal> SINTVAL // Signed 32 bit ints...
803%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
804%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000805%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000806
807// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000808%type <TypeVal> Types TypesV UpRTypes UpRTypesV
809%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000810%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
811%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000812
Chris Lattner6c23f572003-08-22 05:42:10 +0000813%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
814%type <StrVal> Name OptName OptAssign
Chris Lattner00950542001-06-06 20:29:01 +0000815
816
Chris Lattnerf4600482003-06-28 20:01:34 +0000817%token IMPLEMENTATION ZEROINITIALIZER TRUE FALSE BEGINTOK ENDTOK
Chris Lattner15c9c032003-09-08 18:20:29 +0000818%token DECLARE GLOBAL CONSTANT VOLATILE
Chris Lattnerf797cab2003-10-10 04:54:02 +0000819%token TO EXCEPT DOTDOTDOT NULL_TOK CONST INTERNAL LINKONCE WEAK APPENDING
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000820%token OPAQUE NOT EXTERNAL TARGET ENDIAN POINTERSIZE LITTLE BIG
Chris Lattner00950542001-06-06 20:29:01 +0000821
822// Basic Block Terminating Operators
Chris Lattner36143fc2003-09-08 18:54:55 +0000823%token <TermOpVal> RET BR SWITCH INVOKE UNWIND
Chris Lattner00950542001-06-06 20:29:01 +0000824
Chris Lattner00950542001-06-06 20:29:01 +0000825// Binary Operators
826%type <BinaryOpVal> BinaryOps // all the binary operators
Chris Lattner4a6482b2002-09-10 19:57:26 +0000827%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattner42c9e772001-10-20 09:32:59 +0000828%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000829%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000830
831// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000832%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000833
Chris Lattner027dcc52001-07-08 21:10:27 +0000834// Other Operators
835%type <OtherOpVal> ShiftOps
Chris Lattner3b237fc2003-10-19 21:34:28 +0000836%token <OtherOpVal> PHI_TOK CALL CAST SHL SHR VAARG VANEXT
Chris Lattner99e7ab72003-10-18 05:53:13 +0000837%token VA_ARG // FIXME: OBSOLETE
Chris Lattner027dcc52001-07-08 21:10:27 +0000838
Chris Lattner00950542001-06-06 20:29:01 +0000839%start Module
840%%
841
842// Handle constant integer size restriction and conversion...
843//
Chris Lattner51727be2002-06-04 21:58:56 +0000844INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +0000845INTVAL : UINTVAL {
846 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
847 ThrowException("Value too large for type!");
848 $$ = (int32_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000849};
Chris Lattner00950542001-06-06 20:29:01 +0000850
851
Chris Lattner51727be2002-06-04 21:58:56 +0000852EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +0000853EINT64VAL : EUINT64VAL {
854 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
855 ThrowException("Value too large for type!");
856 $$ = (int64_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000857};
Chris Lattner00950542001-06-06 20:29:01 +0000858
Chris Lattner00950542001-06-06 20:29:01 +0000859// Operations that are notably excluded from this list include:
860// RET, BR, & SWITCH because they end basic blocks and are treated specially.
861//
Chris Lattner4a6482b2002-09-10 19:57:26 +0000862ArithmeticOps: ADD | SUB | MUL | DIV | REM;
863LogicalOps : AND | OR | XOR;
864SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
865BinaryOps : ArithmeticOps | LogicalOps | SetCondOps;
866
Chris Lattner51727be2002-06-04 21:58:56 +0000867ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +0000868
Chris Lattnere98dda62001-07-14 06:10:16 +0000869// These are some types that allow classification if we only want a particular
870// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +0000871SIntType : LONG | INT | SHORT | SBYTE;
872UIntType : ULONG | UINT | USHORT | UBYTE;
873IntType : SIntType | UIntType;
874FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +0000875
Chris Lattnere98dda62001-07-14 06:10:16 +0000876// OptAssign - Value producing statements have an optional assignment component
Chris Lattner6c23f572003-08-22 05:42:10 +0000877OptAssign : Name '=' {
Chris Lattner00950542001-06-06 20:29:01 +0000878 $$ = $1;
879 }
880 | /*empty*/ {
881 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +0000882 };
Chris Lattner00950542001-06-06 20:29:01 +0000883
Chris Lattner4ad02e72003-04-16 20:28:45 +0000884OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
885 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
Chris Lattner72ac148d2003-10-16 18:29:00 +0000886 WEAK { $$ = GlobalValue::WeakLinkage; } |
Chris Lattner4ad02e72003-04-16 20:28:45 +0000887 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
888 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattner30c89792001-09-07 16:35:17 +0000889
890//===----------------------------------------------------------------------===//
891// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +0000892// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +0000893// access to it, a user must explicitly use TypesV.
894//
895
896// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +0000897TypesV : Types | VOID { $$ = new PATypeHolder($1); };
898UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +0000899
900Types : UpRTypes {
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000901 if (!UpRefs.empty())
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000902 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
903 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000904 };
Chris Lattner30c89792001-09-07 16:35:17 +0000905
906
907// Derived types are added later...
908//
Chris Lattner51727be2002-06-04 21:58:56 +0000909PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
910PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000911UpRTypes : OPAQUE {
912 $$ = new PATypeHolder(OpaqueType::get());
913 }
914 | PrimType {
915 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +0000916 };
Chris Lattnerd78700d2002-08-16 21:14:40 +0000917UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000918 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +0000919};
Chris Lattner30c89792001-09-07 16:35:17 +0000920
Chris Lattner30c89792001-09-07 16:35:17 +0000921// Include derived types in the Types production.
922//
923UpRTypes : '\\' EUINT64VAL { // Type UpReference
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000924 if ($2 > (uint64_t)~0U) ThrowException("Value out of range!");
Chris Lattner30c89792001-09-07 16:35:17 +0000925 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner9232b992003-04-22 18:42:41 +0000926 UpRefs.push_back(std::make_pair((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000927 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +0000928 UR_OUT("New Upreference!\n");
929 }
Chris Lattner79df7c02002-03-26 18:01:55 +0000930 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner9232b992003-04-22 18:42:41 +0000931 std::vector<const Type*> Params;
Chris Lattner697954c2002-01-20 22:54:45 +0000932 mapto($3->begin(), $3->end(), std::back_inserter(Params),
Chris Lattner876dc572003-10-02 19:00:34 +0000933 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner2079fde2001-10-13 06:41:08 +0000934 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
935 if (isVarArg) Params.pop_back();
936
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000937 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +0000938 delete $3; // Delete the argument list
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000939 delete $1; // Delete the return type handle
Chris Lattner30c89792001-09-07 16:35:17 +0000940 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000941 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000942 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000943 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +0000944 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000945 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +0000946 std::vector<const Type*> Elements;
Chris Lattner697954c2002-01-20 22:54:45 +0000947 mapto($2->begin(), $2->end(), std::back_inserter(Elements),
Chris Lattner876dc572003-10-02 19:00:34 +0000948 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner30c89792001-09-07 16:35:17 +0000949
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000950 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000951 delete $2;
952 }
953 | '{' '}' { // Empty structure type?
Chris Lattner9232b992003-04-22 18:42:41 +0000954 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000955 }
956 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000957 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000958 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000959 };
Chris Lattner30c89792001-09-07 16:35:17 +0000960
Chris Lattner7e708292002-06-25 16:13:24 +0000961// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +0000962// declaration type lists
963//
964TypeListI : UpRTypes {
Chris Lattner9232b992003-04-22 18:42:41 +0000965 $$ = new std::list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +0000966 $$->push_back(*$1); delete $1;
967 }
968 | TypeListI ',' UpRTypes {
969 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +0000970 };
Chris Lattner30c89792001-09-07 16:35:17 +0000971
Chris Lattner7e708292002-06-25 16:13:24 +0000972// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +0000973ArgTypeListI : TypeListI
974 | TypeListI ',' DOTDOTDOT {
975 ($$=$1)->push_back(Type::VoidTy);
976 }
977 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +0000978 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +0000979 }
980 | /*empty*/ {
Chris Lattner9232b992003-04-22 18:42:41 +0000981 $$ = new std::list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +0000982 };
Chris Lattner30c89792001-09-07 16:35:17 +0000983
Chris Lattnere98dda62001-07-14 06:10:16 +0000984// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +0000985// production is used ONLY to represent constants that show up AFTER a 'const',
986// 'constant' or 'global' token at global scope. Constants that can be inlined
987// into other expressions (such as integers and constexprs) are handled by the
988// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +0000989//
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000990ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Chris Lattner949a3622003-07-23 15:30:06 +0000991 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000992 if (ATy == 0)
993 ThrowException("Cannot make array constant with type: '" +
994 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000995 const Type *ETy = ATy->getElementType();
996 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +0000997
Chris Lattner30c89792001-09-07 16:35:17 +0000998 // Verify that we have the correct size...
999 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +00001000 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +00001001 utostr($3->size()) + " arguments, but has size of " +
1002 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +00001003
Chris Lattner30c89792001-09-07 16:35:17 +00001004 // Verify all elements are correct type!
1005 for (unsigned i = 0; i < $3->size(); i++) {
1006 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +00001007 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001008 ETy->getDescription() +"' as required!\nIt is of type '"+
1009 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +00001010 }
1011
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001012 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001013 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001014 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001015 | Types '[' ']' {
Chris Lattner949a3622003-07-23 15:30:06 +00001016 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001017 if (ATy == 0)
1018 ThrowException("Cannot make array constant with type: '" +
1019 (*$1)->getDescription() + "'!");
1020
1021 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +00001022 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +00001023 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +00001024 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattner9232b992003-04-22 18:42:41 +00001025 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +00001026 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001027 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001028 | Types 'c' STRINGCONSTANT {
Chris Lattner949a3622003-07-23 15:30:06 +00001029 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001030 if (ATy == 0)
1031 ThrowException("Cannot make array constant with type: '" +
1032 (*$1)->getDescription() + "'!");
1033
Chris Lattner30c89792001-09-07 16:35:17 +00001034 int NumElements = ATy->getNumElements();
1035 const Type *ETy = ATy->getElementType();
1036 char *EndStr = UnEscapeLexed($3, true);
1037 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +00001038 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +00001039 itostr((int)(EndStr-$3)) +
1040 " when array has size " + itostr(NumElements) + "!");
Chris Lattner9232b992003-04-22 18:42:41 +00001041 std::vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +00001042 if (ETy == Type::SByteTy) {
1043 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001044 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +00001045 } else if (ETy == Type::UByteTy) {
1046 for (char *C = $3; C != EndStr; ++C)
Chris Lattnerbae362f2003-01-30 22:24:26 +00001047 Vals.push_back(ConstantUInt::get(ETy, (unsigned char)*C));
Chris Lattner93750fa2001-07-28 17:48:55 +00001048 } else {
Chris Lattner30c89792001-09-07 16:35:17 +00001049 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +00001050 ThrowException("Cannot build string arrays of non byte sized elements!");
1051 }
Chris Lattner30c89792001-09-07 16:35:17 +00001052 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001053 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +00001054 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001055 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001056 | Types '{' ConstVector '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001057 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001058 if (STy == 0)
1059 ThrowException("Cannot make struct constant with type: '" +
1060 (*$1)->getDescription() + "'!");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001061
Chris Lattnerc6212f12003-05-21 16:06:56 +00001062 if ($3->size() != STy->getNumContainedTypes())
1063 ThrowException("Illegal number of initializers for structure type!");
1064
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001065 // Check to ensure that constants are compatible with the type initializer!
1066 for (unsigned i = 0, e = $3->size(); i != e; ++i)
1067 if ((*$3)[i]->getType() != STy->getElementTypes()[i])
1068 ThrowException("Expected type '" +
1069 STy->getElementTypes()[i]->getDescription() +
1070 "' for element #" + utostr(i) +
1071 " of structure initializer!");
1072
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001073 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001074 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001075 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001076 | Types '{' '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001077 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001078 if (STy == 0)
1079 ThrowException("Cannot make struct constant with type: '" +
1080 (*$1)->getDescription() + "'!");
1081
1082 if (STy->getNumContainedTypes() != 0)
1083 ThrowException("Illegal number of initializers for structure type!");
1084
1085 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1086 delete $1;
1087 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001088 | Types NULL_TOK {
Chris Lattner949a3622003-07-23 15:30:06 +00001089 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001090 if (PTy == 0)
1091 ThrowException("Cannot make null pointer constant with type: '" +
1092 (*$1)->getDescription() + "'!");
1093
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001094 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001095 delete $1;
1096 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001097 | Types SymbolicValueRef {
Chris Lattner949a3622003-07-23 15:30:06 +00001098 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001099 if (Ty == 0)
1100 ThrowException("Global const reference must be a pointer type!");
1101
Chris Lattner3101c252002-08-15 17:58:33 +00001102 // ConstExprs can exist in the body of a function, thus creating
1103 // ConstantPointerRefs whenever they refer to a variable. Because we are in
1104 // the context of a function, getValNonImprovising will search the functions
1105 // symbol table instead of the module symbol table for the global symbol,
1106 // which throws things all off. To get around this, we just tell
1107 // getValNonImprovising that we are at global scope here.
1108 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001109 Function *SavedCurFn = CurFun.CurrentFunction;
1110 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001111
Chris Lattner2079fde2001-10-13 06:41:08 +00001112 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001113
Chris Lattner394f2eb2003-10-16 20:12:13 +00001114 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001115
Chris Lattner2079fde2001-10-13 06:41:08 +00001116 // If this is an initializer for a constant pointer, which is referencing a
1117 // (currently) undefined variable, create a stub now that shall be replaced
1118 // in the future with the right type of variable.
1119 //
1120 if (V == 0) {
1121 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1122 const PointerType *PT = cast<PointerType>(Ty);
1123
1124 // First check to see if the forward references value is already created!
1125 PerModuleInfo::GlobalRefsType::iterator I =
Chris Lattner9232b992003-04-22 18:42:41 +00001126 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001127
1128 if (I != CurModule.GlobalRefs.end()) {
1129 V = I->second; // Placeholder already exists, use it...
Chris Lattnera989b232003-12-23 20:05:15 +00001130 $2.destroy();
Chris Lattner2079fde2001-10-13 06:41:08 +00001131 } else {
Chris Lattner2079fde2001-10-13 06:41:08 +00001132 // Create a placeholder for the global variable reference...
Chris Lattner7a176752001-12-04 00:03:30 +00001133 GlobalVariable *GV = new GlobalVariable(PT->getElementType(),
Chris Lattner4ad02e72003-04-16 20:28:45 +00001134 false,
1135 GlobalValue::ExternalLinkage);
Chris Lattner2079fde2001-10-13 06:41:08 +00001136 // Keep track of the fact that we have a forward ref to recycle it
Chris Lattner9232b992003-04-22 18:42:41 +00001137 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
Chris Lattner2079fde2001-10-13 06:41:08 +00001138
1139 // Must temporarily push this value into the module table...
1140 CurModule.CurrentModule->getGlobalList().push_back(GV);
1141 V = GV;
1142 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001143 }
1144
Chris Lattner2079fde2001-10-13 06:41:08 +00001145 GlobalValue *GV = cast<GlobalValue>(V);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001146 $$ = ConstantPointerRef::get(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001147 delete $1; // Free the type handle
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001148 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001149 | Types ConstExpr {
1150 if ($1->get() != $2->getType())
1151 ThrowException("Mismatched types for constant expression!");
1152 $$ = $2;
1153 delete $1;
Chris Lattnerf4600482003-06-28 20:01:34 +00001154 }
1155 | Types ZEROINITIALIZER {
1156 $$ = Constant::getNullValue($1->get());
1157 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001158 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001159
Chris Lattnere43f40b2002-10-09 00:25:32 +00001160ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001161 if (!ConstantSInt::isValueValidForType($1, $2))
1162 ThrowException("Constant value doesn't fit in type!");
1163 $$ = ConstantSInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001164 }
1165 | UIntType EUINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001166 if (!ConstantUInt::isValueValidForType($1, $2))
1167 ThrowException("Constant value doesn't fit in type!");
1168 $$ = ConstantUInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001169 }
1170 | BOOL TRUE { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001171 $$ = ConstantBool::True;
1172 }
Chris Lattnere43f40b2002-10-09 00:25:32 +00001173 | BOOL FALSE { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001174 $$ = ConstantBool::False;
1175 }
1176 | FPType FPVAL { // Float & Double constants
1177 $$ = ConstantFP::get($1, $2);
1178 };
1179
Chris Lattner00950542001-06-06 20:29:01 +00001180
Chris Lattnerd78700d2002-08-16 21:14:40 +00001181ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattnerae711a82003-11-21 20:27:35 +00001182 if (!$3->getType()->isFirstClassType())
1183 ThrowException("cast constant expression from a non-primitive type: '" +
1184 $3->getType()->getDescription() + "'!");
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001185 if (!$5->get()->isFirstClassType())
1186 ThrowException("cast constant expression to a non-primitive type: '" +
1187 $5->get()->getDescription() + "'!");
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001188 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001189 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001190 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001191 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1192 if (!isa<PointerType>($3->getType()))
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001193 ThrowException("GetElementPtr requires a pointer operand!");
1194
1195 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001196 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001197 if (!IdxTy)
1198 ThrowException("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001199
Chris Lattner9232b992003-04-22 18:42:41 +00001200 std::vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001201 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1202 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001203 IdxVec.push_back(C);
1204 else
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001205 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001206
Chris Lattnerd78700d2002-08-16 21:14:40 +00001207 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001208
Chris Lattnerd78700d2002-08-16 21:14:40 +00001209 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001210 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001211 | BinaryOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001212 if ($3->getType() != $5->getType())
1213 ThrowException("Binary operator types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001214 $$ = ConstantExpr::get($1, $3, $5);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001215 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001216 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001217 if ($5->getType() != Type::UByteTy)
1218 ThrowException("Shift count for shift constant must be unsigned byte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001219 if (!$3->getType()->isInteger())
1220 ThrowException("Shift constant expression requires integer operand!");
Chris Lattner5e458e22003-05-21 17:48:56 +00001221 $$ = ConstantExpr::getShift($1, $3, $5);
Chris Lattner699f1eb2002-08-14 17:12:33 +00001222 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001223
1224
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001225// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001226ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001227 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001228 }
1229 | ConstVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001230 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001231 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001232 };
Chris Lattner00950542001-06-06 20:29:01 +00001233
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001234
Chris Lattner1781aca2001-09-18 04:00:54 +00001235// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001236GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001237
Chris Lattner00950542001-06-06 20:29:01 +00001238
Chris Lattner0e73ce62002-05-02 19:11:13 +00001239//===----------------------------------------------------------------------===//
1240// Rules to match Modules
1241//===----------------------------------------------------------------------===//
1242
1243// Module rule: Capture the result of parsing the whole file into a result
1244// variable...
1245//
1246Module : FunctionList {
1247 $$ = ParserResult = $1;
1248 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001249};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001250
Chris Lattner7e708292002-06-25 16:13:24 +00001251// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001252//
1253FunctionList : FunctionList Function {
1254 $$ = $1;
1255 assert($2->getParent() == 0 && "Function already in module!");
1256 $1->getFunctionList().push_back($2);
Chris Lattner394f2eb2003-10-16 20:12:13 +00001257 CurFun.FunctionDone();
Chris Lattner0e73ce62002-05-02 19:11:13 +00001258 }
1259 | FunctionList FunctionProto {
1260 $$ = $1;
1261 }
1262 | FunctionList IMPLEMENTATION {
1263 $$ = $1;
1264 }
1265 | ConstPool {
1266 $$ = CurModule.CurrentModule;
1267 // Resolve circular types before we parse the body of the module
1268 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001269 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001270
Chris Lattnere98dda62001-07-14 06:10:16 +00001271// ConstPool - Constants with optional names assigned to them.
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001272ConstPool : ConstPool OptAssign CONST ConstVal {
Chris Lattneradf99702003-03-03 23:28:55 +00001273 if (!setValueName($4, $2))
1274 InsertValue($4);
Chris Lattner00950542001-06-06 20:29:01 +00001275 }
Chris Lattner30c89792001-09-07 16:35:17 +00001276 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner4a42e902001-10-22 05:56:09 +00001277 // Eagerly resolve types. This is not an optimization, this is a
1278 // requirement that is due to the fact that we could have this:
1279 //
1280 // %list = type { %list * }
1281 // %list = type { %list * } ; repeated type decl
1282 //
1283 // If types are not resolved eagerly, then the two types will not be
1284 // determined to be the same type!
1285 //
1286 ResolveTypeTo($2, $4->get());
1287
Chris Lattner1781aca2001-09-18 04:00:54 +00001288 // TODO: FIXME when Type are not const
Chris Lattnerb7474512001-10-03 15:39:04 +00001289 if (!setValueName(const_cast<Type*>($4->get()), $2)) {
1290 // If this is not a redefinition of a type...
1291 if (!$2) {
1292 InsertType($4->get(),
Chris Lattner394f2eb2003-10-16 20:12:13 +00001293 inFunctionScope() ? CurFun.Types : CurModule.Types);
Chris Lattnerb7474512001-10-03 15:39:04 +00001294 }
Chris Lattner30c89792001-09-07 16:35:17 +00001295 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001296
1297 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001298 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001299 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001300 }
Chris Lattner4ad02e72003-04-16 20:28:45 +00001301 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Chris Lattnerdda71962001-11-26 18:54:16 +00001302 const Type *Ty = $5->getType();
Chris Lattner1781aca2001-09-18 04:00:54 +00001303 // Global declarations appear in Constant Pool
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001304 Constant *Initializer = $5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001305 if (Initializer == 0)
1306 ThrowException("Global value initializer is not a constant!");
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001307
Chris Lattnerdda71962001-11-26 18:54:16 +00001308 GlobalVariable *GV = new GlobalVariable(Ty, $4, $3, Initializer);
Chris Lattnerb7474512001-10-03 15:39:04 +00001309 if (!setValueName(GV, $2)) { // If not redefining...
1310 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001311 int Slot = InsertValue(GV, CurModule.Values);
1312
1313 if (Slot != -1) {
1314 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1315 } else {
1316 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1317 (char*)GV->getName().c_str()));
1318 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001319 }
Chris Lattner1781aca2001-09-18 04:00:54 +00001320 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001321 | ConstPool OptAssign EXTERNAL GlobalType Types {
1322 const Type *Ty = *$5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001323 // Global declarations appear in Constant Pool
Chris Lattner4ad02e72003-04-16 20:28:45 +00001324 GlobalVariable *GV = new GlobalVariable(Ty,$4,GlobalValue::ExternalLinkage);
Chris Lattnerb7474512001-10-03 15:39:04 +00001325 if (!setValueName(GV, $2)) { // If not redefining...
1326 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001327 int Slot = InsertValue(GV, CurModule.Values);
1328
1329 if (Slot != -1) {
1330 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1331 } else {
1332 assert(GV->hasName() && "Not named and not numbered!?");
1333 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1334 (char*)GV->getName().c_str()));
1335 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001336 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001337 delete $5;
Chris Lattnere98dda62001-07-14 06:10:16 +00001338 }
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001339 | ConstPool TARGET TargetDefinition {
1340 }
Chris Lattner00950542001-06-06 20:29:01 +00001341 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001342 };
Chris Lattner00950542001-06-06 20:29:01 +00001343
1344
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001345
1346BigOrLittle : BIG { $$ = Module::BigEndian; };
1347BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1348
1349TargetDefinition : ENDIAN '=' BigOrLittle {
1350 CurModule.CurrentModule->setEndianness($3);
1351 }
1352 | POINTERSIZE '=' EUINT64VAL {
1353 if ($3 == 32)
1354 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1355 else if ($3 == 64)
1356 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1357 else
1358 ThrowException("Invalid pointer size: '" + utostr($3) + "'!");
1359 };
1360
1361
Chris Lattner00950542001-06-06 20:29:01 +00001362//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001363// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001364//===----------------------------------------------------------------------===//
1365
Chris Lattner6c23f572003-08-22 05:42:10 +00001366Name : VAR_ID | STRINGCONSTANT;
1367OptName : Name | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001368
Chris Lattner6c23f572003-08-22 05:42:10 +00001369ArgVal : Types OptName {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001370 if (*$1 == Type::VoidTy)
1371 ThrowException("void typed arguments are invalid!");
Chris Lattner9232b992003-04-22 18:42:41 +00001372 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
Chris Lattner51727be2002-06-04 21:58:56 +00001373};
Chris Lattner00950542001-06-06 20:29:01 +00001374
Chris Lattner69da5cf2002-10-13 20:57:00 +00001375ArgListH : ArgListH ',' ArgVal {
1376 $$ = $1;
1377 $1->push_back(*$3);
1378 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001379 }
1380 | ArgVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001381 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
Chris Lattner69da5cf2002-10-13 20:57:00 +00001382 $$->push_back(*$1);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001383 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001384 };
Chris Lattner00950542001-06-06 20:29:01 +00001385
1386ArgList : ArgListH {
1387 $$ = $1;
1388 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001389 | ArgListH ',' DOTDOTDOT {
1390 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001391 $$->push_back(std::pair<PATypeHolder*,
1392 char*>(new PATypeHolder(Type::VoidTy), 0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001393 }
1394 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001395 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1396 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001397 }
Chris Lattner00950542001-06-06 20:29:01 +00001398 | /* empty */ {
1399 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001400 };
Chris Lattner00950542001-06-06 20:29:01 +00001401
Chris Lattner6c23f572003-08-22 05:42:10 +00001402FunctionHeaderH : TypesV Name '(' ArgList ')' {
Chris Lattner1f862af2003-04-16 18:13:57 +00001403 UnEscapeLexed($2);
Chris Lattner9232b992003-04-22 18:42:41 +00001404 std::string FunctionName($2);
Chris Lattnerdda71962001-11-26 18:54:16 +00001405
Chris Lattnerc282f5a2003-11-21 22:32:23 +00001406 if (!(*$1)->isFirstClassType() && *$1 != Type::VoidTy)
1407 ThrowException("LLVM functions cannot return aggregate types!");
1408
Chris Lattner9232b992003-04-22 18:42:41 +00001409 std::vector<const Type*> ParamTypeList;
Chris Lattner1f862af2003-04-16 18:13:57 +00001410 if ($4) { // If there are arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001411 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001412 I != $4->end(); ++I)
Chris Lattner69da5cf2002-10-13 20:57:00 +00001413 ParamTypeList.push_back(I->first->get());
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001414 }
Chris Lattner00950542001-06-06 20:29:01 +00001415
Chris Lattner2079fde2001-10-13 06:41:08 +00001416 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1417 if (isVarArg) ParamTypeList.pop_back();
1418
Chris Lattner1f862af2003-04-16 18:13:57 +00001419 const FunctionType *FT = FunctionType::get(*$1, ParamTypeList, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001420 const PointerType *PFT = PointerType::get(FT);
Chris Lattner1f862af2003-04-16 18:13:57 +00001421 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001422
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001423 Function *Fn = 0;
1424 // Is the function already in symtab?
1425 if ((Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1426 // Yes it is. If this is the case, either we need to be a forward decl,
1427 // or it needs to be.
Chris Lattner394f2eb2003-10-16 20:12:13 +00001428 if (!CurFun.isDeclare && !Fn->isExternal())
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001429 ThrowException("Redefinition of function '" + FunctionName + "'!");
1430
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001431 // If we found a preexisting function prototype, remove it from the
1432 // module, so that we don't get spurious conflicts with global & local
1433 // variables.
1434 //
1435 CurModule.CurrentModule->getFunctionList().remove(Fn);
Chris Lattner34538142002-03-08 19:11:42 +00001436
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001437 // Make sure to strip off any argument names so we can't get conflicts...
1438 for (Function::aiterator AI = Fn->abegin(), AE = Fn->aend(); AI != AE; ++AI)
1439 AI->setName("");
Chris Lattner5659dd12002-07-15 00:10:33 +00001440
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001441 } else { // Not already defined?
Chris Lattner4ad02e72003-04-16 20:28:45 +00001442 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001443 InsertValue(Fn, CurModule.Values);
Chris Lattner1f862af2003-04-16 18:13:57 +00001444 CurModule.DeclareNewGlobalValue(Fn, ValID::create($2));
Chris Lattnere1815642001-07-15 06:35:53 +00001445 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001446 free($2); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001447
Chris Lattner394f2eb2003-10-16 20:12:13 +00001448 CurFun.FunctionStart(Fn);
Chris Lattner00950542001-06-06 20:29:01 +00001449
Chris Lattner7e708292002-06-25 16:13:24 +00001450 // Add all of the arguments we parsed to the function...
Chris Lattner1f862af2003-04-16 18:13:57 +00001451 if ($4) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001452 if (isVarArg) { // Nuke the last entry
Chris Lattner1f862af2003-04-16 18:13:57 +00001453 assert($4->back().first->get() == Type::VoidTy && $4->back().second == 0&&
Chris Lattner69da5cf2002-10-13 20:57:00 +00001454 "Not a varargs marker!");
Chris Lattner1f862af2003-04-16 18:13:57 +00001455 delete $4->back().first;
1456 $4->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00001457 }
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001458 Function::aiterator ArgIt = Fn->abegin();
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, ++ArgIt) {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001461 delete I->first; // Delete the typeholder...
1462
1463 if (setValueName(ArgIt, I->second)) // Insert arg into symtab...
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001464 assert(0 && "No arg redef allowed!");
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001465
Chris Lattner69da5cf2002-10-13 20:57:00 +00001466 InsertValue(ArgIt);
Chris Lattner00950542001-06-06 20:29:01 +00001467 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001468
Chris Lattner1f862af2003-04-16 18:13:57 +00001469 delete $4; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00001470 }
Chris Lattner51727be2002-06-04 21:58:56 +00001471};
Chris Lattner00950542001-06-06 20:29:01 +00001472
Chris Lattner9b02cc32002-05-03 18:23:48 +00001473BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1474
Chris Lattner4ad02e72003-04-16 20:28:45 +00001475FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00001476 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001477
Chris Lattner4ad02e72003-04-16 20:28:45 +00001478 // Make sure that we keep track of the linkage type even if there was a
1479 // previous "declare".
1480 $$->setLinkage($1);
Chris Lattner1f862af2003-04-16 18:13:57 +00001481
Chris Lattner7e708292002-06-25 16:13:24 +00001482 // Resolve circular types before we parse the body of the function.
Chris Lattner394f2eb2003-10-16 20:12:13 +00001483 ResolveTypes(CurFun.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001484};
Chris Lattner00950542001-06-06 20:29:01 +00001485
Chris Lattner9b02cc32002-05-03 18:23:48 +00001486END : ENDTOK | '}'; // Allow end of '}' to end a function
1487
Chris Lattner79df7c02002-03-26 18:01:55 +00001488Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001489 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001490};
Chris Lattner00950542001-06-06 20:29:01 +00001491
Chris Lattner394f2eb2003-10-16 20:12:13 +00001492FunctionProto : DECLARE { CurFun.isDeclare = true; } FunctionHeaderH {
1493 $$ = CurFun.CurrentFunction;
Chris Lattner79df7c02002-03-26 18:01:55 +00001494 assert($$->getParent() == 0 && "Function already in module!");
1495 CurModule.CurrentModule->getFunctionList().push_back($$);
Chris Lattner394f2eb2003-10-16 20:12:13 +00001496 CurFun.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001497};
Chris Lattner00950542001-06-06 20:29:01 +00001498
1499//===----------------------------------------------------------------------===//
1500// Rules to match Basic Blocks
1501//===----------------------------------------------------------------------===//
1502
1503ConstValueRef : ESINT64VAL { // A reference to a direct constant
1504 $$ = ValID::create($1);
1505 }
1506 | EUINT64VAL {
1507 $$ = ValID::create($1);
1508 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001509 | FPVAL { // Perhaps it's an FP constant?
1510 $$ = ValID::create($1);
1511 }
Chris Lattner00950542001-06-06 20:29:01 +00001512 | TRUE {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001513 $$ = ValID::create(ConstantBool::True);
Chris Lattner00950542001-06-06 20:29:01 +00001514 }
1515 | FALSE {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001516 $$ = ValID::create(ConstantBool::False);
Chris Lattner00950542001-06-06 20:29:01 +00001517 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001518 | NULL_TOK {
1519 $$ = ValID::createNull();
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001520 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001521 | ConstExpr {
1522 $$ = ValID::create($1);
1523 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00001524
Chris Lattner2079fde2001-10-13 06:41:08 +00001525// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1526// another value.
1527//
1528SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001529 $$ = ValID::create($1);
1530 }
Chris Lattner6c23f572003-08-22 05:42:10 +00001531 | Name { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001532 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001533 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001534
1535// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001536ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001537
Chris Lattner00950542001-06-06 20:29:01 +00001538
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001539// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1540// type immediately preceeds the value reference, and allows complex constant
1541// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001542ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001543 $$ = getVal(*$1, $2); delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001544 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001545
Chris Lattner00950542001-06-06 20:29:01 +00001546BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner7e708292002-06-25 16:13:24 +00001547 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner00950542001-06-06 20:29:01 +00001548 }
Chris Lattner7e708292002-06-25 16:13:24 +00001549 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
1550 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner51727be2002-06-04 21:58:56 +00001551 };
Chris Lattner00950542001-06-06 20:29:01 +00001552
1553
1554// Basic blocks are terminated by branching instructions:
1555// br, br/cc, switch, ret
1556//
Chris Lattner2079fde2001-10-13 06:41:08 +00001557BasicBlock : InstructionList OptAssign BBTerminatorInst {
1558 if (setValueName($3, $2)) { assert(0 && "No redefn allowed!"); }
1559 InsertValue($3);
1560
1561 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001562 InsertValue($1);
1563 $$ = $1;
1564 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001565 | LABELSTR InstructionList OptAssign BBTerminatorInst {
1566 if (setValueName($4, $3)) { assert(0 && "No redefn allowed!"); }
1567 InsertValue($4);
1568
1569 $2->getInstList().push_back($4);
Chris Lattnerb7474512001-10-03 15:39:04 +00001570 if (setValueName($2, $1)) { assert(0 && "No label redef allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001571
1572 InsertValue($2);
1573 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001574 };
Chris Lattner00950542001-06-06 20:29:01 +00001575
1576InstructionList : InstructionList Inst {
1577 $1->getInstList().push_back($2);
1578 $$ = $1;
1579 }
1580 | /* empty */ {
Chris Lattner99e7ab72003-10-18 05:53:13 +00001581 $$ = CurBB = new BasicBlock();
Chris Lattner51727be2002-06-04 21:58:56 +00001582 };
Chris Lattner00950542001-06-06 20:29:01 +00001583
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001584BBTerminatorInst : RET ResolvedVal { // Return with a result...
1585 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001586 }
1587 | RET VOID { // Return with no result...
1588 $$ = new ReturnInst();
1589 }
1590 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner9636a912001-10-01 16:18:37 +00001591 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
Chris Lattner00950542001-06-06 20:29:01 +00001592 } // Conditional Branch...
1593 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner9636a912001-10-01 16:18:37 +00001594 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1595 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
Chris Lattner00950542001-06-06 20:29:01 +00001596 getVal(Type::BoolTy, $3));
1597 }
1598 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1599 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001600 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
Chris Lattner00950542001-06-06 20:29:01 +00001601 $$ = S;
1602
Chris Lattner9232b992003-04-22 18:42:41 +00001603 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00001604 E = $8->end();
1605 for (; I != E; ++I)
Chris Lattner4354f562003-08-23 23:14:52 +00001606 S->addCase(I->first, I->second);
Chris Lattner00950542001-06-06 20:29:01 +00001607 }
Chris Lattner196850c2003-05-15 21:30:00 +00001608 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
1609 SwitchInst *S = new SwitchInst(getVal($2, $3),
1610 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
1611 $$ = S;
1612 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001613 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO ResolvedVal
1614 EXCEPT ResolvedVal {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001615 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001616 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001617
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001618 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1619 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001620 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001621 std::vector<const Type*> ParamTypes;
Chris Lattner2079fde2001-10-13 06:41:08 +00001622 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001623 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1624 I != E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001625 ParamTypes.push_back((*I)->getType());
1626 }
1627
1628 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1629 if (isVarArg) ParamTypes.pop_back();
1630
Chris Lattner79df7c02002-03-26 18:01:55 +00001631 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001632 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00001633 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001634
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001635 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00001636
1637 BasicBlock *Normal = dyn_cast<BasicBlock>($8);
1638 BasicBlock *Except = dyn_cast<BasicBlock>($10);
1639
1640 if (Normal == 0 || Except == 0)
1641 ThrowException("Invoke instruction without label destinations!");
1642
1643 // Create the call node...
1644 if (!$5) { // Has no arguments?
Chris Lattner9232b992003-04-22 18:42:41 +00001645 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001646 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001647 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00001648 // correctly!
1649 //
Chris Lattner79df7c02002-03-26 18:01:55 +00001650 FunctionType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1651 FunctionType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner9232b992003-04-22 18:42:41 +00001652 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001653
1654 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1655 if ((*ArgI)->getType() != *I)
1656 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001657 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001658
1659 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1660 ThrowException("Invalid number of parameters detected!");
1661
Chris Lattner6cdb0112001-11-26 16:54:11 +00001662 $$ = new InvokeInst(V, Normal, Except, *$5);
Chris Lattner2079fde2001-10-13 06:41:08 +00001663 }
Chris Lattner9cd42572003-12-23 22:18:36 +00001664 delete $2;
Chris Lattner2079fde2001-10-13 06:41:08 +00001665 delete $5;
Chris Lattner36143fc2003-09-08 18:54:55 +00001666 }
1667 | UNWIND {
1668 $$ = new UnwindInst();
Chris Lattner51727be2002-06-04 21:58:56 +00001669 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001670
1671
Chris Lattner00950542001-06-06 20:29:01 +00001672
1673JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1674 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001675 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001676 if (V == 0)
1677 ThrowException("May only switch on a constant pool value!");
1678
Chris Lattner9232b992003-04-22 18:42:41 +00001679 $$->push_back(std::make_pair(V, cast<BasicBlock>(getVal($5, $6))));
Chris Lattner00950542001-06-06 20:29:01 +00001680 }
1681 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00001682 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001683 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001684
1685 if (V == 0)
1686 ThrowException("May only switch on a constant pool value!");
1687
Chris Lattner9232b992003-04-22 18:42:41 +00001688 $$->push_back(std::make_pair(V, cast<BasicBlock>(getVal($4, $5))));
Chris Lattner51727be2002-06-04 21:58:56 +00001689 };
Chris Lattner00950542001-06-06 20:29:01 +00001690
1691Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001692 // Is this definition named?? if so, assign the name...
1693 if (setValueName($2, $1)) { assert(0 && "No redefin allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001694 InsertValue($2);
1695 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001696};
Chris Lattner00950542001-06-06 20:29:01 +00001697
Chris Lattnerc24d2082001-06-11 15:04:20 +00001698PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Chris Lattner9232b992003-04-22 18:42:41 +00001699 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
1700 $$->push_back(std::make_pair(getVal(*$1, $3),
1701 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
Chris Lattner30c89792001-09-07 16:35:17 +00001702 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001703 }
1704 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1705 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001706 $1->push_back(std::make_pair(getVal($1->front().first->getType(), $4),
1707 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
Chris Lattner51727be2002-06-04 21:58:56 +00001708 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00001709
1710
Chris Lattner30c89792001-09-07 16:35:17 +00001711ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner9232b992003-04-22 18:42:41 +00001712 $$ = new std::vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001713 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001714 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001715 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001716 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001717 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00001718 };
Chris Lattner00950542001-06-06 20:29:01 +00001719
1720// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00001721ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001722
Chris Lattner4a6482b2002-09-10 19:57:26 +00001723InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1724 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint())
1725 ThrowException("Arithmetic operator requires integer or FP operands!");
1726 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1727 if ($$ == 0)
1728 ThrowException("binary operator returned null!");
1729 delete $2;
1730 }
1731 | LogicalOps Types ValueRef ',' ValueRef {
1732 if (!(*$2)->isIntegral())
1733 ThrowException("Logical operator requires integral operands!");
1734 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1735 if ($$ == 0)
1736 ThrowException("binary operator returned null!");
1737 delete $2;
1738 }
1739 | SetCondOps Types ValueRef ',' ValueRef {
Chris Lattner1cff96a2002-09-10 22:37:46 +00001740 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001741 if ($$ == 0)
1742 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001743 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001744 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00001745 | NOT ResolvedVal {
1746 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
1747 << " Replacing with 'xor'.\n";
1748
1749 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
1750 if (Ones == 0)
1751 ThrowException("Expected integral type for not instruction!");
1752
1753 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00001754 if ($$ == 0)
Chris Lattner699f1eb2002-08-14 17:12:33 +00001755 ThrowException("Could not create a xor instruction!");
Chris Lattner09083092001-07-08 04:57:15 +00001756 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001757 | ShiftOps ResolvedVal ',' ResolvedVal {
1758 if ($4->getType() != Type::UByteTy)
1759 ThrowException("Shift amount must be ubyte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001760 if (!$2->getType()->isInteger())
1761 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001762 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001763 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001764 | CAST ResolvedVal TO Types {
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001765 if (!$4->get()->isFirstClassType())
1766 ThrowException("cast instruction to a non-primitive type: '" +
1767 $4->get()->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001768 $$ = new CastInst($2, *$4);
1769 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001770 }
Chris Lattner8f77dae2003-05-08 02:44:12 +00001771 | VA_ARG ResolvedVal ',' Types {
Chris Lattner99e7ab72003-10-18 05:53:13 +00001772 // FIXME: This is emulation code for an obsolete syntax. This should be
1773 // removed at some point.
1774 if (!ObsoleteVarArgs) {
1775 std::cerr << "WARNING: this file uses obsolete features. "
1776 << "Assemble and disassemble to update it.\n";
1777 ObsoleteVarArgs = true;
1778 }
1779
1780 // First, load the valist...
1781 Instruction *CurVAList = new LoadInst($2, "");
1782 CurBB->getInstList().push_back(CurVAList);
1783
1784 // Emit the vaarg instruction.
1785 $$ = new VAArgInst(CurVAList, *$4);
1786
1787 // Now we must advance the pointer and update it in memory.
1788 Instruction *TheVANext = new VANextInst(CurVAList, *$4);
1789 CurBB->getInstList().push_back(TheVANext);
1790
1791 CurBB->getInstList().push_back(new StoreInst(TheVANext, $2));
1792 delete $4;
1793 }
1794 | VAARG ResolvedVal ',' Types {
1795 $$ = new VAArgInst($2, *$4);
1796 delete $4;
1797 }
1798 | VANEXT ResolvedVal ',' Types {
1799 $$ = new VANextInst($2, *$4);
Chris Lattner8f77dae2003-05-08 02:44:12 +00001800 delete $4;
1801 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00001802 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001803 const Type *Ty = $2->front().first->getType();
Chris Lattner8ea8f362003-10-30 01:38:18 +00001804 if (!Ty->isFirstClassType())
1805 ThrowException("PHI node operands must be of first class type!");
Chris Lattnerc24d2082001-06-11 15:04:20 +00001806 $$ = new PHINode(Ty);
Chris Lattnerbd2531f2003-10-10 16:34:58 +00001807 $$->op_reserve($2->size()*2);
Chris Lattner00950542001-06-06 20:29:01 +00001808 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001809 if ($2->front().first->getType() != Ty)
1810 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001811 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001812 $2->pop_front();
1813 }
1814 delete $2; // Free the list...
1815 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001816 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001817 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001818 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001819
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001820 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1821 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001822 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001823 std::vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001824 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001825 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1826 I != E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00001827 ParamTypes.push_back((*I)->getType());
1828 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001829
1830 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1831 if (isVarArg) ParamTypes.pop_back();
1832
Chris Lattner79df7c02002-03-26 18:01:55 +00001833 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001834 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001835 }
Chris Lattner00950542001-06-06 20:29:01 +00001836
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001837 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001838
Chris Lattner8b81bf52001-07-25 22:47:46 +00001839 // Create the call node...
1840 if (!$5) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00001841 // Make sure no arguments is a good thing!
1842 if (Ty->getNumParams() != 0)
1843 ThrowException("No arguments passed to a function that "
1844 "expects arguments!");
1845
Chris Lattner9232b992003-04-22 18:42:41 +00001846 $$ = new CallInst(V, std::vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001847 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001848 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00001849 // correctly!
1850 //
Chris Lattner79df7c02002-03-26 18:01:55 +00001851 FunctionType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1852 FunctionType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner9232b992003-04-22 18:42:41 +00001853 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001854
1855 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1856 if ((*ArgI)->getType() != *I)
1857 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001858 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001859
Chris Lattner8b81bf52001-07-25 22:47:46 +00001860 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001861 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001862
Chris Lattner6cdb0112001-11-26 16:54:11 +00001863 $$ = new CallInst(V, *$5);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001864 }
Chris Lattnerf64d57a2003-12-23 20:39:17 +00001865 delete $2;
Chris Lattner8b81bf52001-07-25 22:47:46 +00001866 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001867 }
1868 | MemoryInst {
1869 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001870 };
Chris Lattner00950542001-06-06 20:29:01 +00001871
Chris Lattner6cdb0112001-11-26 16:54:11 +00001872
1873// IndexList - List of indices for GEP based instructions...
1874IndexList : ',' ValueRefList {
Chris Lattner15c9c032003-09-08 18:20:29 +00001875 $$ = $2;
1876 } | /* empty */ {
1877 $$ = new std::vector<Value*>();
1878 };
1879
1880OptVolatile : VOLATILE {
1881 $$ = true;
1882 }
1883 | /* empty */ {
1884 $$ = false;
1885 };
1886
Chris Lattner027dcc52001-07-08 21:10:27 +00001887
Chris Lattner00950542001-06-06 20:29:01 +00001888MemoryInst : MALLOC Types {
Chris Lattner05804b72002-09-13 22:28:45 +00001889 $$ = new MallocInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00001890 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001891 }
1892 | MALLOC Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00001893 $$ = new MallocInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001894 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001895 }
1896 | ALLOCA Types {
Chris Lattner05804b72002-09-13 22:28:45 +00001897 $$ = new AllocaInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00001898 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001899 }
1900 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00001901 $$ = new AllocaInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001902 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001903 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001904 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00001905 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001906 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00001907 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001908 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001909 }
1910
Chris Lattner15c9c032003-09-08 18:20:29 +00001911 | OptVolatile LOAD Types ValueRef {
1912 if (!isa<PointerType>($3->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00001913 ThrowException("Can't load from nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00001914 (*$3)->getDescription());
Chris Lattner79ad13802003-09-08 20:29:46 +00001915 $$ = new LoadInst(getVal(*$3, $4), "", $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00001916 delete $3;
Chris Lattner027dcc52001-07-08 21:10:27 +00001917 }
Chris Lattner15c9c032003-09-08 18:20:29 +00001918 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
1919 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00001920 if (!PT)
Chris Lattner72e00252001-12-14 16:28:42 +00001921 ThrowException("Can't store to a nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00001922 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00001923 const Type *ElTy = PT->getElementType();
Chris Lattner15c9c032003-09-08 18:20:29 +00001924 if (ElTy != $3->getType())
1925 ThrowException("Can't store '" + $3->getType()->getDescription() +
Chris Lattner72e00252001-12-14 16:28:42 +00001926 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner0383cc42002-08-21 23:51:21 +00001927
Chris Lattner79ad13802003-09-08 20:29:46 +00001928 $$ = new StoreInst($3, getVal(*$5, $6), $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00001929 delete $5;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001930 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00001931 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00001932 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001933 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner30c89792001-09-07 16:35:17 +00001934 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner72e00252001-12-14 16:28:42 +00001935 ThrowException("Can't get element ptr '" + (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001936 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
1937 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00001938 };
Chris Lattner027dcc52001-07-08 21:10:27 +00001939
Brian Gaeked0fde302003-11-11 22:41:34 +00001940
Chris Lattner00950542001-06-06 20:29:01 +00001941%%
Chris Lattner09083092001-07-08 04:57:15 +00001942int yyerror(const char *ErrorMsg) {
Chris Lattner9232b992003-04-22 18:42:41 +00001943 std::string where
1944 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001945 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Chris Lattner9232b992003-04-22 18:42:41 +00001946 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001947 if (yychar == YYEMPTY)
1948 errMsg += "end-of-file.";
1949 else
Chris Lattner9232b992003-04-22 18:42:41 +00001950 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001951 ThrowException(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00001952 return 0;
1953}