blob: 28b4ad6e71e160590c62a0d4d2812f3c5a08ac45 [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
32static Module *ParserResult;
Chris Lattner9232b992003-04-22 18:42:41 +000033std::string CurFilename;
Chris Lattner00950542001-06-06 20:29:01 +000034
Chris Lattner30c89792001-09-07 16:35:17 +000035// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
36// relating to upreferences in the input stream.
37//
38//#define DEBUG_UPREFS 1
39#ifdef DEBUG_UPREFS
Chris Lattner699f1eb2002-08-14 17:12:33 +000040#define UR_OUT(X) std::cerr << X
Chris Lattner30c89792001-09-07 16:35:17 +000041#else
42#define UR_OUT(X)
43#endif
44
Vikram S. Adved3f7eb02002-07-14 22:59:28 +000045#define YYERROR_VERBOSE 1
46
Chris Lattner99e7ab72003-10-18 05:53:13 +000047// HACK ALERT: This variable is used to implement the automatic conversion of
48// variable argument instructions from their old to new forms. When this
49// compatiblity "Feature" is removed, this should be too.
50//
51static BasicBlock *CurBB;
52static bool ObsoleteVarArgs;
53
54
Chris Lattner7e708292002-06-25 16:13:24 +000055// This contains info used when building the body of a function. It is
56// destroyed when the function is completed.
Chris Lattner00950542001-06-06 20:29:01 +000057//
Chris Lattner9232b992003-04-22 18:42:41 +000058typedef std::vector<Value *> ValueList; // Numbered defs
59static void ResolveDefinitions(std::vector<ValueList> &LateResolvers,
60 std::vector<ValueList> *FutureLateResolvers = 0);
Chris Lattner00950542001-06-06 20:29:01 +000061
62static struct PerModuleInfo {
63 Module *CurrentModule;
Chris Lattner9232b992003-04-22 18:42:41 +000064 std::vector<ValueList> Values; // Module level numbered definitions
65 std::vector<ValueList> LateResolveValues;
66 std::vector<PATypeHolder> Types;
67 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000068
Chris Lattner2079fde2001-10-13 06:41:08 +000069 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
70 // references to global values. Global values may be referenced before they
71 // are defined, and if so, the temporary object that they represent is held
Chris Lattnere9bb2df2001-12-03 22:26:30 +000072 // here. This is used for forward references of ConstantPointerRefs.
Chris Lattner2079fde2001-10-13 06:41:08 +000073 //
Chris Lattner9232b992003-04-22 18:42:41 +000074 typedef std::map<std::pair<const PointerType *,
75 ValID>, GlobalVariable*> GlobalRefsType;
Chris Lattner2079fde2001-10-13 06:41:08 +000076 GlobalRefsType GlobalRefs;
77
Chris Lattner00950542001-06-06 20:29:01 +000078 void ModuleDone() {
Chris Lattner7e708292002-06-25 16:13:24 +000079 // If we could not resolve some functions at function compilation time
80 // (calls to functions before they are defined), resolve them now... Types
81 // are resolved when the constant pool has been completely parsed.
Chris Lattner30c89792001-09-07 16:35:17 +000082 //
Chris Lattner00950542001-06-06 20:29:01 +000083 ResolveDefinitions(LateResolveValues);
84
Chris Lattner2079fde2001-10-13 06:41:08 +000085 // Check to make sure that all global value forward references have been
86 // resolved!
87 //
88 if (!GlobalRefs.empty()) {
Chris Lattner9232b992003-04-22 18:42:41 +000089 std::string UndefinedReferences = "Unresolved global references exist:\n";
Chris Lattner749ce032002-03-11 22:12:39 +000090
91 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
92 I != E; ++I) {
93 UndefinedReferences += " " + I->first.first->getDescription() + " " +
94 I->first.second.getName() + "\n";
95 }
96 ThrowException(UndefinedReferences);
Chris Lattner2079fde2001-10-13 06:41:08 +000097 }
98
Chris Lattner7e708292002-06-25 16:13:24 +000099 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000100 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +0000101 CurrentModule = 0;
102 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000103
104
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000105 // DeclareNewGlobalValue - Called every time a new GV has been defined. This
Chris Lattner2079fde2001-10-13 06:41:08 +0000106 // is used to remove things from the forward declaration map, resolving them
107 // to the correct thing as needed.
108 //
109 void DeclareNewGlobalValue(GlobalValue *GV, ValID D) {
110 // Check to see if there is a forward reference to this global variable...
111 // if there is, eliminate it and patch the reference to use the new def'n.
Chris Lattner9232b992003-04-22 18:42:41 +0000112 GlobalRefsType::iterator I =
113 GlobalRefs.find(std::make_pair(GV->getType(), D));
Chris Lattner2079fde2001-10-13 06:41:08 +0000114
115 if (I != GlobalRefs.end()) {
116 GlobalVariable *OldGV = I->second; // Get the placeholder...
Misha Brukman5560c9d2003-08-18 14:43:39 +0000117 I->first.second.destroy(); // Free string memory if necessary
Chris Lattner2079fde2001-10-13 06:41:08 +0000118
119 // Loop over all of the uses of the GlobalValue. The only thing they are
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000120 // allowed to be is ConstantPointerRef's.
Chris Lattnerfd059242003-10-15 16:48:29 +0000121 assert(OldGV->hasOneUse() && "Only one reference should exist!");
Chris Lattner9e932bd2002-10-14 03:28:42 +0000122 User *U = OldGV->use_back(); // Must be a ConstantPointerRef...
123 ConstantPointerRef *CPR = cast<ConstantPointerRef>(U);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000124
Chris Lattner9e932bd2002-10-14 03:28:42 +0000125 // Change the const pool reference to point to the real global variable
126 // now. This should drop a use from the OldGV.
127 CPR->mutateReferences(OldGV, GV);
128 assert(OldGV->use_empty() && "All uses should be gone now!");
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000129
130 // Remove OldGV from the module...
Chris Lattner2079fde2001-10-13 06:41:08 +0000131 CurrentModule->getGlobalList().remove(OldGV);
132 delete OldGV; // Delete the old placeholder
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000133
Chris Lattner2079fde2001-10-13 06:41:08 +0000134 // Remove the map entry for the global now that it has been created...
135 GlobalRefs.erase(I);
136 }
137 }
138
Chris Lattner00950542001-06-06 20:29:01 +0000139} CurModule;
140
Chris Lattner79df7c02002-03-26 18:01:55 +0000141static struct PerFunctionInfo {
Chris Lattner7e708292002-06-25 16:13:24 +0000142 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner00950542001-06-06 20:29:01 +0000143
Chris Lattner9232b992003-04-22 18:42:41 +0000144 std::vector<ValueList> Values; // Keep track of numbered definitions
145 std::vector<ValueList> LateResolveValues;
146 std::vector<PATypeHolder> Types;
147 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner7e708292002-06-25 16:13:24 +0000148 bool isDeclare; // Is this function a forward declararation?
Chris Lattner00950542001-06-06 20:29:01 +0000149
Chris Lattner79df7c02002-03-26 18:01:55 +0000150 inline PerFunctionInfo() {
151 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000152 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000153 }
154
Chris Lattner79df7c02002-03-26 18:01:55 +0000155 inline void FunctionStart(Function *M) {
156 CurrentFunction = M;
Chris Lattner00950542001-06-06 20:29:01 +0000157 }
158
Chris Lattner79df7c02002-03-26 18:01:55 +0000159 void FunctionDone() {
Chris Lattner00950542001-06-06 20:29:01 +0000160 // If we could not resolve some blocks at parsing time (forward branches)
161 // resolve the branches now...
Chris Lattner386a3b72001-10-16 19:54:17 +0000162 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000163
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000164 // Make sure to resolve any constant expr references that might exist within
165 // the function we just declared itself.
166 ValID FID;
167 if (CurrentFunction->hasName()) {
168 FID = ValID::create((char*)CurrentFunction->getName().c_str());
169 } else {
170 unsigned Slot = CurrentFunction->getType()->getUniqueID();
171 assert(CurModule.Values.size() > Slot && "Function not inserted?");
172 // Figure out which slot number if is...
173 for (unsigned i = 0; ; ++i) {
174 assert(i < CurModule.Values[Slot].size() && "Function not found!");
175 if (CurModule.Values[Slot][i] == CurrentFunction) {
176 FID = ValID::create((int)i);
177 break;
178 }
179 }
180 }
181 CurModule.DeclareNewGlobalValue(CurrentFunction, FID);
182
Chris Lattner7e708292002-06-25 16:13:24 +0000183 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000184 Types.clear();
Chris Lattner79df7c02002-03-26 18:01:55 +0000185 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000186 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000187 }
Chris Lattner394f2eb2003-10-16 20:12:13 +0000188} CurFun; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000189
Chris Lattner394f2eb2003-10-16 20:12:13 +0000190static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000191
Chris Lattner00950542001-06-06 20:29:01 +0000192
193//===----------------------------------------------------------------------===//
194// Code to handle definitions of all the types
195//===----------------------------------------------------------------------===//
196
Chris Lattner9232b992003-04-22 18:42:41 +0000197static int InsertValue(Value *D,
Chris Lattner394f2eb2003-10-16 20:12:13 +0000198 std::vector<ValueList> &ValueTab = CurFun.Values) {
Chris Lattner2079fde2001-10-13 06:41:08 +0000199 if (D->hasName()) return -1; // Is this a numbered definition?
200
201 // Yes, insert the value into the value table...
202 unsigned type = D->getType()->getUniqueID();
203 if (ValueTab.size() <= type)
204 ValueTab.resize(type+1, ValueList());
205 //printf("Values[%d][%d] = %d\n", type, ValueTab[type].size(), D);
206 ValueTab[type].push_back(D);
207 return ValueTab[type].size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000208}
209
Chris Lattner30c89792001-09-07 16:35:17 +0000210// TODO: FIXME when Type are not const
Chris Lattner9232b992003-04-22 18:42:41 +0000211static void InsertType(const Type *Ty, std::vector<PATypeHolder> &Types) {
Chris Lattner30c89792001-09-07 16:35:17 +0000212 Types.push_back(Ty);
213}
214
215static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000216 switch (D.Type) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000217 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000218 unsigned Num = (unsigned)D.Num;
219
220 // Module constants occupy the lowest numbered slots...
221 if (Num < CurModule.Types.size())
222 return CurModule.Types[Num];
223
224 Num -= CurModule.Types.size();
225
226 // Check that the number is within bounds...
Chris Lattner394f2eb2003-10-16 20:12:13 +0000227 if (Num <= CurFun.Types.size())
228 return CurFun.Types[Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000229 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000230 }
Chris Lattnerf8dff732002-07-18 05:18:37 +0000231 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000232 std::string Name(D.Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000233 SymbolTable *SymTab = 0;
Chris Lattner6e6026b2002-11-20 18:36:02 +0000234 Value *N = 0;
235 if (inFunctionScope()) {
Chris Lattner394f2eb2003-10-16 20:12:13 +0000236 SymTab = &CurFun.CurrentFunction->getSymbolTable();
Chris Lattner6e6026b2002-11-20 18:36:02 +0000237 N = SymTab->lookup(Type::TypeTy, Name);
238 }
Chris Lattner30c89792001-09-07 16:35:17 +0000239
240 if (N == 0) {
Chris Lattner7e708292002-06-25 16:13:24 +0000241 // Symbol table doesn't automatically chain yet... because the function
Chris Lattner30c89792001-09-07 16:35:17 +0000242 // hasn't been added to the module...
243 //
Chris Lattner6e6026b2002-11-20 18:36:02 +0000244 SymTab = &CurModule.CurrentModule->getSymbolTable();
245 N = SymTab->lookup(Type::TypeTy, Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000246 if (N == 0) break;
247 }
248
249 D.destroy(); // Free old strdup'd memory...
Chris Lattner949a3622003-07-23 15:30:06 +0000250 return cast<Type>(N);
Chris Lattner30c89792001-09-07 16:35:17 +0000251 }
252 default:
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000253 ThrowException("Internal parser error: Invalid symbol type reference!");
Chris Lattner30c89792001-09-07 16:35:17 +0000254 }
255
256 // If we reached here, we referenced either a symbol that we don't know about
257 // or an id number that hasn't been read yet. We may be referencing something
258 // forward, so just create an entry to be resolved later and get to it...
259 //
260 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
261
Chris Lattner9232b992003-04-22 18:42:41 +0000262 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000263 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
Chris Lattner4a42e902001-10-22 05:56:09 +0000264
Chris Lattner9232b992003-04-22 18:42:41 +0000265 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000266 if (I != LateResolver.end()) {
267 return I->second;
268 }
Chris Lattner30c89792001-09-07 16:35:17 +0000269
Chris Lattner82269592001-10-22 06:01:08 +0000270 Type *Typ = OpaqueType::get();
Chris Lattner9232b992003-04-22 18:42:41 +0000271 LateResolver.insert(std::make_pair(D, Typ));
Chris Lattner30c89792001-09-07 16:35:17 +0000272 return Typ;
273}
274
Chris Lattner9232b992003-04-22 18:42:41 +0000275static Value *lookupInSymbolTable(const Type *Ty, const std::string &Name) {
Chris Lattner6e6026b2002-11-20 18:36:02 +0000276 SymbolTable &SymTab =
Chris Lattner394f2eb2003-10-16 20:12:13 +0000277 inFunctionScope() ? CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner9705a152002-05-02 19:27:42 +0000278 CurModule.CurrentModule->getSymbolTable();
Chris Lattner6e6026b2002-11-20 18:36:02 +0000279 return SymTab.lookup(Ty, Name);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000280}
281
Chris Lattner2079fde2001-10-13 06:41:08 +0000282// getValNonImprovising - Look up the value specified by the provided type and
283// the provided ValID. If the value exists and has already been defined, return
284// it. Otherwise return null.
285//
286static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000287 if (isa<FunctionType>(Ty))
288 ThrowException("Functions are not values and "
289 "must be referenced as pointers");
Chris Lattner386a3b72001-10-16 19:54:17 +0000290
Chris Lattner30c89792001-09-07 16:35:17 +0000291 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000292 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000293 unsigned type = Ty->getUniqueID();
Chris Lattner00950542001-06-06 20:29:01 +0000294 unsigned Num = (unsigned)D.Num;
295
296 // Module constants occupy the lowest numbered slots...
297 if (type < CurModule.Values.size()) {
298 if (Num < CurModule.Values[type].size())
299 return CurModule.Values[type][Num];
300
301 Num -= CurModule.Values[type].size();
302 }
303
304 // Make sure that our type is within bounds
Chris Lattner394f2eb2003-10-16 20:12:13 +0000305 if (CurFun.Values.size() <= type) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000306
307 // Check that the number is within bounds...
Chris Lattner394f2eb2003-10-16 20:12:13 +0000308 if (CurFun.Values[type].size() <= Num) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000309
Chris Lattner394f2eb2003-10-16 20:12:13 +0000310 return CurFun.Values[type][Num];
Chris Lattner00950542001-06-06 20:29:01 +0000311 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000312
Chris Lattner1a1cb112001-09-30 22:46:54 +0000313 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000314 Value *N = lookupInSymbolTable(Ty, std::string(D.Name));
Chris Lattner2079fde2001-10-13 06:41:08 +0000315 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000316
317 D.destroy(); // Free old strdup'd memory...
318 return N;
319 }
320
Chris Lattner2079fde2001-10-13 06:41:08 +0000321 // Check to make sure that "Ty" is an integral type, and that our
322 // value will fit into the specified type...
323 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattnerd78700d2002-08-16 21:14:40 +0000324 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
325 ThrowException("Signed integral constant '" +
326 itostr(D.ConstPool64) + "' is invalid for type '" +
327 Ty->getDescription() + "'!");
328 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000329
330 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000331 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
332 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000333 ThrowException("Integral constant '" + utostr(D.UConstPool64) +
334 "' is invalid or out of range!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000335 } else { // This is really a signed reference. Transmogrify.
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000336 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000337 }
338 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000339 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000340 }
341
Chris Lattner2079fde2001-10-13 06:41:08 +0000342 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000343 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner2079fde2001-10-13 06:41:08 +0000344 ThrowException("FP constant invalid for type!!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000345 return ConstantFP::get(Ty, D.ConstPoolFP);
Chris Lattner2079fde2001-10-13 06:41:08 +0000346
347 case ValID::ConstNullVal: // Is it a null value?
Chris Lattner9b625032002-05-06 16:15:30 +0000348 if (!isa<PointerType>(Ty))
Chris Lattner2079fde2001-10-13 06:41:08 +0000349 ThrowException("Cannot create a a non pointer null!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000350 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner2079fde2001-10-13 06:41:08 +0000351
Chris Lattnerd78700d2002-08-16 21:14:40 +0000352 case ValID::ConstantVal: // Fully resolved constant?
353 if (D.ConstantValue->getType() != Ty)
354 ThrowException("Constant expression type different from required type!");
355 return D.ConstantValue;
356
Chris Lattner30c89792001-09-07 16:35:17 +0000357 default:
358 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000359 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000360 } // End of switch
361
Chris Lattner2079fde2001-10-13 06:41:08 +0000362 assert(0 && "Unhandled case!");
363 return 0;
364}
365
366
367// getVal - This function is identical to getValNonImprovising, except that if a
368// value is not already defined, it "improvises" by creating a placeholder var
369// that looks and acts just like the requested variable. When the value is
370// defined later, all uses of the placeholder variable are replaced with the
371// real thing.
372//
373static Value *getVal(const Type *Ty, const ValID &D) {
374 assert(Ty != Type::TypeTy && "Should use getTypeVal for types!");
375
376 // See if the value has already been defined...
377 Value *V = getValNonImprovising(Ty, D);
378 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000379
380 // If we reached here, we referenced either a symbol that we don't know about
381 // or an id number that hasn't been read yet. We may be referencing something
382 // forward, so just create an entry to be resolved later and get to it...
383 //
Chris Lattner00950542001-06-06 20:29:01 +0000384 Value *d = 0;
Chris Lattner30c89792001-09-07 16:35:17 +0000385 switch (Ty->getPrimitiveID()) {
386 case Type::LabelTyID: d = new BBPlaceHolder(Ty, D); break;
Chris Lattner30c89792001-09-07 16:35:17 +0000387 default: d = new ValuePlaceHolder(Ty, D); break;
Chris Lattner00950542001-06-06 20:29:01 +0000388 }
389
390 assert(d != 0 && "How did we not make something?");
Chris Lattner79df7c02002-03-26 18:01:55 +0000391 if (inFunctionScope())
Chris Lattner394f2eb2003-10-16 20:12:13 +0000392 InsertValue(d, CurFun.LateResolveValues);
Chris Lattner386a3b72001-10-16 19:54:17 +0000393 else
394 InsertValue(d, CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000395 return d;
396}
397
398
399//===----------------------------------------------------------------------===//
400// Code to handle forward references in instructions
401//===----------------------------------------------------------------------===//
402//
403// This code handles the late binding needed with statements that reference
404// values not defined yet... for example, a forward branch, or the PHI node for
405// a loop body.
406//
Chris Lattner394f2eb2003-10-16 20:12:13 +0000407// This keeps a table (CurFun.LateResolveValues) of all such forward references
Chris Lattner00950542001-06-06 20:29:01 +0000408// and back patchs after we are done.
409//
410
411// ResolveDefinitions - If we could not resolve some defs at parsing
412// time (forward branches, phi functions for loops, etc...) resolve the
413// defs now...
414//
Chris Lattner9232b992003-04-22 18:42:41 +0000415static void ResolveDefinitions(std::vector<ValueList> &LateResolvers,
416 std::vector<ValueList> *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000417 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
418 for (unsigned ty = 0; ty < LateResolvers.size(); ty++) {
419 while (!LateResolvers[ty].empty()) {
420 Value *V = LateResolvers[ty].back();
Chris Lattner386a3b72001-10-16 19:54:17 +0000421 assert(!isa<Type>(V) && "Types should be in LateResolveTypes!");
422
Chris Lattner00950542001-06-06 20:29:01 +0000423 LateResolvers[ty].pop_back();
424 ValID &DID = getValIDFromPlaceHolder(V);
425
Chris Lattner2079fde2001-10-13 06:41:08 +0000426 Value *TheRealValue = getValNonImprovising(Type::getUniqueIDType(ty),DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000427 if (TheRealValue) {
428 V->replaceAllUsesWith(TheRealValue);
429 delete V;
430 } else if (FutureLateResolvers) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000431 // Functions have their unresolved items forwarded to the module late
Chris Lattner386a3b72001-10-16 19:54:17 +0000432 // resolver table
433 InsertValue(V, *FutureLateResolvers);
434 } else {
Chris Lattner9705a152002-05-02 19:27:42 +0000435 if (DID.Type == ValID::NameVal)
Chris Lattner30c89792001-09-07 16:35:17 +0000436 ThrowException("Reference to an invalid definition: '" +DID.getName()+
437 "' of type '" + V->getType()->getDescription() + "'",
438 getLineNumFromPlaceHolder(V));
439 else
440 ThrowException("Reference to an invalid definition: #" +
441 itostr(DID.Num) + " of type '" +
442 V->getType()->getDescription() + "'",
443 getLineNumFromPlaceHolder(V));
444 }
Chris Lattner00950542001-06-06 20:29:01 +0000445 }
446 }
447
448 LateResolvers.clear();
449}
450
Chris Lattner4a42e902001-10-22 05:56:09 +0000451// ResolveTypeTo - A brand new type was just declared. This means that (if
452// name is not null) things referencing Name can be resolved. Otherwise, things
453// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000454//
Chris Lattner4a42e902001-10-22 05:56:09 +0000455static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner9232b992003-04-22 18:42:41 +0000456 std::vector<PATypeHolder> &Types = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000457 CurFun.Types : CurModule.Types;
Chris Lattner00950542001-06-06 20:29:01 +0000458
Chris Lattner4a42e902001-10-22 05:56:09 +0000459 ValID D;
460 if (Name) D = ValID::create(Name);
461 else D = ValID::create((int)Types.size());
Chris Lattner30c89792001-09-07 16:35:17 +0000462
Chris Lattner9232b992003-04-22 18:42:41 +0000463 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000464 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
Chris Lattner4a42e902001-10-22 05:56:09 +0000465
Chris Lattner9232b992003-04-22 18:42:41 +0000466 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000467 if (I != LateResolver.end()) {
Chris Lattner51727be2002-06-04 21:58:56 +0000468 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner4a42e902001-10-22 05:56:09 +0000469 LateResolver.erase(I);
470 }
471}
472
473// ResolveTypes - At this point, all types should be resolved. Any that aren't
474// are errors.
475//
Chris Lattner9232b992003-04-22 18:42:41 +0000476static void ResolveTypes(std::map<ValID, PATypeHolder> &LateResolveTypes) {
Chris Lattner4a42e902001-10-22 05:56:09 +0000477 if (!LateResolveTypes.empty()) {
Chris Lattner82269592001-10-22 06:01:08 +0000478 const ValID &DID = LateResolveTypes.begin()->first;
Chris Lattner4a42e902001-10-22 05:56:09 +0000479
480 if (DID.Type == ValID::NameVal)
Chris Lattner82269592001-10-22 06:01:08 +0000481 ThrowException("Reference to an invalid type: '" +DID.getName() + "'");
Chris Lattner4a42e902001-10-22 05:56:09 +0000482 else
Chris Lattner82269592001-10-22 06:01:08 +0000483 ThrowException("Reference to an invalid type: #" + itostr(DID.Num));
Chris Lattner30c89792001-09-07 16:35:17 +0000484 }
485}
486
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000487
Chris Lattner1781aca2001-09-18 04:00:54 +0000488// setValueName - Set the specified value to the name given. The name may be
489// null potentially, in which case this is a noop. The string passed in is
490// assumed to be a malloc'd string buffer, and is freed by this function.
491//
Chris Lattnerb7474512001-10-03 15:39:04 +0000492// This function returns true if the value has already been defined, but is
493// allowed to be redefined in the specified context. If the name is a new name
494// for the typeplane, false is returned.
495//
496static bool setValueName(Value *V, char *NameStr) {
497 if (NameStr == 0) return false;
Chris Lattner386a3b72001-10-16 19:54:17 +0000498
Chris Lattner9232b992003-04-22 18:42:41 +0000499 std::string Name(NameStr); // Copy string
Chris Lattner1781aca2001-09-18 04:00:54 +0000500 free(NameStr); // Free old string
501
Chris Lattner2079fde2001-10-13 06:41:08 +0000502 if (V->getType() == Type::VoidTy)
503 ThrowException("Can't assign name '" + Name +
504 "' to a null valued instruction!");
505
Chris Lattner6e6026b2002-11-20 18:36:02 +0000506 SymbolTable &ST = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000507 CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner6e6026b2002-11-20 18:36:02 +0000508 CurModule.CurrentModule->getSymbolTable();
Chris Lattner30c89792001-09-07 16:35:17 +0000509
Chris Lattner6e6026b2002-11-20 18:36:02 +0000510 Value *Existing = ST.lookup(V->getType(), Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000511 if (Existing) { // Inserting a name that is already defined???
512 // There is only one case where this is allowed: when we are refining an
513 // opaque type. In this case, Existing will be an opaque type.
Chris Lattner949a3622003-07-23 15:30:06 +0000514 if (const Type *Ty = dyn_cast<Type>(Existing)) {
Chris Lattner51727be2002-06-04 21:58:56 +0000515 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Ty)) {
Chris Lattner30c89792001-09-07 16:35:17 +0000516 // We ARE replacing an opaque type!
Chris Lattner51727be2002-06-04 21:58:56 +0000517 ((OpaqueType*)OpTy)->refineAbstractTypeTo(cast<Type>(V));
Chris Lattnerb7474512001-10-03 15:39:04 +0000518 return true;
Chris Lattner30c89792001-09-07 16:35:17 +0000519 }
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000520 }
Chris Lattner30c89792001-09-07 16:35:17 +0000521
Chris Lattner9636a912001-10-01 16:18:37 +0000522 // Otherwise, we are a simple redefinition of a value, check to see if it
523 // is defined the same as the old one...
Chris Lattneradf99702003-03-03 23:28:55 +0000524 if (const Type *Ty = dyn_cast<Type>(Existing)) {
525 if (Ty == cast<Type>(V)) return true; // Yes, it's equal.
Chris Lattner699f1eb2002-08-14 17:12:33 +0000526 // std::cerr << "Type: " << Ty->getDescription() << " != "
Chris Lattner949a3622003-07-23 15:30:06 +0000527 // << cast<Type>(V)->getDescription() << "!\n";
Chris Lattneradf99702003-03-03 23:28:55 +0000528 } else if (const Constant *C = dyn_cast<Constant>(Existing)) {
529 if (C == V) return true; // Constants are equal to themselves
Chris Lattnerb7474512001-10-03 15:39:04 +0000530 } else if (GlobalVariable *EGV = dyn_cast<GlobalVariable>(Existing)) {
Chris Lattner43efcbf2001-10-03 19:35:57 +0000531 // We are allowed to redefine a global variable in two circumstances:
532 // 1. If at least one of the globals is uninitialized or
533 // 2. If both initializers have the same value.
534 //
Chris Lattner89219832001-10-03 19:35:04 +0000535 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000536 if (!EGV->hasInitializer() || !GV->hasInitializer() ||
537 EGV->getInitializer() == GV->getInitializer()) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000538
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000539 // Make sure the existing global version gets the initializer! Make
540 // sure that it also gets marked const if the new version is.
Chris Lattner89219832001-10-03 19:35:04 +0000541 if (GV->hasInitializer() && !EGV->hasInitializer())
542 EGV->setInitializer(GV->getInitializer());
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000543 if (GV->isConstant())
544 EGV->setConstant(true);
Chris Lattner4ad02e72003-04-16 20:28:45 +0000545 EGV->setLinkage(GV->getLinkage());
Chris Lattner89219832001-10-03 19:35:04 +0000546
Chris Lattner2079fde2001-10-13 06:41:08 +0000547 delete GV; // Destroy the duplicate!
Chris Lattner89219832001-10-03 19:35:04 +0000548 return true; // They are equivalent!
549 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000550 }
Chris Lattner9636a912001-10-01 16:18:37 +0000551 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000552 ThrowException("Redefinition of value named '" + Name + "' in the '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000553 V->getType()->getDescription() + "' type plane!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000554 }
Chris Lattner00950542001-06-06 20:29:01 +0000555
Chris Lattner6e6026b2002-11-20 18:36:02 +0000556 V->setName(Name, &ST);
Chris Lattnerb7474512001-10-03 15:39:04 +0000557 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000558}
559
Chris Lattner8896eda2001-07-09 19:38:36 +0000560
Chris Lattner30c89792001-09-07 16:35:17 +0000561//===----------------------------------------------------------------------===//
562// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000563//
Chris Lattner8896eda2001-07-09 19:38:36 +0000564
Chris Lattner30c89792001-09-07 16:35:17 +0000565// TypeContains - Returns true if Ty contains E in it.
566//
567static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3ff43872001-09-28 22:56:31 +0000568 return find(df_begin(Ty), df_end(Ty), E) != df_end(Ty);
Chris Lattner30c89792001-09-07 16:35:17 +0000569}
Chris Lattner698b56e2001-07-20 19:15:08 +0000570
Chris Lattner30c89792001-09-07 16:35:17 +0000571
Chris Lattner9232b992003-04-22 18:42:41 +0000572static std::vector<std::pair<unsigned, OpaqueType *> > UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000573
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000574static PATypeHolder HandleUpRefs(const Type *ty) {
575 PATypeHolder Ty(ty);
Chris Lattner5084d032001-11-02 07:46:26 +0000576 UR_OUT("Type '" << ty->getDescription() <<
577 "' newly formed. Resolving upreferences.\n" <<
578 UpRefs.size() << " upreferences active!\n");
Chris Lattner30c89792001-09-07 16:35:17 +0000579 for (unsigned i = 0; i < UpRefs.size(); ) {
Chris Lattner5084d032001-11-02 07:46:26 +0000580 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Chris Lattner30c89792001-09-07 16:35:17 +0000581 << UpRefs[i].second->getDescription() << ") = "
Chris Lattner5084d032001-11-02 07:46:26 +0000582 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << endl);
Chris Lattner30c89792001-09-07 16:35:17 +0000583 if (TypeContains(Ty, UpRefs[i].second)) {
584 unsigned Level = --UpRefs[i].first; // Decrement level of upreference
Chris Lattner5084d032001-11-02 07:46:26 +0000585 UR_OUT(" Uplevel Ref Level = " << Level << endl);
Chris Lattner30c89792001-09-07 16:35:17 +0000586 if (Level == 0) { // Upreference should be resolved!
Chris Lattner5084d032001-11-02 07:46:26 +0000587 UR_OUT(" * Resolving upreference for "
588 << UpRefs[i].second->getDescription() << endl;
Chris Lattner9232b992003-04-22 18:42:41 +0000589 std::string OldName = UpRefs[i].second->getDescription());
Chris Lattner30c89792001-09-07 16:35:17 +0000590 UpRefs[i].second->refineAbstractTypeTo(Ty);
591 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattner5084d032001-11-02 07:46:26 +0000592 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
Chris Lattner30c89792001-09-07 16:35:17 +0000593 << (const void*)Ty << ", " << Ty->getDescription() << endl);
594 continue;
595 }
596 }
597
598 ++i; // Otherwise, no resolve, move on...
Chris Lattner8896eda2001-07-09 19:38:36 +0000599 }
Chris Lattner30c89792001-09-07 16:35:17 +0000600 // FIXME: TODO: this should return the updated type
Chris Lattner8896eda2001-07-09 19:38:36 +0000601 return Ty;
602}
603
Chris Lattner30c89792001-09-07 16:35:17 +0000604
Chris Lattner00950542001-06-06 20:29:01 +0000605//===----------------------------------------------------------------------===//
606// RunVMAsmParser - Define an interface to this parser
607//===----------------------------------------------------------------------===//
608//
Chris Lattner9232b992003-04-22 18:42:41 +0000609Module *RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000610 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000611 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000612 llvmAsmlineno = 1; // Reset the current line number...
Chris Lattner99e7ab72003-10-18 05:53:13 +0000613 ObsoleteVarArgs = false;
Chris Lattner00950542001-06-06 20:29:01 +0000614
Chris Lattner75f20532003-04-22 18:02:52 +0000615 // Allocate a new module to read
616 CurModule.CurrentModule = new Module(Filename);
Chris Lattner00950542001-06-06 20:29:01 +0000617 yyparse(); // Parse the file.
Chris Lattner99e7ab72003-10-18 05:53:13 +0000618
Chris Lattner00950542001-06-06 20:29:01 +0000619 Module *Result = ParserResult;
Chris Lattner99e7ab72003-10-18 05:53:13 +0000620
621 // Check to see if they called va_start but not va_arg..
622 if (!ObsoleteVarArgs)
623 if (Function *F = Result->getNamedFunction("llvm.va_start"))
624 if (F->asize() == 1) {
625 std::cerr << "WARNING: this file uses obsolete features. "
626 << "Assemble and disassemble to update it.\n";
627 ObsoleteVarArgs = true;
628 }
629
630
631 if (ObsoleteVarArgs) {
632 // If the user is making use of obsolete varargs intrinsics, adjust them for
633 // the user.
634 if (Function *F = Result->getNamedFunction("llvm.va_start")) {
635 assert(F->asize() == 1 && "Obsolete va_start takes 1 argument!");
636
637 const Type *RetTy = F->getFunctionType()->getParamType(0);
638 RetTy = cast<PointerType>(RetTy)->getElementType();
639 Function *NF = Result->getOrInsertFunction("llvm.va_start", RetTy, 0);
640
641 while (!F->use_empty()) {
642 CallInst *CI = cast<CallInst>(F->use_back());
643 Value *V = new CallInst(NF, "", CI);
644 new StoreInst(V, CI->getOperand(1), CI);
645 CI->getParent()->getInstList().erase(CI);
646 }
647 Result->getFunctionList().erase(F);
648 }
649
650 if (Function *F = Result->getNamedFunction("llvm.va_end")) {
651 assert(F->asize() == 1 && "Obsolete va_end takes 1 argument!");
652 const Type *ArgTy = F->getFunctionType()->getParamType(0);
653 ArgTy = cast<PointerType>(ArgTy)->getElementType();
654 Function *NF = Result->getOrInsertFunction("llvm.va_end", Type::VoidTy,
655 ArgTy, 0);
656
657 while (!F->use_empty()) {
658 CallInst *CI = cast<CallInst>(F->use_back());
659 Value *V = new LoadInst(CI->getOperand(1), "", CI);
660 new CallInst(NF, V, "", CI);
661 CI->getParent()->getInstList().erase(CI);
662 }
663 Result->getFunctionList().erase(F);
664 }
665
666 if (Function *F = Result->getNamedFunction("llvm.va_copy")) {
667 assert(F->asize() == 2 && "Obsolete va_copy takes 2 argument!");
668 const Type *ArgTy = F->getFunctionType()->getParamType(0);
669 ArgTy = cast<PointerType>(ArgTy)->getElementType();
670 Function *NF = Result->getOrInsertFunction("llvm.va_copy", ArgTy,
671 ArgTy, 0);
672
673 while (!F->use_empty()) {
674 CallInst *CI = cast<CallInst>(F->use_back());
675 Value *V = new CallInst(NF, CI->getOperand(2), "", CI);
676 new StoreInst(V, CI->getOperand(1), CI);
677 CI->getParent()->getInstList().erase(CI);
678 }
679 Result->getFunctionList().erase(F);
680 }
681 }
682
Chris Lattner00950542001-06-06 20:29:01 +0000683 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
684 ParserResult = 0;
685
686 return Result;
687}
688
689%}
690
691%union {
Chris Lattner30c89792001-09-07 16:35:17 +0000692 Module *ModuleVal;
Chris Lattner79df7c02002-03-26 18:01:55 +0000693 Function *FunctionVal;
Chris Lattner69da5cf2002-10-13 20:57:00 +0000694 std::pair<PATypeHolder*, char*> *ArgVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000695 BasicBlock *BasicBlockVal;
696 TerminatorInst *TermInstVal;
697 Instruction *InstVal;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000698 Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000699
Chris Lattner30c89792001-09-07 16:35:17 +0000700 const Type *PrimType;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000701 PATypeHolder *TypeVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000702 Value *ValueVal;
703
Chris Lattner69da5cf2002-10-13 20:57:00 +0000704 std::vector<std::pair<PATypeHolder*,char*> > *ArgList;
Chris Lattner697954c2002-01-20 22:54:45 +0000705 std::vector<Value*> *ValueList;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000706 std::list<PATypeHolder> *TypeList;
Chris Lattner697954c2002-01-20 22:54:45 +0000707 std::list<std::pair<Value*,
708 BasicBlock*> > *PHIList; // Represent the RHS of PHI node
Chris Lattner46748042002-04-09 19:41:42 +0000709 std::vector<std::pair<Constant*, BasicBlock*> > *JumpTable;
Chris Lattner697954c2002-01-20 22:54:45 +0000710 std::vector<Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000711
Chris Lattner4ad02e72003-04-16 20:28:45 +0000712 GlobalValue::LinkageTypes Linkage;
Chris Lattner30c89792001-09-07 16:35:17 +0000713 int64_t SInt64Val;
714 uint64_t UInt64Val;
715 int SIntVal;
716 unsigned UIntVal;
717 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000718 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000719
Chris Lattner30c89792001-09-07 16:35:17 +0000720 char *StrVal; // This memory is strdup'd!
721 ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000722
Chris Lattner30c89792001-09-07 16:35:17 +0000723 Instruction::BinaryOps BinaryOpVal;
724 Instruction::TermOps TermOpVal;
725 Instruction::MemoryOps MemOpVal;
726 Instruction::OtherOps OtherOpVal;
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000727 Module::Endianness Endianness;
Chris Lattner00950542001-06-06 20:29:01 +0000728}
729
Chris Lattner79df7c02002-03-26 18:01:55 +0000730%type <ModuleVal> Module FunctionList
731%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000732%type <BasicBlockVal> BasicBlock InstructionList
733%type <TermInstVal> BBTerminatorInst
734%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000735%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000736%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000737%type <ArgList> ArgList ArgListH
738%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000739%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000740%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000741%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000742%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000743%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +0000744%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner15c9c032003-09-08 18:20:29 +0000745%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattner4ad02e72003-04-16 20:28:45 +0000746%type <Linkage> OptLinkage
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000747%type <Endianness> BigOrLittle
Chris Lattner00950542001-06-06 20:29:01 +0000748
Chris Lattner2079fde2001-10-13 06:41:08 +0000749// ValueRef - Unresolved reference to a definition or BB
750%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000751%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000752// Tokens and types for handling constant integer values
753//
754// ESINT64VAL - A negative number within long long range
755%token <SInt64Val> ESINT64VAL
756
757// EUINT64VAL - A positive number within uns. long long range
758%token <UInt64Val> EUINT64VAL
759%type <SInt64Val> EINT64VAL
760
761%token <SIntVal> SINTVAL // Signed 32 bit ints...
762%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
763%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000764%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000765
766// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000767%type <TypeVal> Types TypesV UpRTypes UpRTypesV
768%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000769%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
770%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000771
Chris Lattner6c23f572003-08-22 05:42:10 +0000772%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
773%type <StrVal> Name OptName OptAssign
Chris Lattner00950542001-06-06 20:29:01 +0000774
775
Chris Lattnerf4600482003-06-28 20:01:34 +0000776%token IMPLEMENTATION ZEROINITIALIZER TRUE FALSE BEGINTOK ENDTOK
Chris Lattner15c9c032003-09-08 18:20:29 +0000777%token DECLARE GLOBAL CONSTANT VOLATILE
Chris Lattnerf797cab2003-10-10 04:54:02 +0000778%token TO EXCEPT DOTDOTDOT NULL_TOK CONST INTERNAL LINKONCE WEAK APPENDING
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000779%token OPAQUE NOT EXTERNAL TARGET ENDIAN POINTERSIZE LITTLE BIG
Chris Lattner00950542001-06-06 20:29:01 +0000780
781// Basic Block Terminating Operators
Chris Lattner36143fc2003-09-08 18:54:55 +0000782%token <TermOpVal> RET BR SWITCH INVOKE UNWIND
Chris Lattner00950542001-06-06 20:29:01 +0000783
Chris Lattner00950542001-06-06 20:29:01 +0000784// Binary Operators
785%type <BinaryOpVal> BinaryOps // all the binary operators
Chris Lattner4a6482b2002-09-10 19:57:26 +0000786%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattner42c9e772001-10-20 09:32:59 +0000787%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000788%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000789
790// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000791%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000792
Chris Lattner027dcc52001-07-08 21:10:27 +0000793// Other Operators
794%type <OtherOpVal> ShiftOps
Chris Lattner3b237fc2003-10-19 21:34:28 +0000795%token <OtherOpVal> PHI_TOK CALL CAST SHL SHR VAARG VANEXT
Chris Lattner99e7ab72003-10-18 05:53:13 +0000796%token VA_ARG // FIXME: OBSOLETE
Chris Lattner027dcc52001-07-08 21:10:27 +0000797
Chris Lattner00950542001-06-06 20:29:01 +0000798%start Module
799%%
800
801// Handle constant integer size restriction and conversion...
802//
Chris Lattner51727be2002-06-04 21:58:56 +0000803INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +0000804INTVAL : UINTVAL {
805 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
806 ThrowException("Value too large for type!");
807 $$ = (int32_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000808};
Chris Lattner00950542001-06-06 20:29:01 +0000809
810
Chris Lattner51727be2002-06-04 21:58:56 +0000811EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +0000812EINT64VAL : EUINT64VAL {
813 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
814 ThrowException("Value too large for type!");
815 $$ = (int64_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000816};
Chris Lattner00950542001-06-06 20:29:01 +0000817
Chris Lattner00950542001-06-06 20:29:01 +0000818// Operations that are notably excluded from this list include:
819// RET, BR, & SWITCH because they end basic blocks and are treated specially.
820//
Chris Lattner4a6482b2002-09-10 19:57:26 +0000821ArithmeticOps: ADD | SUB | MUL | DIV | REM;
822LogicalOps : AND | OR | XOR;
823SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
824BinaryOps : ArithmeticOps | LogicalOps | SetCondOps;
825
Chris Lattner51727be2002-06-04 21:58:56 +0000826ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +0000827
Chris Lattnere98dda62001-07-14 06:10:16 +0000828// These are some types that allow classification if we only want a particular
829// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +0000830SIntType : LONG | INT | SHORT | SBYTE;
831UIntType : ULONG | UINT | USHORT | UBYTE;
832IntType : SIntType | UIntType;
833FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +0000834
Chris Lattnere98dda62001-07-14 06:10:16 +0000835// OptAssign - Value producing statements have an optional assignment component
Chris Lattner6c23f572003-08-22 05:42:10 +0000836OptAssign : Name '=' {
Chris Lattner00950542001-06-06 20:29:01 +0000837 $$ = $1;
838 }
839 | /*empty*/ {
840 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +0000841 };
Chris Lattner00950542001-06-06 20:29:01 +0000842
Chris Lattner4ad02e72003-04-16 20:28:45 +0000843OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
844 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
Chris Lattner72ac148d2003-10-16 18:29:00 +0000845 WEAK { $$ = GlobalValue::WeakLinkage; } |
Chris Lattner4ad02e72003-04-16 20:28:45 +0000846 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
847 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattner30c89792001-09-07 16:35:17 +0000848
849//===----------------------------------------------------------------------===//
850// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +0000851// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +0000852// access to it, a user must explicitly use TypesV.
853//
854
855// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +0000856TypesV : Types | VOID { $$ = new PATypeHolder($1); };
857UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +0000858
859Types : UpRTypes {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000860 if (UpRefs.size())
861 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
862 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000863 };
Chris Lattner30c89792001-09-07 16:35:17 +0000864
865
866// Derived types are added later...
867//
Chris Lattner51727be2002-06-04 21:58:56 +0000868PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
869PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000870UpRTypes : OPAQUE {
871 $$ = new PATypeHolder(OpaqueType::get());
872 }
873 | PrimType {
874 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +0000875 };
Chris Lattnerd78700d2002-08-16 21:14:40 +0000876UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000877 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +0000878};
Chris Lattner30c89792001-09-07 16:35:17 +0000879
Chris Lattner30c89792001-09-07 16:35:17 +0000880// Include derived types in the Types production.
881//
882UpRTypes : '\\' EUINT64VAL { // Type UpReference
883 if ($2 > (uint64_t)INT64_MAX) ThrowException("Value out of range!");
884 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner9232b992003-04-22 18:42:41 +0000885 UpRefs.push_back(std::make_pair((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000886 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +0000887 UR_OUT("New Upreference!\n");
888 }
Chris Lattner79df7c02002-03-26 18:01:55 +0000889 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner9232b992003-04-22 18:42:41 +0000890 std::vector<const Type*> Params;
Chris Lattner697954c2002-01-20 22:54:45 +0000891 mapto($3->begin(), $3->end(), std::back_inserter(Params),
Chris Lattner876dc572003-10-02 19:00:34 +0000892 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner2079fde2001-10-13 06:41:08 +0000893 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
894 if (isVarArg) Params.pop_back();
895
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000896 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +0000897 delete $3; // Delete the argument list
898 delete $1; // Delete the old type handle
899 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000900 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000901 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000902 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +0000903 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000904 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +0000905 std::vector<const Type*> Elements;
Chris Lattner697954c2002-01-20 22:54:45 +0000906 mapto($2->begin(), $2->end(), std::back_inserter(Elements),
Chris Lattner876dc572003-10-02 19:00:34 +0000907 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner30c89792001-09-07 16:35:17 +0000908
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000909 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000910 delete $2;
911 }
912 | '{' '}' { // Empty structure type?
Chris Lattner9232b992003-04-22 18:42:41 +0000913 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000914 }
915 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000916 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000917 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000918 };
Chris Lattner30c89792001-09-07 16:35:17 +0000919
Chris Lattner7e708292002-06-25 16:13:24 +0000920// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +0000921// declaration type lists
922//
923TypeListI : UpRTypes {
Chris Lattner9232b992003-04-22 18:42:41 +0000924 $$ = new std::list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +0000925 $$->push_back(*$1); delete $1;
926 }
927 | TypeListI ',' UpRTypes {
928 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +0000929 };
Chris Lattner30c89792001-09-07 16:35:17 +0000930
Chris Lattner7e708292002-06-25 16:13:24 +0000931// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +0000932ArgTypeListI : TypeListI
933 | TypeListI ',' DOTDOTDOT {
934 ($$=$1)->push_back(Type::VoidTy);
935 }
936 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +0000937 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +0000938 }
939 | /*empty*/ {
Chris Lattner9232b992003-04-22 18:42:41 +0000940 $$ = new std::list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +0000941 };
Chris Lattner30c89792001-09-07 16:35:17 +0000942
Chris Lattnere98dda62001-07-14 06:10:16 +0000943// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +0000944// production is used ONLY to represent constants that show up AFTER a 'const',
945// 'constant' or 'global' token at global scope. Constants that can be inlined
946// into other expressions (such as integers and constexprs) are handled by the
947// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +0000948//
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000949ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Chris Lattner949a3622003-07-23 15:30:06 +0000950 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000951 if (ATy == 0)
952 ThrowException("Cannot make array constant with type: '" +
953 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000954 const Type *ETy = ATy->getElementType();
955 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +0000956
Chris Lattner30c89792001-09-07 16:35:17 +0000957 // Verify that we have the correct size...
958 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +0000959 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +0000960 utostr($3->size()) + " arguments, but has size of " +
961 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +0000962
Chris Lattner30c89792001-09-07 16:35:17 +0000963 // Verify all elements are correct type!
964 for (unsigned i = 0; i < $3->size(); i++) {
965 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +0000966 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +0000967 ETy->getDescription() +"' as required!\nIt is of type '"+
968 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +0000969 }
970
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000971 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +0000972 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000973 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000974 | Types '[' ']' {
Chris Lattner949a3622003-07-23 15:30:06 +0000975 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000976 if (ATy == 0)
977 ThrowException("Cannot make array constant with type: '" +
978 (*$1)->getDescription() + "'!");
979
980 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +0000981 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +0000982 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +0000983 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattner9232b992003-04-22 18:42:41 +0000984 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +0000985 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +0000986 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000987 | Types 'c' STRINGCONSTANT {
Chris Lattner949a3622003-07-23 15:30:06 +0000988 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000989 if (ATy == 0)
990 ThrowException("Cannot make array constant with type: '" +
991 (*$1)->getDescription() + "'!");
992
Chris Lattner30c89792001-09-07 16:35:17 +0000993 int NumElements = ATy->getNumElements();
994 const Type *ETy = ATy->getElementType();
995 char *EndStr = UnEscapeLexed($3, true);
996 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +0000997 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +0000998 itostr((int)(EndStr-$3)) +
999 " when array has size " + itostr(NumElements) + "!");
Chris Lattner9232b992003-04-22 18:42:41 +00001000 std::vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +00001001 if (ETy == Type::SByteTy) {
1002 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001003 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +00001004 } else if (ETy == Type::UByteTy) {
1005 for (char *C = $3; C != EndStr; ++C)
Chris Lattnerbae362f2003-01-30 22:24:26 +00001006 Vals.push_back(ConstantUInt::get(ETy, (unsigned char)*C));
Chris Lattner93750fa2001-07-28 17:48:55 +00001007 } else {
Chris Lattner30c89792001-09-07 16:35:17 +00001008 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +00001009 ThrowException("Cannot build string arrays of non byte sized elements!");
1010 }
Chris Lattner30c89792001-09-07 16:35:17 +00001011 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001012 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +00001013 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001014 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001015 | Types '{' ConstVector '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001016 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001017 if (STy == 0)
1018 ThrowException("Cannot make struct constant with type: '" +
1019 (*$1)->getDescription() + "'!");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001020
Chris Lattnerc6212f12003-05-21 16:06:56 +00001021 if ($3->size() != STy->getNumContainedTypes())
1022 ThrowException("Illegal number of initializers for structure type!");
1023
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001024 // Check to ensure that constants are compatible with the type initializer!
1025 for (unsigned i = 0, e = $3->size(); i != e; ++i)
1026 if ((*$3)[i]->getType() != STy->getElementTypes()[i])
1027 ThrowException("Expected type '" +
1028 STy->getElementTypes()[i]->getDescription() +
1029 "' for element #" + utostr(i) +
1030 " of structure initializer!");
1031
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001032 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001033 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001034 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001035 | Types '{' '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001036 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001037 if (STy == 0)
1038 ThrowException("Cannot make struct constant with type: '" +
1039 (*$1)->getDescription() + "'!");
1040
1041 if (STy->getNumContainedTypes() != 0)
1042 ThrowException("Illegal number of initializers for structure type!");
1043
1044 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1045 delete $1;
1046 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001047 | Types NULL_TOK {
Chris Lattner949a3622003-07-23 15:30:06 +00001048 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001049 if (PTy == 0)
1050 ThrowException("Cannot make null pointer constant with type: '" +
1051 (*$1)->getDescription() + "'!");
1052
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001053 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001054 delete $1;
1055 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001056 | Types SymbolicValueRef {
Chris Lattner949a3622003-07-23 15:30:06 +00001057 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001058 if (Ty == 0)
1059 ThrowException("Global const reference must be a pointer type!");
1060
Chris Lattner3101c252002-08-15 17:58:33 +00001061 // ConstExprs can exist in the body of a function, thus creating
1062 // ConstantPointerRefs whenever they refer to a variable. Because we are in
1063 // the context of a function, getValNonImprovising will search the functions
1064 // symbol table instead of the module symbol table for the global symbol,
1065 // which throws things all off. To get around this, we just tell
1066 // getValNonImprovising that we are at global scope here.
1067 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001068 Function *SavedCurFn = CurFun.CurrentFunction;
1069 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001070
Chris Lattner2079fde2001-10-13 06:41:08 +00001071 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001072
Chris Lattner394f2eb2003-10-16 20:12:13 +00001073 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001074
Chris Lattner2079fde2001-10-13 06:41:08 +00001075 // If this is an initializer for a constant pointer, which is referencing a
1076 // (currently) undefined variable, create a stub now that shall be replaced
1077 // in the future with the right type of variable.
1078 //
1079 if (V == 0) {
1080 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1081 const PointerType *PT = cast<PointerType>(Ty);
1082
1083 // First check to see if the forward references value is already created!
1084 PerModuleInfo::GlobalRefsType::iterator I =
Chris Lattner9232b992003-04-22 18:42:41 +00001085 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001086
1087 if (I != CurModule.GlobalRefs.end()) {
1088 V = I->second; // Placeholder already exists, use it...
1089 } else {
1090 // TODO: Include line number info by creating a subclass of
1091 // TODO: GlobalVariable here that includes the said information!
1092
1093 // Create a placeholder for the global variable reference...
Chris Lattner7a176752001-12-04 00:03:30 +00001094 GlobalVariable *GV = new GlobalVariable(PT->getElementType(),
Chris Lattner4ad02e72003-04-16 20:28:45 +00001095 false,
1096 GlobalValue::ExternalLinkage);
Chris Lattner2079fde2001-10-13 06:41:08 +00001097 // Keep track of the fact that we have a forward ref to recycle it
Chris Lattner9232b992003-04-22 18:42:41 +00001098 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
Chris Lattner2079fde2001-10-13 06:41:08 +00001099
1100 // Must temporarily push this value into the module table...
1101 CurModule.CurrentModule->getGlobalList().push_back(GV);
1102 V = GV;
1103 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001104 }
1105
Chris Lattner2079fde2001-10-13 06:41:08 +00001106 GlobalValue *GV = cast<GlobalValue>(V);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001107 $$ = ConstantPointerRef::get(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001108 delete $1; // Free the type handle
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001109 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001110 | Types ConstExpr {
1111 if ($1->get() != $2->getType())
1112 ThrowException("Mismatched types for constant expression!");
1113 $$ = $2;
1114 delete $1;
Chris Lattnerf4600482003-06-28 20:01:34 +00001115 }
1116 | Types ZEROINITIALIZER {
1117 $$ = Constant::getNullValue($1->get());
1118 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001119 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001120
Chris Lattnere43f40b2002-10-09 00:25:32 +00001121ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001122 if (!ConstantSInt::isValueValidForType($1, $2))
1123 ThrowException("Constant value doesn't fit in type!");
1124 $$ = ConstantSInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001125 }
1126 | UIntType EUINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001127 if (!ConstantUInt::isValueValidForType($1, $2))
1128 ThrowException("Constant value doesn't fit in type!");
1129 $$ = ConstantUInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001130 }
1131 | BOOL TRUE { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001132 $$ = ConstantBool::True;
1133 }
Chris Lattnere43f40b2002-10-09 00:25:32 +00001134 | BOOL FALSE { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001135 $$ = ConstantBool::False;
1136 }
1137 | FPType FPVAL { // Float & Double constants
1138 $$ = ConstantFP::get($1, $2);
1139 };
1140
Chris Lattner00950542001-06-06 20:29:01 +00001141
Chris Lattnerd78700d2002-08-16 21:14:40 +00001142ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001143 if (!$5->get()->isFirstClassType())
1144 ThrowException("cast constant expression to a non-primitive type: '" +
1145 $5->get()->getDescription() + "'!");
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001146 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001147 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001148 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001149 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1150 if (!isa<PointerType>($3->getType()))
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001151 ThrowException("GetElementPtr requires a pointer operand!");
1152
1153 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001154 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001155 if (!IdxTy)
1156 ThrowException("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001157
Chris Lattner9232b992003-04-22 18:42:41 +00001158 std::vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001159 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1160 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001161 IdxVec.push_back(C);
1162 else
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001163 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001164
Chris Lattnerd78700d2002-08-16 21:14:40 +00001165 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001166
Chris Lattnerd78700d2002-08-16 21:14:40 +00001167 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001168 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001169 | BinaryOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001170 if ($3->getType() != $5->getType())
1171 ThrowException("Binary operator types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001172 $$ = ConstantExpr::get($1, $3, $5);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001173 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001174 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001175 if ($5->getType() != Type::UByteTy)
1176 ThrowException("Shift count for shift constant must be unsigned byte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001177 if (!$3->getType()->isInteger())
1178 ThrowException("Shift constant expression requires integer operand!");
Chris Lattner5e458e22003-05-21 17:48:56 +00001179 $$ = ConstantExpr::getShift($1, $3, $5);
Chris Lattner699f1eb2002-08-14 17:12:33 +00001180 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001181
1182
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001183// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001184ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001185 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001186 }
1187 | ConstVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001188 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001189 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001190 };
Chris Lattner00950542001-06-06 20:29:01 +00001191
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001192
Chris Lattner1781aca2001-09-18 04:00:54 +00001193// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001194GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001195
Chris Lattner00950542001-06-06 20:29:01 +00001196
Chris Lattner0e73ce62002-05-02 19:11:13 +00001197//===----------------------------------------------------------------------===//
1198// Rules to match Modules
1199//===----------------------------------------------------------------------===//
1200
1201// Module rule: Capture the result of parsing the whole file into a result
1202// variable...
1203//
1204Module : FunctionList {
1205 $$ = ParserResult = $1;
1206 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001207};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001208
Chris Lattner7e708292002-06-25 16:13:24 +00001209// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001210//
1211FunctionList : FunctionList Function {
1212 $$ = $1;
1213 assert($2->getParent() == 0 && "Function already in module!");
1214 $1->getFunctionList().push_back($2);
Chris Lattner394f2eb2003-10-16 20:12:13 +00001215 CurFun.FunctionDone();
Chris Lattner0e73ce62002-05-02 19:11:13 +00001216 }
1217 | FunctionList FunctionProto {
1218 $$ = $1;
1219 }
1220 | FunctionList IMPLEMENTATION {
1221 $$ = $1;
1222 }
1223 | ConstPool {
1224 $$ = CurModule.CurrentModule;
1225 // Resolve circular types before we parse the body of the module
1226 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001227 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001228
Chris Lattnere98dda62001-07-14 06:10:16 +00001229// ConstPool - Constants with optional names assigned to them.
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001230ConstPool : ConstPool OptAssign CONST ConstVal {
Chris Lattneradf99702003-03-03 23:28:55 +00001231 if (!setValueName($4, $2))
1232 InsertValue($4);
Chris Lattner00950542001-06-06 20:29:01 +00001233 }
Chris Lattner30c89792001-09-07 16:35:17 +00001234 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner4a42e902001-10-22 05:56:09 +00001235 // Eagerly resolve types. This is not an optimization, this is a
1236 // requirement that is due to the fact that we could have this:
1237 //
1238 // %list = type { %list * }
1239 // %list = type { %list * } ; repeated type decl
1240 //
1241 // If types are not resolved eagerly, then the two types will not be
1242 // determined to be the same type!
1243 //
1244 ResolveTypeTo($2, $4->get());
1245
Chris Lattner1781aca2001-09-18 04:00:54 +00001246 // TODO: FIXME when Type are not const
Chris Lattnerb7474512001-10-03 15:39:04 +00001247 if (!setValueName(const_cast<Type*>($4->get()), $2)) {
1248 // If this is not a redefinition of a type...
1249 if (!$2) {
1250 InsertType($4->get(),
Chris Lattner394f2eb2003-10-16 20:12:13 +00001251 inFunctionScope() ? CurFun.Types : CurModule.Types);
Chris Lattnerb7474512001-10-03 15:39:04 +00001252 }
Chris Lattner30c89792001-09-07 16:35:17 +00001253 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001254
1255 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001256 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001257 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001258 }
Chris Lattner4ad02e72003-04-16 20:28:45 +00001259 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Chris Lattnerdda71962001-11-26 18:54:16 +00001260 const Type *Ty = $5->getType();
Chris Lattner1781aca2001-09-18 04:00:54 +00001261 // Global declarations appear in Constant Pool
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001262 Constant *Initializer = $5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001263 if (Initializer == 0)
1264 ThrowException("Global value initializer is not a constant!");
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001265
Chris Lattnerdda71962001-11-26 18:54:16 +00001266 GlobalVariable *GV = new GlobalVariable(Ty, $4, $3, Initializer);
Chris Lattnerb7474512001-10-03 15:39:04 +00001267 if (!setValueName(GV, $2)) { // If not redefining...
1268 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001269 int Slot = InsertValue(GV, CurModule.Values);
1270
1271 if (Slot != -1) {
1272 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1273 } else {
1274 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1275 (char*)GV->getName().c_str()));
1276 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001277 }
Chris Lattner1781aca2001-09-18 04:00:54 +00001278 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001279 | ConstPool OptAssign EXTERNAL GlobalType Types {
1280 const Type *Ty = *$5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001281 // Global declarations appear in Constant Pool
Chris Lattner4ad02e72003-04-16 20:28:45 +00001282 GlobalVariable *GV = new GlobalVariable(Ty,$4,GlobalValue::ExternalLinkage);
Chris Lattnerb7474512001-10-03 15:39:04 +00001283 if (!setValueName(GV, $2)) { // If not redefining...
1284 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001285 int Slot = InsertValue(GV, CurModule.Values);
1286
1287 if (Slot != -1) {
1288 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1289 } else {
1290 assert(GV->hasName() && "Not named and not numbered!?");
1291 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1292 (char*)GV->getName().c_str()));
1293 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001294 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001295 delete $5;
Chris Lattnere98dda62001-07-14 06:10:16 +00001296 }
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001297 | ConstPool TARGET TargetDefinition {
1298 }
Chris Lattner00950542001-06-06 20:29:01 +00001299 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001300 };
Chris Lattner00950542001-06-06 20:29:01 +00001301
1302
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001303
1304BigOrLittle : BIG { $$ = Module::BigEndian; };
1305BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1306
1307TargetDefinition : ENDIAN '=' BigOrLittle {
1308 CurModule.CurrentModule->setEndianness($3);
1309 }
1310 | POINTERSIZE '=' EUINT64VAL {
1311 if ($3 == 32)
1312 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1313 else if ($3 == 64)
1314 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1315 else
1316 ThrowException("Invalid pointer size: '" + utostr($3) + "'!");
1317 };
1318
1319
Chris Lattner00950542001-06-06 20:29:01 +00001320//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001321// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001322//===----------------------------------------------------------------------===//
1323
Chris Lattner6c23f572003-08-22 05:42:10 +00001324Name : VAR_ID | STRINGCONSTANT;
1325OptName : Name | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001326
Chris Lattner6c23f572003-08-22 05:42:10 +00001327ArgVal : Types OptName {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001328 if (*$1 == Type::VoidTy)
1329 ThrowException("void typed arguments are invalid!");
Chris Lattner9232b992003-04-22 18:42:41 +00001330 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
Chris Lattner51727be2002-06-04 21:58:56 +00001331};
Chris Lattner00950542001-06-06 20:29:01 +00001332
Chris Lattner69da5cf2002-10-13 20:57:00 +00001333ArgListH : ArgListH ',' ArgVal {
1334 $$ = $1;
1335 $1->push_back(*$3);
1336 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001337 }
1338 | ArgVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001339 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
Chris Lattner69da5cf2002-10-13 20:57:00 +00001340 $$->push_back(*$1);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001341 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001342 };
Chris Lattner00950542001-06-06 20:29:01 +00001343
1344ArgList : ArgListH {
1345 $$ = $1;
1346 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001347 | ArgListH ',' DOTDOTDOT {
1348 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001349 $$->push_back(std::pair<PATypeHolder*,
1350 char*>(new PATypeHolder(Type::VoidTy), 0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001351 }
1352 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001353 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1354 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001355 }
Chris Lattner00950542001-06-06 20:29:01 +00001356 | /* empty */ {
1357 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001358 };
Chris Lattner00950542001-06-06 20:29:01 +00001359
Chris Lattner6c23f572003-08-22 05:42:10 +00001360FunctionHeaderH : TypesV Name '(' ArgList ')' {
Chris Lattner1f862af2003-04-16 18:13:57 +00001361 UnEscapeLexed($2);
Chris Lattner9232b992003-04-22 18:42:41 +00001362 std::string FunctionName($2);
Chris Lattnerdda71962001-11-26 18:54:16 +00001363
Chris Lattner9232b992003-04-22 18:42:41 +00001364 std::vector<const Type*> ParamTypeList;
Chris Lattner1f862af2003-04-16 18:13:57 +00001365 if ($4) { // If there are arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001366 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001367 I != $4->end(); ++I)
Chris Lattner69da5cf2002-10-13 20:57:00 +00001368 ParamTypeList.push_back(I->first->get());
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001369 }
Chris Lattner00950542001-06-06 20:29:01 +00001370
Chris Lattner2079fde2001-10-13 06:41:08 +00001371 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1372 if (isVarArg) ParamTypeList.pop_back();
1373
Chris Lattner1f862af2003-04-16 18:13:57 +00001374 const FunctionType *FT = FunctionType::get(*$1, ParamTypeList, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001375 const PointerType *PFT = PointerType::get(FT);
Chris Lattner1f862af2003-04-16 18:13:57 +00001376 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001377
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001378 Function *Fn = 0;
1379 // Is the function already in symtab?
1380 if ((Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1381 // Yes it is. If this is the case, either we need to be a forward decl,
1382 // or it needs to be.
Chris Lattner394f2eb2003-10-16 20:12:13 +00001383 if (!CurFun.isDeclare && !Fn->isExternal())
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001384 ThrowException("Redefinition of function '" + FunctionName + "'!");
1385
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001386 // If we found a preexisting function prototype, remove it from the
1387 // module, so that we don't get spurious conflicts with global & local
1388 // variables.
1389 //
1390 CurModule.CurrentModule->getFunctionList().remove(Fn);
Chris Lattner34538142002-03-08 19:11:42 +00001391
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001392 // Make sure to strip off any argument names so we can't get conflicts...
1393 for (Function::aiterator AI = Fn->abegin(), AE = Fn->aend(); AI != AE; ++AI)
1394 AI->setName("");
Chris Lattner5659dd12002-07-15 00:10:33 +00001395
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001396 } else { // Not already defined?
Chris Lattner4ad02e72003-04-16 20:28:45 +00001397 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001398 InsertValue(Fn, CurModule.Values);
Chris Lattner1f862af2003-04-16 18:13:57 +00001399 CurModule.DeclareNewGlobalValue(Fn, ValID::create($2));
Chris Lattnere1815642001-07-15 06:35:53 +00001400 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001401 free($2); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001402
Chris Lattner394f2eb2003-10-16 20:12:13 +00001403 CurFun.FunctionStart(Fn);
Chris Lattner00950542001-06-06 20:29:01 +00001404
Chris Lattner7e708292002-06-25 16:13:24 +00001405 // Add all of the arguments we parsed to the function...
Chris Lattner1f862af2003-04-16 18:13:57 +00001406 if ($4) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001407 if (isVarArg) { // Nuke the last entry
Chris Lattner1f862af2003-04-16 18:13:57 +00001408 assert($4->back().first->get() == Type::VoidTy && $4->back().second == 0&&
Chris Lattner69da5cf2002-10-13 20:57:00 +00001409 "Not a varargs marker!");
Chris Lattner1f862af2003-04-16 18:13:57 +00001410 delete $4->back().first;
1411 $4->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00001412 }
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001413 Function::aiterator ArgIt = Fn->abegin();
Chris Lattner9232b992003-04-22 18:42:41 +00001414 for (std::vector<std::pair<PATypeHolder*, char*> >::iterator I =$4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001415 I != $4->end(); ++I, ++ArgIt) {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001416 delete I->first; // Delete the typeholder...
1417
1418 if (setValueName(ArgIt, I->second)) // Insert arg into symtab...
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001419 assert(0 && "No arg redef allowed!");
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001420
Chris Lattner69da5cf2002-10-13 20:57:00 +00001421 InsertValue(ArgIt);
Chris Lattner00950542001-06-06 20:29:01 +00001422 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001423
Chris Lattner1f862af2003-04-16 18:13:57 +00001424 delete $4; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00001425 }
Chris Lattner51727be2002-06-04 21:58:56 +00001426};
Chris Lattner00950542001-06-06 20:29:01 +00001427
Chris Lattner9b02cc32002-05-03 18:23:48 +00001428BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1429
Chris Lattner4ad02e72003-04-16 20:28:45 +00001430FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00001431 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001432
Chris Lattner4ad02e72003-04-16 20:28:45 +00001433 // Make sure that we keep track of the linkage type even if there was a
1434 // previous "declare".
1435 $$->setLinkage($1);
Chris Lattner1f862af2003-04-16 18:13:57 +00001436
Chris Lattner7e708292002-06-25 16:13:24 +00001437 // Resolve circular types before we parse the body of the function.
Chris Lattner394f2eb2003-10-16 20:12:13 +00001438 ResolveTypes(CurFun.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001439};
Chris Lattner00950542001-06-06 20:29:01 +00001440
Chris Lattner9b02cc32002-05-03 18:23:48 +00001441END : ENDTOK | '}'; // Allow end of '}' to end a function
1442
Chris Lattner79df7c02002-03-26 18:01:55 +00001443Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001444 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001445};
Chris Lattner00950542001-06-06 20:29:01 +00001446
Chris Lattner394f2eb2003-10-16 20:12:13 +00001447FunctionProto : DECLARE { CurFun.isDeclare = true; } FunctionHeaderH {
1448 $$ = CurFun.CurrentFunction;
Chris Lattner79df7c02002-03-26 18:01:55 +00001449 assert($$->getParent() == 0 && "Function already in module!");
1450 CurModule.CurrentModule->getFunctionList().push_back($$);
Chris Lattner394f2eb2003-10-16 20:12:13 +00001451 CurFun.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001452};
Chris Lattner00950542001-06-06 20:29:01 +00001453
1454//===----------------------------------------------------------------------===//
1455// Rules to match Basic Blocks
1456//===----------------------------------------------------------------------===//
1457
1458ConstValueRef : ESINT64VAL { // A reference to a direct constant
1459 $$ = ValID::create($1);
1460 }
1461 | EUINT64VAL {
1462 $$ = ValID::create($1);
1463 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001464 | FPVAL { // Perhaps it's an FP constant?
1465 $$ = ValID::create($1);
1466 }
Chris Lattner00950542001-06-06 20:29:01 +00001467 | TRUE {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001468 $$ = ValID::create(ConstantBool::True);
Chris Lattner00950542001-06-06 20:29:01 +00001469 }
1470 | FALSE {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001471 $$ = ValID::create(ConstantBool::False);
Chris Lattner00950542001-06-06 20:29:01 +00001472 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001473 | NULL_TOK {
1474 $$ = ValID::createNull();
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001475 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001476 | ConstExpr {
1477 $$ = ValID::create($1);
1478 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00001479
Chris Lattner2079fde2001-10-13 06:41:08 +00001480// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1481// another value.
1482//
1483SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001484 $$ = ValID::create($1);
1485 }
Chris Lattner6c23f572003-08-22 05:42:10 +00001486 | Name { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001487 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001488 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001489
1490// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001491ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001492
Chris Lattner00950542001-06-06 20:29:01 +00001493
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001494// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1495// type immediately preceeds the value reference, and allows complex constant
1496// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001497ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001498 $$ = getVal(*$1, $2); delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001499 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001500
Chris Lattner00950542001-06-06 20:29:01 +00001501BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner7e708292002-06-25 16:13:24 +00001502 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner00950542001-06-06 20:29:01 +00001503 }
Chris Lattner7e708292002-06-25 16:13:24 +00001504 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
1505 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner51727be2002-06-04 21:58:56 +00001506 };
Chris Lattner00950542001-06-06 20:29:01 +00001507
1508
1509// Basic blocks are terminated by branching instructions:
1510// br, br/cc, switch, ret
1511//
Chris Lattner2079fde2001-10-13 06:41:08 +00001512BasicBlock : InstructionList OptAssign BBTerminatorInst {
1513 if (setValueName($3, $2)) { assert(0 && "No redefn allowed!"); }
1514 InsertValue($3);
1515
1516 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001517 InsertValue($1);
1518 $$ = $1;
1519 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001520 | LABELSTR InstructionList OptAssign BBTerminatorInst {
1521 if (setValueName($4, $3)) { assert(0 && "No redefn allowed!"); }
1522 InsertValue($4);
1523
1524 $2->getInstList().push_back($4);
Chris Lattnerb7474512001-10-03 15:39:04 +00001525 if (setValueName($2, $1)) { assert(0 && "No label redef allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001526
1527 InsertValue($2);
1528 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001529 };
Chris Lattner00950542001-06-06 20:29:01 +00001530
1531InstructionList : InstructionList Inst {
1532 $1->getInstList().push_back($2);
1533 $$ = $1;
1534 }
1535 | /* empty */ {
Chris Lattner99e7ab72003-10-18 05:53:13 +00001536 $$ = CurBB = new BasicBlock();
Chris Lattner51727be2002-06-04 21:58:56 +00001537 };
Chris Lattner00950542001-06-06 20:29:01 +00001538
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001539BBTerminatorInst : RET ResolvedVal { // Return with a result...
1540 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001541 }
1542 | RET VOID { // Return with no result...
1543 $$ = new ReturnInst();
1544 }
1545 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner9636a912001-10-01 16:18:37 +00001546 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
Chris Lattner00950542001-06-06 20:29:01 +00001547 } // Conditional Branch...
1548 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner9636a912001-10-01 16:18:37 +00001549 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1550 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
Chris Lattner00950542001-06-06 20:29:01 +00001551 getVal(Type::BoolTy, $3));
1552 }
1553 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1554 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001555 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
Chris Lattner00950542001-06-06 20:29:01 +00001556 $$ = S;
1557
Chris Lattner9232b992003-04-22 18:42:41 +00001558 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00001559 E = $8->end();
1560 for (; I != E; ++I)
Chris Lattner4354f562003-08-23 23:14:52 +00001561 S->addCase(I->first, I->second);
Chris Lattner00950542001-06-06 20:29:01 +00001562 }
Chris Lattner196850c2003-05-15 21:30:00 +00001563 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
1564 SwitchInst *S = new SwitchInst(getVal($2, $3),
1565 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
1566 $$ = S;
1567 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001568 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO ResolvedVal
1569 EXCEPT ResolvedVal {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001570 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001571 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001572
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001573 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1574 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001575 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001576 std::vector<const Type*> ParamTypes;
Chris Lattner2079fde2001-10-13 06:41:08 +00001577 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001578 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1579 I != E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001580 ParamTypes.push_back((*I)->getType());
1581 }
1582
1583 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1584 if (isVarArg) ParamTypes.pop_back();
1585
Chris Lattner79df7c02002-03-26 18:01:55 +00001586 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001587 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00001588 }
1589 delete $2;
1590
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001591 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00001592
1593 BasicBlock *Normal = dyn_cast<BasicBlock>($8);
1594 BasicBlock *Except = dyn_cast<BasicBlock>($10);
1595
1596 if (Normal == 0 || Except == 0)
1597 ThrowException("Invoke instruction without label destinations!");
1598
1599 // Create the call node...
1600 if (!$5) { // Has no arguments?
Chris Lattner9232b992003-04-22 18:42:41 +00001601 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001602 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001603 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00001604 // correctly!
1605 //
Chris Lattner79df7c02002-03-26 18:01:55 +00001606 FunctionType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1607 FunctionType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner9232b992003-04-22 18:42:41 +00001608 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001609
1610 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1611 if ((*ArgI)->getType() != *I)
1612 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001613 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001614
1615 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1616 ThrowException("Invalid number of parameters detected!");
1617
Chris Lattner6cdb0112001-11-26 16:54:11 +00001618 $$ = new InvokeInst(V, Normal, Except, *$5);
Chris Lattner2079fde2001-10-13 06:41:08 +00001619 }
1620 delete $5;
Chris Lattner36143fc2003-09-08 18:54:55 +00001621 }
1622 | UNWIND {
1623 $$ = new UnwindInst();
Chris Lattner51727be2002-06-04 21:58:56 +00001624 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001625
1626
Chris Lattner00950542001-06-06 20:29:01 +00001627
1628JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1629 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001630 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001631 if (V == 0)
1632 ThrowException("May only switch on a constant pool value!");
1633
Chris Lattner9232b992003-04-22 18:42:41 +00001634 $$->push_back(std::make_pair(V, cast<BasicBlock>(getVal($5, $6))));
Chris Lattner00950542001-06-06 20:29:01 +00001635 }
1636 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00001637 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001638 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001639
1640 if (V == 0)
1641 ThrowException("May only switch on a constant pool value!");
1642
Chris Lattner9232b992003-04-22 18:42:41 +00001643 $$->push_back(std::make_pair(V, cast<BasicBlock>(getVal($4, $5))));
Chris Lattner51727be2002-06-04 21:58:56 +00001644 };
Chris Lattner00950542001-06-06 20:29:01 +00001645
1646Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001647 // Is this definition named?? if so, assign the name...
1648 if (setValueName($2, $1)) { assert(0 && "No redefin allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001649 InsertValue($2);
1650 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001651};
Chris Lattner00950542001-06-06 20:29:01 +00001652
Chris Lattnerc24d2082001-06-11 15:04:20 +00001653PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Chris Lattner9232b992003-04-22 18:42:41 +00001654 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
1655 $$->push_back(std::make_pair(getVal(*$1, $3),
1656 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
Chris Lattner30c89792001-09-07 16:35:17 +00001657 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001658 }
1659 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1660 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001661 $1->push_back(std::make_pair(getVal($1->front().first->getType(), $4),
1662 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
Chris Lattner51727be2002-06-04 21:58:56 +00001663 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00001664
1665
Chris Lattner30c89792001-09-07 16:35:17 +00001666ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner9232b992003-04-22 18:42:41 +00001667 $$ = new std::vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001668 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001669 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001670 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001671 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001672 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00001673 };
Chris Lattner00950542001-06-06 20:29:01 +00001674
1675// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00001676ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001677
Chris Lattner4a6482b2002-09-10 19:57:26 +00001678InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1679 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint())
1680 ThrowException("Arithmetic operator requires integer or FP operands!");
1681 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1682 if ($$ == 0)
1683 ThrowException("binary operator returned null!");
1684 delete $2;
1685 }
1686 | LogicalOps Types ValueRef ',' ValueRef {
1687 if (!(*$2)->isIntegral())
1688 ThrowException("Logical operator requires integral operands!");
1689 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1690 if ($$ == 0)
1691 ThrowException("binary operator returned null!");
1692 delete $2;
1693 }
1694 | SetCondOps Types ValueRef ',' ValueRef {
Chris Lattner1cff96a2002-09-10 22:37:46 +00001695 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001696 if ($$ == 0)
1697 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001698 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001699 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00001700 | NOT ResolvedVal {
1701 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
1702 << " Replacing with 'xor'.\n";
1703
1704 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
1705 if (Ones == 0)
1706 ThrowException("Expected integral type for not instruction!");
1707
1708 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00001709 if ($$ == 0)
Chris Lattner699f1eb2002-08-14 17:12:33 +00001710 ThrowException("Could not create a xor instruction!");
Chris Lattner09083092001-07-08 04:57:15 +00001711 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001712 | ShiftOps ResolvedVal ',' ResolvedVal {
1713 if ($4->getType() != Type::UByteTy)
1714 ThrowException("Shift amount must be ubyte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001715 if (!$2->getType()->isInteger())
1716 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001717 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001718 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001719 | CAST ResolvedVal TO Types {
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001720 if (!$4->get()->isFirstClassType())
1721 ThrowException("cast instruction to a non-primitive type: '" +
1722 $4->get()->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001723 $$ = new CastInst($2, *$4);
1724 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001725 }
Chris Lattner8f77dae2003-05-08 02:44:12 +00001726 | VA_ARG ResolvedVal ',' Types {
Chris Lattner99e7ab72003-10-18 05:53:13 +00001727 // FIXME: This is emulation code for an obsolete syntax. This should be
1728 // removed at some point.
1729 if (!ObsoleteVarArgs) {
1730 std::cerr << "WARNING: this file uses obsolete features. "
1731 << "Assemble and disassemble to update it.\n";
1732 ObsoleteVarArgs = true;
1733 }
1734
1735 // First, load the valist...
1736 Instruction *CurVAList = new LoadInst($2, "");
1737 CurBB->getInstList().push_back(CurVAList);
1738
1739 // Emit the vaarg instruction.
1740 $$ = new VAArgInst(CurVAList, *$4);
1741
1742 // Now we must advance the pointer and update it in memory.
1743 Instruction *TheVANext = new VANextInst(CurVAList, *$4);
1744 CurBB->getInstList().push_back(TheVANext);
1745
1746 CurBB->getInstList().push_back(new StoreInst(TheVANext, $2));
1747 delete $4;
1748 }
1749 | VAARG ResolvedVal ',' Types {
1750 $$ = new VAArgInst($2, *$4);
1751 delete $4;
1752 }
1753 | VANEXT ResolvedVal ',' Types {
1754 $$ = new VANextInst($2, *$4);
Chris Lattner8f77dae2003-05-08 02:44:12 +00001755 delete $4;
1756 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00001757 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001758 const Type *Ty = $2->front().first->getType();
1759 $$ = new PHINode(Ty);
Chris Lattnerbd2531f2003-10-10 16:34:58 +00001760 $$->op_reserve($2->size()*2);
Chris Lattner00950542001-06-06 20:29:01 +00001761 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001762 if ($2->front().first->getType() != Ty)
1763 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001764 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001765 $2->pop_front();
1766 }
1767 delete $2; // Free the list...
1768 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001769 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001770 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001771 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001772
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001773 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1774 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001775 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001776 std::vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001777 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001778 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1779 I != E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00001780 ParamTypes.push_back((*I)->getType());
1781 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001782
1783 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1784 if (isVarArg) ParamTypes.pop_back();
1785
Chris Lattner79df7c02002-03-26 18:01:55 +00001786 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001787 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001788 }
Chris Lattner30c89792001-09-07 16:35:17 +00001789 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001790
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001791 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001792
Chris Lattner8b81bf52001-07-25 22:47:46 +00001793 // Create the call node...
1794 if (!$5) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00001795 // Make sure no arguments is a good thing!
1796 if (Ty->getNumParams() != 0)
1797 ThrowException("No arguments passed to a function that "
1798 "expects arguments!");
1799
Chris Lattner9232b992003-04-22 18:42:41 +00001800 $$ = new CallInst(V, std::vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001801 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001802 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00001803 // correctly!
1804 //
Chris Lattner79df7c02002-03-26 18:01:55 +00001805 FunctionType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1806 FunctionType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner9232b992003-04-22 18:42:41 +00001807 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001808
1809 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1810 if ((*ArgI)->getType() != *I)
1811 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001812 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001813
Chris Lattner8b81bf52001-07-25 22:47:46 +00001814 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001815 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001816
Chris Lattner6cdb0112001-11-26 16:54:11 +00001817 $$ = new CallInst(V, *$5);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001818 }
1819 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001820 }
1821 | MemoryInst {
1822 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001823 };
Chris Lattner00950542001-06-06 20:29:01 +00001824
Chris Lattner6cdb0112001-11-26 16:54:11 +00001825
1826// IndexList - List of indices for GEP based instructions...
1827IndexList : ',' ValueRefList {
Chris Lattner15c9c032003-09-08 18:20:29 +00001828 $$ = $2;
1829 } | /* empty */ {
1830 $$ = new std::vector<Value*>();
1831 };
1832
1833OptVolatile : VOLATILE {
1834 $$ = true;
1835 }
1836 | /* empty */ {
1837 $$ = false;
1838 };
1839
Chris Lattner027dcc52001-07-08 21:10:27 +00001840
Chris Lattner00950542001-06-06 20:29:01 +00001841MemoryInst : MALLOC Types {
Chris Lattner05804b72002-09-13 22:28:45 +00001842 $$ = new MallocInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00001843 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001844 }
1845 | MALLOC Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00001846 $$ = new MallocInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001847 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001848 }
1849 | ALLOCA Types {
Chris Lattner05804b72002-09-13 22:28:45 +00001850 $$ = new AllocaInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00001851 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001852 }
1853 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00001854 $$ = new AllocaInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001855 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001856 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001857 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00001858 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001859 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00001860 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001861 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001862 }
1863
Chris Lattner15c9c032003-09-08 18:20:29 +00001864 | OptVolatile LOAD Types ValueRef {
1865 if (!isa<PointerType>($3->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00001866 ThrowException("Can't load from nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00001867 (*$3)->getDescription());
Chris Lattner79ad13802003-09-08 20:29:46 +00001868 $$ = new LoadInst(getVal(*$3, $4), "", $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00001869 delete $3;
Chris Lattner027dcc52001-07-08 21:10:27 +00001870 }
Chris Lattner15c9c032003-09-08 18:20:29 +00001871 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
1872 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00001873 if (!PT)
Chris Lattner72e00252001-12-14 16:28:42 +00001874 ThrowException("Can't store to a nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00001875 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00001876 const Type *ElTy = PT->getElementType();
Chris Lattner15c9c032003-09-08 18:20:29 +00001877 if (ElTy != $3->getType())
1878 ThrowException("Can't store '" + $3->getType()->getDescription() +
Chris Lattner72e00252001-12-14 16:28:42 +00001879 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner0383cc42002-08-21 23:51:21 +00001880
Chris Lattner79ad13802003-09-08 20:29:46 +00001881 $$ = new StoreInst($3, getVal(*$5, $6), $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00001882 delete $5;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001883 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00001884 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00001885 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001886 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner30c89792001-09-07 16:35:17 +00001887 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner72e00252001-12-14 16:28:42 +00001888 ThrowException("Can't get element ptr '" + (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001889 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
1890 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00001891 };
Chris Lattner027dcc52001-07-08 21:10:27 +00001892
Chris Lattner00950542001-06-06 20:29:01 +00001893%%
Chris Lattner09083092001-07-08 04:57:15 +00001894int yyerror(const char *ErrorMsg) {
Chris Lattner9232b992003-04-22 18:42:41 +00001895 std::string where
1896 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001897 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Chris Lattner9232b992003-04-22 18:42:41 +00001898 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001899 if (yychar == YYEMPTY)
1900 errMsg += "end-of-file.";
1901 else
Chris Lattner9232b992003-04-22 18:42:41 +00001902 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001903 ThrowException(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00001904 return 0;
1905}