blob: 0981eabd82ed7d5c68b146dfd9de54b8762baba5 [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files --------*- C++ -*-===//
John Criswell856ba762003-10-21 15:17:13 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This file implements the bison parser for LLVM assembly languages files.
11//
Chris Lattnercf3056d2003-10-13 03:32:08 +000012//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +000013
Chris Lattner00950542001-06-06 20:29:01 +000014%{
15#include "ParserInternals.h"
Chris Lattner00950542001-06-06 20:29:01 +000016#include "llvm/SymbolTable.h"
17#include "llvm/Module.h"
Chris Lattner00950542001-06-06 20:29:01 +000018#include "llvm/iTerminators.h"
19#include "llvm/iMemory.h"
Chris Lattner1cff96a2002-09-10 22:37:46 +000020#include "llvm/iOperators.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000021#include "llvm/iPHINode.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000022#include "Support/STLExtras.h"
Chris Lattner00950542001-06-06 20:29:01 +000023#include <list>
Chris Lattnerc188eeb2002-07-30 18:54:25 +000024#include <utility>
Chris Lattner30c89792001-09-07 16:35:17 +000025#include <algorithm>
Chris Lattner00950542001-06-06 20:29:01 +000026
Chris Lattner386a3b72001-10-16 19:54:17 +000027int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
Chris Lattner09083092001-07-08 04:57:15 +000028int yylex(); // declaration" of xxx warnings.
Chris Lattner00950542001-06-06 20:29:01 +000029int yyparse();
30
Brian Gaeked0fde302003-11-11 22:41:34 +000031namespace llvm {
32
Chris Lattner00950542001-06-06 20:29:01 +000033static Module *ParserResult;
Chris Lattner9232b992003-04-22 18:42:41 +000034std::string CurFilename;
Chris Lattner00950542001-06-06 20:29:01 +000035
Chris Lattner30c89792001-09-07 16:35:17 +000036// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
37// relating to upreferences in the input stream.
38//
39//#define DEBUG_UPREFS 1
40#ifdef DEBUG_UPREFS
Chris Lattner699f1eb2002-08-14 17:12:33 +000041#define UR_OUT(X) std::cerr << X
Chris Lattner30c89792001-09-07 16:35:17 +000042#else
43#define UR_OUT(X)
44#endif
45
Vikram S. Adved3f7eb02002-07-14 22:59:28 +000046#define YYERROR_VERBOSE 1
47
Chris Lattner99e7ab72003-10-18 05:53:13 +000048// HACK ALERT: This variable is used to implement the automatic conversion of
49// variable argument instructions from their old to new forms. When this
50// compatiblity "Feature" is removed, this should be too.
51//
52static BasicBlock *CurBB;
53static bool ObsoleteVarArgs;
54
55
Chris Lattner7e708292002-06-25 16:13:24 +000056// This contains info used when building the body of a function. It is
57// destroyed when the function is completed.
Chris Lattner00950542001-06-06 20:29:01 +000058//
Chris Lattner9232b992003-04-22 18:42:41 +000059typedef std::vector<Value *> ValueList; // Numbered defs
60static void ResolveDefinitions(std::vector<ValueList> &LateResolvers,
61 std::vector<ValueList> *FutureLateResolvers = 0);
Chris Lattner00950542001-06-06 20:29:01 +000062
63static struct PerModuleInfo {
64 Module *CurrentModule;
Chris Lattner9232b992003-04-22 18:42:41 +000065 std::vector<ValueList> Values; // Module level numbered definitions
66 std::vector<ValueList> LateResolveValues;
67 std::vector<PATypeHolder> Types;
68 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000069
Chris Lattner2079fde2001-10-13 06:41:08 +000070 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
71 // references to global values. Global values may be referenced before they
72 // are defined, and if so, the temporary object that they represent is held
Chris Lattnere9bb2df2001-12-03 22:26:30 +000073 // here. This is used for forward references of ConstantPointerRefs.
Chris Lattner2079fde2001-10-13 06:41:08 +000074 //
Chris Lattner9232b992003-04-22 18:42:41 +000075 typedef std::map<std::pair<const PointerType *,
76 ValID>, GlobalVariable*> GlobalRefsType;
Chris Lattner2079fde2001-10-13 06:41:08 +000077 GlobalRefsType GlobalRefs;
78
Chris Lattner00950542001-06-06 20:29:01 +000079 void ModuleDone() {
Chris Lattner7e708292002-06-25 16:13:24 +000080 // If we could not resolve some functions at function compilation time
81 // (calls to functions before they are defined), resolve them now... Types
82 // are resolved when the constant pool has been completely parsed.
Chris Lattner30c89792001-09-07 16:35:17 +000083 //
Chris Lattner00950542001-06-06 20:29:01 +000084 ResolveDefinitions(LateResolveValues);
85
Chris Lattner2079fde2001-10-13 06:41:08 +000086 // Check to make sure that all global value forward references have been
87 // resolved!
88 //
89 if (!GlobalRefs.empty()) {
Chris Lattner9232b992003-04-22 18:42:41 +000090 std::string UndefinedReferences = "Unresolved global references exist:\n";
Chris Lattner749ce032002-03-11 22:12:39 +000091
92 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
93 I != E; ++I) {
94 UndefinedReferences += " " + I->first.first->getDescription() + " " +
95 I->first.second.getName() + "\n";
96 }
97 ThrowException(UndefinedReferences);
Chris Lattner2079fde2001-10-13 06:41:08 +000098 }
99
Chris Lattner7e708292002-06-25 16:13:24 +0000100 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000101 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +0000102 CurrentModule = 0;
103 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000104
105
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000106 // DeclareNewGlobalValue - Called every time a new GV has been defined. This
Chris Lattner2079fde2001-10-13 06:41:08 +0000107 // is used to remove things from the forward declaration map, resolving them
108 // to the correct thing as needed.
109 //
110 void DeclareNewGlobalValue(GlobalValue *GV, ValID D) {
111 // Check to see if there is a forward reference to this global variable...
112 // if there is, eliminate it and patch the reference to use the new def'n.
Chris Lattner9232b992003-04-22 18:42:41 +0000113 GlobalRefsType::iterator I =
114 GlobalRefs.find(std::make_pair(GV->getType(), D));
Chris Lattner2079fde2001-10-13 06:41:08 +0000115
116 if (I != GlobalRefs.end()) {
117 GlobalVariable *OldGV = I->second; // Get the placeholder...
Misha Brukman5560c9d2003-08-18 14:43:39 +0000118 I->first.second.destroy(); // Free string memory if necessary
Chris Lattner2079fde2001-10-13 06:41:08 +0000119
120 // Loop over all of the uses of the GlobalValue. The only thing they are
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000121 // allowed to be is ConstantPointerRef's.
Chris Lattnerfd059242003-10-15 16:48:29 +0000122 assert(OldGV->hasOneUse() && "Only one reference should exist!");
Chris Lattner9e932bd2002-10-14 03:28:42 +0000123 User *U = OldGV->use_back(); // Must be a ConstantPointerRef...
124 ConstantPointerRef *CPR = cast<ConstantPointerRef>(U);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000125
Chris Lattner9e932bd2002-10-14 03:28:42 +0000126 // Change the const pool reference to point to the real global variable
127 // now. This should drop a use from the OldGV.
128 CPR->mutateReferences(OldGV, GV);
129 assert(OldGV->use_empty() && "All uses should be gone now!");
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000130
131 // Remove OldGV from the module...
Chris Lattner2079fde2001-10-13 06:41:08 +0000132 CurrentModule->getGlobalList().remove(OldGV);
133 delete OldGV; // Delete the old placeholder
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000134
Chris Lattner2079fde2001-10-13 06:41:08 +0000135 // Remove the map entry for the global now that it has been created...
136 GlobalRefs.erase(I);
137 }
138 }
139
Chris Lattner00950542001-06-06 20:29:01 +0000140} CurModule;
141
Chris Lattner79df7c02002-03-26 18:01:55 +0000142static struct PerFunctionInfo {
Chris Lattner7e708292002-06-25 16:13:24 +0000143 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner00950542001-06-06 20:29:01 +0000144
Chris Lattner9232b992003-04-22 18:42:41 +0000145 std::vector<ValueList> Values; // Keep track of numbered definitions
146 std::vector<ValueList> LateResolveValues;
147 std::vector<PATypeHolder> Types;
148 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner15b69722003-11-12 04:40:30 +0000149 SymbolTable LocalSymtab;
Chris Lattner7e708292002-06-25 16:13:24 +0000150 bool isDeclare; // Is this function a forward declararation?
Chris Lattner00950542001-06-06 20:29:01 +0000151
Chris Lattner79df7c02002-03-26 18:01:55 +0000152 inline PerFunctionInfo() {
153 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000154 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000155 }
156
Chris Lattner79df7c02002-03-26 18:01:55 +0000157 inline void FunctionStart(Function *M) {
158 CurrentFunction = M;
Chris Lattner00950542001-06-06 20:29:01 +0000159 }
160
Chris Lattner79df7c02002-03-26 18:01:55 +0000161 void FunctionDone() {
Chris Lattner00950542001-06-06 20:29:01 +0000162 // If we could not resolve some blocks at parsing time (forward branches)
163 // resolve the branches now...
Chris Lattner386a3b72001-10-16 19:54:17 +0000164 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000165
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000166 // Make sure to resolve any constant expr references that might exist within
167 // the function we just declared itself.
168 ValID FID;
169 if (CurrentFunction->hasName()) {
170 FID = ValID::create((char*)CurrentFunction->getName().c_str());
171 } else {
172 unsigned Slot = CurrentFunction->getType()->getUniqueID();
173 assert(CurModule.Values.size() > Slot && "Function not inserted?");
174 // Figure out which slot number if is...
175 for (unsigned i = 0; ; ++i) {
176 assert(i < CurModule.Values[Slot].size() && "Function not found!");
177 if (CurModule.Values[Slot][i] == CurrentFunction) {
178 FID = ValID::create((int)i);
179 break;
180 }
181 }
182 }
183 CurModule.DeclareNewGlobalValue(CurrentFunction, FID);
184
Chris Lattner7e708292002-06-25 16:13:24 +0000185 Values.clear(); // Clear out function local definitions
Chris Lattner15b69722003-11-12 04:40:30 +0000186 Types.clear(); // Clear out function local types
187 LocalSymtab.clear(); // Clear out function local symbol table
Chris Lattner79df7c02002-03-26 18:01:55 +0000188 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000189 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000190 }
Chris Lattner394f2eb2003-10-16 20:12:13 +0000191} CurFun; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000192
Chris Lattner394f2eb2003-10-16 20:12:13 +0000193static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000194
Chris Lattner00950542001-06-06 20:29:01 +0000195
196//===----------------------------------------------------------------------===//
197// Code to handle definitions of all the types
198//===----------------------------------------------------------------------===//
199
Chris Lattner9232b992003-04-22 18:42:41 +0000200static int InsertValue(Value *D,
Chris Lattner394f2eb2003-10-16 20:12:13 +0000201 std::vector<ValueList> &ValueTab = CurFun.Values) {
Chris Lattner2079fde2001-10-13 06:41:08 +0000202 if (D->hasName()) return -1; // Is this a numbered definition?
203
204 // Yes, insert the value into the value table...
205 unsigned type = D->getType()->getUniqueID();
206 if (ValueTab.size() <= type)
207 ValueTab.resize(type+1, ValueList());
208 //printf("Values[%d][%d] = %d\n", type, ValueTab[type].size(), D);
209 ValueTab[type].push_back(D);
210 return ValueTab[type].size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000211}
212
Chris Lattner30c89792001-09-07 16:35:17 +0000213// TODO: FIXME when Type are not const
Chris Lattner9232b992003-04-22 18:42:41 +0000214static void InsertType(const Type *Ty, std::vector<PATypeHolder> &Types) {
Chris Lattner30c89792001-09-07 16:35:17 +0000215 Types.push_back(Ty);
216}
217
218static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000219 switch (D.Type) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000220 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000221 unsigned Num = (unsigned)D.Num;
222
223 // Module constants occupy the lowest numbered slots...
224 if (Num < CurModule.Types.size())
225 return CurModule.Types[Num];
226
227 Num -= CurModule.Types.size();
228
229 // Check that the number is within bounds...
Chris Lattner394f2eb2003-10-16 20:12:13 +0000230 if (Num <= CurFun.Types.size())
231 return CurFun.Types[Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000232 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000233 }
Chris Lattnerf8dff732002-07-18 05:18:37 +0000234 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000235 std::string Name(D.Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000236 SymbolTable *SymTab = 0;
Chris Lattner6e6026b2002-11-20 18:36:02 +0000237 Value *N = 0;
238 if (inFunctionScope()) {
Chris Lattner394f2eb2003-10-16 20:12:13 +0000239 SymTab = &CurFun.CurrentFunction->getSymbolTable();
Chris Lattner6e6026b2002-11-20 18:36:02 +0000240 N = SymTab->lookup(Type::TypeTy, Name);
241 }
Chris Lattner30c89792001-09-07 16:35:17 +0000242
243 if (N == 0) {
Chris Lattner7e708292002-06-25 16:13:24 +0000244 // Symbol table doesn't automatically chain yet... because the function
Chris Lattner30c89792001-09-07 16:35:17 +0000245 // hasn't been added to the module...
246 //
Chris Lattner6e6026b2002-11-20 18:36:02 +0000247 SymTab = &CurModule.CurrentModule->getSymbolTable();
248 N = SymTab->lookup(Type::TypeTy, Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000249 if (N == 0) break;
250 }
251
252 D.destroy(); // Free old strdup'd memory...
Chris Lattner949a3622003-07-23 15:30:06 +0000253 return cast<Type>(N);
Chris Lattner30c89792001-09-07 16:35:17 +0000254 }
255 default:
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000256 ThrowException("Internal parser error: Invalid symbol type reference!");
Chris Lattner30c89792001-09-07 16:35:17 +0000257 }
258
259 // If we reached here, we referenced either a symbol that we don't know about
260 // or an id number that hasn't been read yet. We may be referencing something
261 // forward, so just create an entry to be resolved later and get to it...
262 //
263 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
264
Chris Lattner9232b992003-04-22 18:42:41 +0000265 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000266 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
Chris Lattner4a42e902001-10-22 05:56:09 +0000267
Chris Lattner9232b992003-04-22 18:42:41 +0000268 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000269 if (I != LateResolver.end()) {
270 return I->second;
271 }
Chris Lattner30c89792001-09-07 16:35:17 +0000272
Chris Lattner82269592001-10-22 06:01:08 +0000273 Type *Typ = OpaqueType::get();
Chris Lattner9232b992003-04-22 18:42:41 +0000274 LateResolver.insert(std::make_pair(D, Typ));
Chris Lattner30c89792001-09-07 16:35:17 +0000275 return Typ;
276}
277
Chris Lattner9232b992003-04-22 18:42:41 +0000278static Value *lookupInSymbolTable(const Type *Ty, const std::string &Name) {
Chris Lattner6e6026b2002-11-20 18:36:02 +0000279 SymbolTable &SymTab =
Chris Lattner394f2eb2003-10-16 20:12:13 +0000280 inFunctionScope() ? CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner9705a152002-05-02 19:27:42 +0000281 CurModule.CurrentModule->getSymbolTable();
Chris Lattner6e6026b2002-11-20 18:36:02 +0000282 return SymTab.lookup(Ty, Name);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000283}
284
Chris Lattner2079fde2001-10-13 06:41:08 +0000285// getValNonImprovising - Look up the value specified by the provided type and
286// the provided ValID. If the value exists and has already been defined, return
287// it. Otherwise return null.
288//
289static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000290 if (isa<FunctionType>(Ty))
291 ThrowException("Functions are not values and "
292 "must be referenced as pointers");
Chris Lattner386a3b72001-10-16 19:54:17 +0000293
Chris Lattner30c89792001-09-07 16:35:17 +0000294 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000295 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000296 unsigned type = Ty->getUniqueID();
Chris Lattner00950542001-06-06 20:29:01 +0000297 unsigned Num = (unsigned)D.Num;
298
299 // Module constants occupy the lowest numbered slots...
300 if (type < CurModule.Values.size()) {
301 if (Num < CurModule.Values[type].size())
302 return CurModule.Values[type][Num];
303
304 Num -= CurModule.Values[type].size();
305 }
306
307 // Make sure that our type is within bounds
Chris Lattner394f2eb2003-10-16 20:12:13 +0000308 if (CurFun.Values.size() <= type) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000309
310 // Check that the number is within bounds...
Chris Lattner394f2eb2003-10-16 20:12:13 +0000311 if (CurFun.Values[type].size() <= Num) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000312
Chris Lattner394f2eb2003-10-16 20:12:13 +0000313 return CurFun.Values[type][Num];
Chris Lattner00950542001-06-06 20:29:01 +0000314 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000315
Chris Lattner1a1cb112001-09-30 22:46:54 +0000316 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000317 Value *N = lookupInSymbolTable(Ty, std::string(D.Name));
Chris Lattner2079fde2001-10-13 06:41:08 +0000318 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000319
320 D.destroy(); // Free old strdup'd memory...
321 return N;
322 }
323
Chris Lattner2079fde2001-10-13 06:41:08 +0000324 // Check to make sure that "Ty" is an integral type, and that our
325 // value will fit into the specified type...
326 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattnerd78700d2002-08-16 21:14:40 +0000327 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
328 ThrowException("Signed integral constant '" +
329 itostr(D.ConstPool64) + "' is invalid for type '" +
330 Ty->getDescription() + "'!");
331 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000332
333 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000334 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
335 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000336 ThrowException("Integral constant '" + utostr(D.UConstPool64) +
337 "' is invalid or out of range!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000338 } else { // This is really a signed reference. Transmogrify.
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000339 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000340 }
341 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000342 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000343 }
344
Chris Lattner2079fde2001-10-13 06:41:08 +0000345 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000346 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner2079fde2001-10-13 06:41:08 +0000347 ThrowException("FP constant invalid for type!!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000348 return ConstantFP::get(Ty, D.ConstPoolFP);
Chris Lattner2079fde2001-10-13 06:41:08 +0000349
350 case ValID::ConstNullVal: // Is it a null value?
Chris Lattner9b625032002-05-06 16:15:30 +0000351 if (!isa<PointerType>(Ty))
Chris Lattner2079fde2001-10-13 06:41:08 +0000352 ThrowException("Cannot create a a non pointer null!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000353 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner2079fde2001-10-13 06:41:08 +0000354
Chris Lattnerd78700d2002-08-16 21:14:40 +0000355 case ValID::ConstantVal: // Fully resolved constant?
356 if (D.ConstantValue->getType() != Ty)
357 ThrowException("Constant expression type different from required type!");
358 return D.ConstantValue;
359
Chris Lattner30c89792001-09-07 16:35:17 +0000360 default:
361 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000362 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000363 } // End of switch
364
Chris Lattner2079fde2001-10-13 06:41:08 +0000365 assert(0 && "Unhandled case!");
366 return 0;
367}
368
369
370// getVal - This function is identical to getValNonImprovising, except that if a
371// value is not already defined, it "improvises" by creating a placeholder var
372// that looks and acts just like the requested variable. When the value is
373// defined later, all uses of the placeholder variable are replaced with the
374// real thing.
375//
376static Value *getVal(const Type *Ty, const ValID &D) {
377 assert(Ty != Type::TypeTy && "Should use getTypeVal for types!");
378
379 // See if the value has already been defined...
380 Value *V = getValNonImprovising(Ty, D);
381 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000382
383 // If we reached here, we referenced either a symbol that we don't know about
384 // or an id number that hasn't been read yet. We may be referencing something
385 // forward, so just create an entry to be resolved later and get to it...
386 //
Chris Lattner00950542001-06-06 20:29:01 +0000387 Value *d = 0;
Chris Lattner30c89792001-09-07 16:35:17 +0000388 switch (Ty->getPrimitiveID()) {
389 case Type::LabelTyID: d = new BBPlaceHolder(Ty, D); break;
Chris Lattner30c89792001-09-07 16:35:17 +0000390 default: d = new ValuePlaceHolder(Ty, D); break;
Chris Lattner00950542001-06-06 20:29:01 +0000391 }
392
393 assert(d != 0 && "How did we not make something?");
Chris Lattner79df7c02002-03-26 18:01:55 +0000394 if (inFunctionScope())
Chris Lattner394f2eb2003-10-16 20:12:13 +0000395 InsertValue(d, CurFun.LateResolveValues);
Chris Lattner386a3b72001-10-16 19:54:17 +0000396 else
397 InsertValue(d, CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000398 return d;
399}
400
401
402//===----------------------------------------------------------------------===//
403// Code to handle forward references in instructions
404//===----------------------------------------------------------------------===//
405//
406// This code handles the late binding needed with statements that reference
407// values not defined yet... for example, a forward branch, or the PHI node for
408// a loop body.
409//
Chris Lattner394f2eb2003-10-16 20:12:13 +0000410// This keeps a table (CurFun.LateResolveValues) of all such forward references
Chris Lattner00950542001-06-06 20:29:01 +0000411// and back patchs after we are done.
412//
413
414// ResolveDefinitions - If we could not resolve some defs at parsing
415// time (forward branches, phi functions for loops, etc...) resolve the
416// defs now...
417//
Chris Lattner9232b992003-04-22 18:42:41 +0000418static void ResolveDefinitions(std::vector<ValueList> &LateResolvers,
419 std::vector<ValueList> *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000420 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
421 for (unsigned ty = 0; ty < LateResolvers.size(); ty++) {
422 while (!LateResolvers[ty].empty()) {
423 Value *V = LateResolvers[ty].back();
Chris Lattner386a3b72001-10-16 19:54:17 +0000424 assert(!isa<Type>(V) && "Types should be in LateResolveTypes!");
425
Chris Lattner00950542001-06-06 20:29:01 +0000426 LateResolvers[ty].pop_back();
427 ValID &DID = getValIDFromPlaceHolder(V);
428
Chris Lattner2079fde2001-10-13 06:41:08 +0000429 Value *TheRealValue = getValNonImprovising(Type::getUniqueIDType(ty),DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000430 if (TheRealValue) {
431 V->replaceAllUsesWith(TheRealValue);
432 delete V;
433 } else if (FutureLateResolvers) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000434 // Functions have their unresolved items forwarded to the module late
Chris Lattner386a3b72001-10-16 19:54:17 +0000435 // resolver table
436 InsertValue(V, *FutureLateResolvers);
437 } else {
Chris Lattner9705a152002-05-02 19:27:42 +0000438 if (DID.Type == ValID::NameVal)
Chris Lattner30c89792001-09-07 16:35:17 +0000439 ThrowException("Reference to an invalid definition: '" +DID.getName()+
440 "' of type '" + V->getType()->getDescription() + "'",
441 getLineNumFromPlaceHolder(V));
442 else
443 ThrowException("Reference to an invalid definition: #" +
444 itostr(DID.Num) + " of type '" +
445 V->getType()->getDescription() + "'",
446 getLineNumFromPlaceHolder(V));
447 }
Chris Lattner00950542001-06-06 20:29:01 +0000448 }
449 }
450
451 LateResolvers.clear();
452}
453
Chris Lattner4a42e902001-10-22 05:56:09 +0000454// ResolveTypeTo - A brand new type was just declared. This means that (if
455// name is not null) things referencing Name can be resolved. Otherwise, things
456// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000457//
Chris Lattner4a42e902001-10-22 05:56:09 +0000458static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner9232b992003-04-22 18:42:41 +0000459 std::vector<PATypeHolder> &Types = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000460 CurFun.Types : CurModule.Types;
Chris Lattner00950542001-06-06 20:29:01 +0000461
Chris Lattner4a42e902001-10-22 05:56:09 +0000462 ValID D;
463 if (Name) D = ValID::create(Name);
464 else D = ValID::create((int)Types.size());
Chris Lattner30c89792001-09-07 16:35:17 +0000465
Chris Lattner9232b992003-04-22 18:42:41 +0000466 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000467 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
Chris Lattner4a42e902001-10-22 05:56:09 +0000468
Chris Lattner9232b992003-04-22 18:42:41 +0000469 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000470 if (I != LateResolver.end()) {
Chris Lattner51727be2002-06-04 21:58:56 +0000471 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner4a42e902001-10-22 05:56:09 +0000472 LateResolver.erase(I);
473 }
474}
475
476// ResolveTypes - At this point, all types should be resolved. Any that aren't
477// are errors.
478//
Chris Lattner9232b992003-04-22 18:42:41 +0000479static void ResolveTypes(std::map<ValID, PATypeHolder> &LateResolveTypes) {
Chris Lattner4a42e902001-10-22 05:56:09 +0000480 if (!LateResolveTypes.empty()) {
Chris Lattner82269592001-10-22 06:01:08 +0000481 const ValID &DID = LateResolveTypes.begin()->first;
Chris Lattner4a42e902001-10-22 05:56:09 +0000482
483 if (DID.Type == ValID::NameVal)
Chris Lattner82269592001-10-22 06:01:08 +0000484 ThrowException("Reference to an invalid type: '" +DID.getName() + "'");
Chris Lattner4a42e902001-10-22 05:56:09 +0000485 else
Chris Lattner82269592001-10-22 06:01:08 +0000486 ThrowException("Reference to an invalid type: #" + itostr(DID.Num));
Chris Lattner30c89792001-09-07 16:35:17 +0000487 }
488}
489
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000490
Chris Lattner1781aca2001-09-18 04:00:54 +0000491// setValueName - Set the specified value to the name given. The name may be
492// null potentially, in which case this is a noop. The string passed in is
493// assumed to be a malloc'd string buffer, and is freed by this function.
494//
Chris Lattnerb7474512001-10-03 15:39:04 +0000495// This function returns true if the value has already been defined, but is
496// allowed to be redefined in the specified context. If the name is a new name
497// for the typeplane, false is returned.
498//
499static bool setValueName(Value *V, char *NameStr) {
500 if (NameStr == 0) return false;
Chris Lattner386a3b72001-10-16 19:54:17 +0000501
Chris Lattner9232b992003-04-22 18:42:41 +0000502 std::string Name(NameStr); // Copy string
Chris Lattner1781aca2001-09-18 04:00:54 +0000503 free(NameStr); // Free old string
504
Chris Lattner2079fde2001-10-13 06:41:08 +0000505 if (V->getType() == Type::VoidTy)
506 ThrowException("Can't assign name '" + Name +
507 "' to a null valued instruction!");
508
Chris Lattner6e6026b2002-11-20 18:36:02 +0000509 SymbolTable &ST = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000510 CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner6e6026b2002-11-20 18:36:02 +0000511 CurModule.CurrentModule->getSymbolTable();
Chris Lattner30c89792001-09-07 16:35:17 +0000512
Chris Lattner6e6026b2002-11-20 18:36:02 +0000513 Value *Existing = ST.lookup(V->getType(), Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000514 if (Existing) { // Inserting a name that is already defined???
515 // There is only one case where this is allowed: when we are refining an
516 // opaque type. In this case, Existing will be an opaque type.
Chris Lattner949a3622003-07-23 15:30:06 +0000517 if (const Type *Ty = dyn_cast<Type>(Existing)) {
Chris Lattner51727be2002-06-04 21:58:56 +0000518 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Ty)) {
Chris Lattner30c89792001-09-07 16:35:17 +0000519 // We ARE replacing an opaque type!
Chris Lattner51727be2002-06-04 21:58:56 +0000520 ((OpaqueType*)OpTy)->refineAbstractTypeTo(cast<Type>(V));
Chris Lattnerb7474512001-10-03 15:39:04 +0000521 return true;
Chris Lattner30c89792001-09-07 16:35:17 +0000522 }
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000523 }
Chris Lattner30c89792001-09-07 16:35:17 +0000524
Chris Lattner9636a912001-10-01 16:18:37 +0000525 // Otherwise, we are a simple redefinition of a value, check to see if it
526 // is defined the same as the old one...
Chris Lattneradf99702003-03-03 23:28:55 +0000527 if (const Type *Ty = dyn_cast<Type>(Existing)) {
528 if (Ty == cast<Type>(V)) return true; // Yes, it's equal.
Chris Lattner699f1eb2002-08-14 17:12:33 +0000529 // std::cerr << "Type: " << Ty->getDescription() << " != "
Chris Lattner949a3622003-07-23 15:30:06 +0000530 // << cast<Type>(V)->getDescription() << "!\n";
Chris Lattneradf99702003-03-03 23:28:55 +0000531 } else if (const Constant *C = dyn_cast<Constant>(Existing)) {
532 if (C == V) return true; // Constants are equal to themselves
Chris Lattnerb7474512001-10-03 15:39:04 +0000533 } else if (GlobalVariable *EGV = dyn_cast<GlobalVariable>(Existing)) {
Chris Lattner43efcbf2001-10-03 19:35:57 +0000534 // We are allowed to redefine a global variable in two circumstances:
535 // 1. If at least one of the globals is uninitialized or
536 // 2. If both initializers have the same value.
537 //
Chris Lattner89219832001-10-03 19:35:04 +0000538 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000539 if (!EGV->hasInitializer() || !GV->hasInitializer() ||
540 EGV->getInitializer() == GV->getInitializer()) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000541
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000542 // Make sure the existing global version gets the initializer! Make
543 // sure that it also gets marked const if the new version is.
Chris Lattner89219832001-10-03 19:35:04 +0000544 if (GV->hasInitializer() && !EGV->hasInitializer())
545 EGV->setInitializer(GV->getInitializer());
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000546 if (GV->isConstant())
547 EGV->setConstant(true);
Chris Lattner4ad02e72003-04-16 20:28:45 +0000548 EGV->setLinkage(GV->getLinkage());
Chris Lattner89219832001-10-03 19:35:04 +0000549
Chris Lattner2079fde2001-10-13 06:41:08 +0000550 delete GV; // Destroy the duplicate!
Chris Lattner89219832001-10-03 19:35:04 +0000551 return true; // They are equivalent!
552 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000553 }
Chris Lattner9636a912001-10-01 16:18:37 +0000554 }
Chris Lattner0b268fb2003-11-25 03:54:16 +0000555
Chris Lattner2079fde2001-10-13 06:41:08 +0000556 ThrowException("Redefinition of value named '" + Name + "' in the '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000557 V->getType()->getDescription() + "' type plane!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000558 }
Chris Lattner00950542001-06-06 20:29:01 +0000559
Chris Lattner15b69722003-11-12 04:40:30 +0000560 // Set the name
Chris Lattner6e6026b2002-11-20 18:36:02 +0000561 V->setName(Name, &ST);
Chris Lattner15b69722003-11-12 04:40:30 +0000562
563 // If we're in function scope
564 if (inFunctionScope()) {
565 // Look up the symbol in the function's local symboltable
566 Existing = CurFun.LocalSymtab.lookup(V->getType(),Name);
567
568 // If it already exists
569 if (Existing) {
Chris Lattner15b69722003-11-12 04:40:30 +0000570 // Bail
571 ThrowException("Redefinition of value named '" + Name + "' in the '" +
572 V->getType()->getDescription() + "' type plane!");
573
574 // otherwise, since it doesn't exist
575 } else {
576 // Insert it.
577 CurFun.LocalSymtab.insert(V);
578 }
579 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000580 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000581}
582
Chris Lattner8896eda2001-07-09 19:38:36 +0000583
Chris Lattner30c89792001-09-07 16:35:17 +0000584//===----------------------------------------------------------------------===//
585// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000586//
Chris Lattner8896eda2001-07-09 19:38:36 +0000587
Chris Lattner3b073862004-02-09 03:19:29 +0000588// TypeContains - Returns true if Ty directly contains E in it.
Chris Lattner30c89792001-09-07 16:35:17 +0000589//
590static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3b073862004-02-09 03:19:29 +0000591 return find(Ty->subtype_begin(), Ty->subtype_end(), E) != Ty->subtype_end();
592}
593
594namespace {
595 struct UpRefRecord {
596 // NestingLevel - The number of nesting levels that need to be popped before
597 // this type is resolved.
598 unsigned NestingLevel;
599
600 // LastContainedTy - This is the type at the current binding level for the
601 // type. Every time we reduce the nesting level, this gets updated.
602 const Type *LastContainedTy;
603
604 // UpRefTy - This is the actual opaque type that the upreference is
605 // represented with.
606 OpaqueType *UpRefTy;
607
608 UpRefRecord(unsigned NL, OpaqueType *URTy)
609 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
610 };
Chris Lattner30c89792001-09-07 16:35:17 +0000611}
Chris Lattner698b56e2001-07-20 19:15:08 +0000612
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000613// UpRefs - A list of the outstanding upreferences that need to be resolved.
Chris Lattner3b073862004-02-09 03:19:29 +0000614static std::vector<UpRefRecord> UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000615
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000616/// HandleUpRefs - Every time we finish a new layer of types, this function is
617/// called. It loops through the UpRefs vector, which is a list of the
618/// currently active types. For each type, if the up reference is contained in
619/// the newly completed type, we decrement the level count. When the level
620/// count reaches zero, the upreferenced type is the type that is passed in:
621/// thus we can complete the cycle.
622///
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000623static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner2c37c182004-02-09 03:03:10 +0000624 if (!ty->isAbstract()) return ty;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000625 PATypeHolder Ty(ty);
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000626 UR_OUT("Type '" << Ty->getDescription() <<
Chris Lattner5084d032001-11-02 07:46:26 +0000627 "' newly formed. Resolving upreferences.\n" <<
628 UpRefs.size() << " upreferences active!\n");
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000629 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Chris Lattner5084d032001-11-02 07:46:26 +0000630 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Chris Lattner30c89792001-09-07 16:35:17 +0000631 << UpRefs[i].second->getDescription() << ") = "
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000632 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner3b073862004-02-09 03:19:29 +0000633 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
634 // Decrement level of upreference
635 unsigned Level = --UpRefs[i].NestingLevel;
636 UpRefs[i].LastContainedTy = Ty;
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000637 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Chris Lattner30c89792001-09-07 16:35:17 +0000638 if (Level == 0) { // Upreference should be resolved!
Chris Lattner5084d032001-11-02 07:46:26 +0000639 UR_OUT(" * Resolving upreference for "
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000640 << UpRefs[i].second->getDescription() << "\n";
Chris Lattner3b073862004-02-09 03:19:29 +0000641 std::string OldName = UpRefs[i].UpRefTy->getDescription());
642 UpRefs[i].UpRefTy->refineAbstractTypeTo(Ty);
Chris Lattner5084d032001-11-02 07:46:26 +0000643 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000644 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
645 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
646 --i; // Do not skip the next element...
Chris Lattner30c89792001-09-07 16:35:17 +0000647 }
648 }
Chris Lattner8896eda2001-07-09 19:38:36 +0000649 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000650
Chris Lattner8896eda2001-07-09 19:38:36 +0000651 return Ty;
652}
653
Chris Lattner30c89792001-09-07 16:35:17 +0000654
Chris Lattner00950542001-06-06 20:29:01 +0000655//===----------------------------------------------------------------------===//
656// RunVMAsmParser - Define an interface to this parser
657//===----------------------------------------------------------------------===//
658//
Chris Lattner9232b992003-04-22 18:42:41 +0000659Module *RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000660 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000661 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000662 llvmAsmlineno = 1; // Reset the current line number...
Chris Lattner99e7ab72003-10-18 05:53:13 +0000663 ObsoleteVarArgs = false;
Chris Lattner00950542001-06-06 20:29:01 +0000664
Chris Lattner75f20532003-04-22 18:02:52 +0000665 // Allocate a new module to read
666 CurModule.CurrentModule = new Module(Filename);
Chris Lattner42570982003-11-26 07:24:58 +0000667
668 try {
669 yyparse(); // Parse the file.
670 } catch (...) {
671 // Clear the symbol table so it doesn't complain when it
672 // gets destructed
673 CurFun.LocalSymtab.clear();
674 throw;
675 }
Chris Lattner99e7ab72003-10-18 05:53:13 +0000676
Chris Lattner00950542001-06-06 20:29:01 +0000677 Module *Result = ParserResult;
Chris Lattner99e7ab72003-10-18 05:53:13 +0000678
679 // Check to see if they called va_start but not va_arg..
680 if (!ObsoleteVarArgs)
681 if (Function *F = Result->getNamedFunction("llvm.va_start"))
682 if (F->asize() == 1) {
683 std::cerr << "WARNING: this file uses obsolete features. "
684 << "Assemble and disassemble to update it.\n";
685 ObsoleteVarArgs = true;
686 }
687
688
689 if (ObsoleteVarArgs) {
690 // If the user is making use of obsolete varargs intrinsics, adjust them for
691 // the user.
692 if (Function *F = Result->getNamedFunction("llvm.va_start")) {
693 assert(F->asize() == 1 && "Obsolete va_start takes 1 argument!");
694
695 const Type *RetTy = F->getFunctionType()->getParamType(0);
696 RetTy = cast<PointerType>(RetTy)->getElementType();
697 Function *NF = Result->getOrInsertFunction("llvm.va_start", RetTy, 0);
698
699 while (!F->use_empty()) {
700 CallInst *CI = cast<CallInst>(F->use_back());
701 Value *V = new CallInst(NF, "", CI);
702 new StoreInst(V, CI->getOperand(1), CI);
703 CI->getParent()->getInstList().erase(CI);
704 }
705 Result->getFunctionList().erase(F);
706 }
707
708 if (Function *F = Result->getNamedFunction("llvm.va_end")) {
709 assert(F->asize() == 1 && "Obsolete va_end takes 1 argument!");
710 const Type *ArgTy = F->getFunctionType()->getParamType(0);
711 ArgTy = cast<PointerType>(ArgTy)->getElementType();
712 Function *NF = Result->getOrInsertFunction("llvm.va_end", Type::VoidTy,
713 ArgTy, 0);
714
715 while (!F->use_empty()) {
716 CallInst *CI = cast<CallInst>(F->use_back());
717 Value *V = new LoadInst(CI->getOperand(1), "", CI);
718 new CallInst(NF, V, "", CI);
719 CI->getParent()->getInstList().erase(CI);
720 }
721 Result->getFunctionList().erase(F);
722 }
723
724 if (Function *F = Result->getNamedFunction("llvm.va_copy")) {
725 assert(F->asize() == 2 && "Obsolete va_copy takes 2 argument!");
726 const Type *ArgTy = F->getFunctionType()->getParamType(0);
727 ArgTy = cast<PointerType>(ArgTy)->getElementType();
728 Function *NF = Result->getOrInsertFunction("llvm.va_copy", ArgTy,
729 ArgTy, 0);
730
731 while (!F->use_empty()) {
732 CallInst *CI = cast<CallInst>(F->use_back());
733 Value *V = new CallInst(NF, CI->getOperand(2), "", CI);
734 new StoreInst(V, CI->getOperand(1), CI);
735 CI->getParent()->getInstList().erase(CI);
736 }
737 Result->getFunctionList().erase(F);
738 }
739 }
740
Chris Lattner00950542001-06-06 20:29:01 +0000741 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
742 ParserResult = 0;
743
744 return Result;
745}
746
Brian Gaeked0fde302003-11-11 22:41:34 +0000747} // End llvm namespace
748
749using namespace llvm;
750
Chris Lattner00950542001-06-06 20:29:01 +0000751%}
752
753%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000754 llvm::Module *ModuleVal;
755 llvm::Function *FunctionVal;
756 std::pair<llvm::PATypeHolder*, char*> *ArgVal;
757 llvm::BasicBlock *BasicBlockVal;
758 llvm::TerminatorInst *TermInstVal;
759 llvm::Instruction *InstVal;
760 llvm::Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000761
Brian Gaeked0fde302003-11-11 22:41:34 +0000762 const llvm::Type *PrimType;
763 llvm::PATypeHolder *TypeVal;
764 llvm::Value *ValueVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000765
Brian Gaeked0fde302003-11-11 22:41:34 +0000766 std::vector<std::pair<llvm::PATypeHolder*,char*> > *ArgList;
767 std::vector<llvm::Value*> *ValueList;
768 std::list<llvm::PATypeHolder> *TypeList;
769 std::list<std::pair<llvm::Value*,
770 llvm::BasicBlock*> > *PHIList; // Represent the RHS of PHI node
771 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
772 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000773
Brian Gaeked0fde302003-11-11 22:41:34 +0000774 llvm::GlobalValue::LinkageTypes Linkage;
Chris Lattner30c89792001-09-07 16:35:17 +0000775 int64_t SInt64Val;
776 uint64_t UInt64Val;
777 int SIntVal;
778 unsigned UIntVal;
779 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000780 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000781
Chris Lattner30c89792001-09-07 16:35:17 +0000782 char *StrVal; // This memory is strdup'd!
Brian Gaeked0fde302003-11-11 22:41:34 +0000783 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000784
Brian Gaeked0fde302003-11-11 22:41:34 +0000785 llvm::Instruction::BinaryOps BinaryOpVal;
786 llvm::Instruction::TermOps TermOpVal;
787 llvm::Instruction::MemoryOps MemOpVal;
788 llvm::Instruction::OtherOps OtherOpVal;
789 llvm::Module::Endianness Endianness;
Chris Lattner00950542001-06-06 20:29:01 +0000790}
791
Chris Lattner79df7c02002-03-26 18:01:55 +0000792%type <ModuleVal> Module FunctionList
793%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000794%type <BasicBlockVal> BasicBlock InstructionList
795%type <TermInstVal> BBTerminatorInst
796%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000797%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000798%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000799%type <ArgList> ArgList ArgListH
800%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000801%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000802%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000803%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000804%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000805%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +0000806%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner15c9c032003-09-08 18:20:29 +0000807%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattner4ad02e72003-04-16 20:28:45 +0000808%type <Linkage> OptLinkage
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000809%type <Endianness> BigOrLittle
Chris Lattner00950542001-06-06 20:29:01 +0000810
Chris Lattner2079fde2001-10-13 06:41:08 +0000811// ValueRef - Unresolved reference to a definition or BB
812%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000813%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000814// Tokens and types for handling constant integer values
815//
816// ESINT64VAL - A negative number within long long range
817%token <SInt64Val> ESINT64VAL
818
819// EUINT64VAL - A positive number within uns. long long range
820%token <UInt64Val> EUINT64VAL
821%type <SInt64Val> EINT64VAL
822
823%token <SIntVal> SINTVAL // Signed 32 bit ints...
824%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
825%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000826%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000827
828// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000829%type <TypeVal> Types TypesV UpRTypes UpRTypesV
830%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000831%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
832%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000833
Chris Lattner6c23f572003-08-22 05:42:10 +0000834%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
835%type <StrVal> Name OptName OptAssign
Chris Lattner00950542001-06-06 20:29:01 +0000836
837
Chris Lattnerf4600482003-06-28 20:01:34 +0000838%token IMPLEMENTATION ZEROINITIALIZER TRUE FALSE BEGINTOK ENDTOK
Chris Lattner15c9c032003-09-08 18:20:29 +0000839%token DECLARE GLOBAL CONSTANT VOLATILE
Chris Lattnera58e3a12004-02-08 21:48:25 +0000840%token TO DOTDOTDOT NULL_TOK CONST INTERNAL LINKONCE WEAK APPENDING
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000841%token OPAQUE NOT EXTERNAL TARGET ENDIAN POINTERSIZE LITTLE BIG
Chris Lattner00950542001-06-06 20:29:01 +0000842
843// Basic Block Terminating Operators
Chris Lattner36143fc2003-09-08 18:54:55 +0000844%token <TermOpVal> RET BR SWITCH INVOKE UNWIND
Chris Lattner00950542001-06-06 20:29:01 +0000845
Chris Lattner00950542001-06-06 20:29:01 +0000846// Binary Operators
847%type <BinaryOpVal> BinaryOps // all the binary operators
Chris Lattner4a6482b2002-09-10 19:57:26 +0000848%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattner42c9e772001-10-20 09:32:59 +0000849%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000850%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000851
852// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000853%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000854
Chris Lattner027dcc52001-07-08 21:10:27 +0000855// Other Operators
856%type <OtherOpVal> ShiftOps
Chris Lattner3b237fc2003-10-19 21:34:28 +0000857%token <OtherOpVal> PHI_TOK CALL CAST SHL SHR VAARG VANEXT
Chris Lattner99e7ab72003-10-18 05:53:13 +0000858%token VA_ARG // FIXME: OBSOLETE
Chris Lattner027dcc52001-07-08 21:10:27 +0000859
Chris Lattner00950542001-06-06 20:29:01 +0000860%start Module
861%%
862
863// Handle constant integer size restriction and conversion...
864//
Chris Lattner51727be2002-06-04 21:58:56 +0000865INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +0000866INTVAL : UINTVAL {
867 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
868 ThrowException("Value too large for type!");
869 $$ = (int32_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000870};
Chris Lattner00950542001-06-06 20:29:01 +0000871
872
Chris Lattner51727be2002-06-04 21:58:56 +0000873EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +0000874EINT64VAL : EUINT64VAL {
875 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
876 ThrowException("Value too large for type!");
877 $$ = (int64_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000878};
Chris Lattner00950542001-06-06 20:29:01 +0000879
Chris Lattner00950542001-06-06 20:29:01 +0000880// Operations that are notably excluded from this list include:
881// RET, BR, & SWITCH because they end basic blocks and are treated specially.
882//
Chris Lattner4a6482b2002-09-10 19:57:26 +0000883ArithmeticOps: ADD | SUB | MUL | DIV | REM;
884LogicalOps : AND | OR | XOR;
885SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
886BinaryOps : ArithmeticOps | LogicalOps | SetCondOps;
887
Chris Lattner51727be2002-06-04 21:58:56 +0000888ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +0000889
Chris Lattnere98dda62001-07-14 06:10:16 +0000890// These are some types that allow classification if we only want a particular
891// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +0000892SIntType : LONG | INT | SHORT | SBYTE;
893UIntType : ULONG | UINT | USHORT | UBYTE;
894IntType : SIntType | UIntType;
895FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +0000896
Chris Lattnere98dda62001-07-14 06:10:16 +0000897// OptAssign - Value producing statements have an optional assignment component
Chris Lattner6c23f572003-08-22 05:42:10 +0000898OptAssign : Name '=' {
Chris Lattner00950542001-06-06 20:29:01 +0000899 $$ = $1;
900 }
901 | /*empty*/ {
902 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +0000903 };
Chris Lattner00950542001-06-06 20:29:01 +0000904
Chris Lattner4ad02e72003-04-16 20:28:45 +0000905OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
906 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
Chris Lattner72ac148d2003-10-16 18:29:00 +0000907 WEAK { $$ = GlobalValue::WeakLinkage; } |
Chris Lattner4ad02e72003-04-16 20:28:45 +0000908 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
909 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattner30c89792001-09-07 16:35:17 +0000910
911//===----------------------------------------------------------------------===//
912// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +0000913// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +0000914// access to it, a user must explicitly use TypesV.
915//
916
917// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +0000918TypesV : Types | VOID { $$ = new PATypeHolder($1); };
919UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +0000920
921Types : UpRTypes {
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000922 if (!UpRefs.empty())
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000923 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
924 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000925 };
Chris Lattner30c89792001-09-07 16:35:17 +0000926
927
928// Derived types are added later...
929//
Chris Lattner51727be2002-06-04 21:58:56 +0000930PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
931PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000932UpRTypes : OPAQUE {
933 $$ = new PATypeHolder(OpaqueType::get());
934 }
935 | PrimType {
936 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +0000937 };
Chris Lattnerd78700d2002-08-16 21:14:40 +0000938UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000939 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +0000940};
Chris Lattner30c89792001-09-07 16:35:17 +0000941
Chris Lattner30c89792001-09-07 16:35:17 +0000942// Include derived types in the Types production.
943//
944UpRTypes : '\\' EUINT64VAL { // Type UpReference
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000945 if ($2 > (uint64_t)~0U) ThrowException("Value out of range!");
Chris Lattner30c89792001-09-07 16:35:17 +0000946 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3b073862004-02-09 03:19:29 +0000947 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000948 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +0000949 UR_OUT("New Upreference!\n");
950 }
Chris Lattner79df7c02002-03-26 18:01:55 +0000951 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner9232b992003-04-22 18:42:41 +0000952 std::vector<const Type*> Params;
Chris Lattner697954c2002-01-20 22:54:45 +0000953 mapto($3->begin(), $3->end(), std::back_inserter(Params),
Chris Lattner876dc572003-10-02 19:00:34 +0000954 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner2079fde2001-10-13 06:41:08 +0000955 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
956 if (isVarArg) Params.pop_back();
957
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000958 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +0000959 delete $3; // Delete the argument list
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000960 delete $1; // Delete the return type handle
Chris Lattner30c89792001-09-07 16:35:17 +0000961 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000962 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000963 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000964 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +0000965 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000966 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +0000967 std::vector<const Type*> Elements;
Chris Lattner697954c2002-01-20 22:54:45 +0000968 mapto($2->begin(), $2->end(), std::back_inserter(Elements),
Chris Lattner876dc572003-10-02 19:00:34 +0000969 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner30c89792001-09-07 16:35:17 +0000970
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000971 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000972 delete $2;
973 }
974 | '{' '}' { // Empty structure type?
Chris Lattner9232b992003-04-22 18:42:41 +0000975 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000976 }
977 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000978 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000979 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000980 };
Chris Lattner30c89792001-09-07 16:35:17 +0000981
Chris Lattner7e708292002-06-25 16:13:24 +0000982// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +0000983// declaration type lists
984//
985TypeListI : UpRTypes {
Chris Lattner9232b992003-04-22 18:42:41 +0000986 $$ = new std::list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +0000987 $$->push_back(*$1); delete $1;
988 }
989 | TypeListI ',' UpRTypes {
990 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +0000991 };
Chris Lattner30c89792001-09-07 16:35:17 +0000992
Chris Lattner7e708292002-06-25 16:13:24 +0000993// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +0000994ArgTypeListI : TypeListI
995 | TypeListI ',' DOTDOTDOT {
996 ($$=$1)->push_back(Type::VoidTy);
997 }
998 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +0000999 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +00001000 }
1001 | /*empty*/ {
Chris Lattner9232b992003-04-22 18:42:41 +00001002 $$ = new std::list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +00001003 };
Chris Lattner30c89792001-09-07 16:35:17 +00001004
Chris Lattnere98dda62001-07-14 06:10:16 +00001005// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +00001006// production is used ONLY to represent constants that show up AFTER a 'const',
1007// 'constant' or 'global' token at global scope. Constants that can be inlined
1008// into other expressions (such as integers and constexprs) are handled by the
1009// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +00001010//
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001011ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Chris Lattner949a3622003-07-23 15:30:06 +00001012 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001013 if (ATy == 0)
1014 ThrowException("Cannot make array constant with type: '" +
1015 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001016 const Type *ETy = ATy->getElementType();
1017 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +00001018
Chris Lattner30c89792001-09-07 16:35:17 +00001019 // Verify that we have the correct size...
1020 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +00001021 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +00001022 utostr($3->size()) + " arguments, but has size of " +
1023 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +00001024
Chris Lattner30c89792001-09-07 16:35:17 +00001025 // Verify all elements are correct type!
1026 for (unsigned i = 0; i < $3->size(); i++) {
1027 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +00001028 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001029 ETy->getDescription() +"' as required!\nIt is of type '"+
1030 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +00001031 }
1032
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001033 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001034 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001035 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001036 | Types '[' ']' {
Chris Lattner949a3622003-07-23 15:30:06 +00001037 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001038 if (ATy == 0)
1039 ThrowException("Cannot make array constant with type: '" +
1040 (*$1)->getDescription() + "'!");
1041
1042 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +00001043 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +00001044 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +00001045 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattner9232b992003-04-22 18:42:41 +00001046 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +00001047 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001048 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001049 | Types 'c' STRINGCONSTANT {
Chris Lattner949a3622003-07-23 15:30:06 +00001050 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001051 if (ATy == 0)
1052 ThrowException("Cannot make array constant with type: '" +
1053 (*$1)->getDescription() + "'!");
1054
Chris Lattner30c89792001-09-07 16:35:17 +00001055 int NumElements = ATy->getNumElements();
1056 const Type *ETy = ATy->getElementType();
1057 char *EndStr = UnEscapeLexed($3, true);
1058 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +00001059 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +00001060 itostr((int)(EndStr-$3)) +
1061 " when array has size " + itostr(NumElements) + "!");
Chris Lattner9232b992003-04-22 18:42:41 +00001062 std::vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +00001063 if (ETy == Type::SByteTy) {
1064 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001065 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +00001066 } else if (ETy == Type::UByteTy) {
1067 for (char *C = $3; C != EndStr; ++C)
Chris Lattnerbae362f2003-01-30 22:24:26 +00001068 Vals.push_back(ConstantUInt::get(ETy, (unsigned char)*C));
Chris Lattner93750fa2001-07-28 17:48:55 +00001069 } else {
Chris Lattner30c89792001-09-07 16:35:17 +00001070 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +00001071 ThrowException("Cannot build string arrays of non byte sized elements!");
1072 }
Chris Lattner30c89792001-09-07 16:35:17 +00001073 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001074 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +00001075 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001076 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001077 | Types '{' ConstVector '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001078 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001079 if (STy == 0)
1080 ThrowException("Cannot make struct constant with type: '" +
1081 (*$1)->getDescription() + "'!");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001082
Chris Lattnerc6212f12003-05-21 16:06:56 +00001083 if ($3->size() != STy->getNumContainedTypes())
1084 ThrowException("Illegal number of initializers for structure type!");
1085
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001086 // Check to ensure that constants are compatible with the type initializer!
1087 for (unsigned i = 0, e = $3->size(); i != e; ++i)
1088 if ((*$3)[i]->getType() != STy->getElementTypes()[i])
1089 ThrowException("Expected type '" +
1090 STy->getElementTypes()[i]->getDescription() +
1091 "' for element #" + utostr(i) +
1092 " of structure initializer!");
1093
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001094 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001095 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001096 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001097 | Types '{' '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001098 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001099 if (STy == 0)
1100 ThrowException("Cannot make struct constant with type: '" +
1101 (*$1)->getDescription() + "'!");
1102
1103 if (STy->getNumContainedTypes() != 0)
1104 ThrowException("Illegal number of initializers for structure type!");
1105
1106 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1107 delete $1;
1108 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001109 | Types NULL_TOK {
Chris Lattner949a3622003-07-23 15:30:06 +00001110 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001111 if (PTy == 0)
1112 ThrowException("Cannot make null pointer constant with type: '" +
1113 (*$1)->getDescription() + "'!");
1114
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001115 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001116 delete $1;
1117 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001118 | Types SymbolicValueRef {
Chris Lattner949a3622003-07-23 15:30:06 +00001119 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001120 if (Ty == 0)
1121 ThrowException("Global const reference must be a pointer type!");
1122
Chris Lattner3101c252002-08-15 17:58:33 +00001123 // ConstExprs can exist in the body of a function, thus creating
1124 // ConstantPointerRefs whenever they refer to a variable. Because we are in
1125 // the context of a function, getValNonImprovising will search the functions
1126 // symbol table instead of the module symbol table for the global symbol,
1127 // which throws things all off. To get around this, we just tell
1128 // getValNonImprovising that we are at global scope here.
1129 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001130 Function *SavedCurFn = CurFun.CurrentFunction;
1131 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001132
Chris Lattner2079fde2001-10-13 06:41:08 +00001133 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001134
Chris Lattner394f2eb2003-10-16 20:12:13 +00001135 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001136
Chris Lattner2079fde2001-10-13 06:41:08 +00001137 // If this is an initializer for a constant pointer, which is referencing a
1138 // (currently) undefined variable, create a stub now that shall be replaced
1139 // in the future with the right type of variable.
1140 //
1141 if (V == 0) {
1142 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1143 const PointerType *PT = cast<PointerType>(Ty);
1144
1145 // First check to see if the forward references value is already created!
1146 PerModuleInfo::GlobalRefsType::iterator I =
Chris Lattner9232b992003-04-22 18:42:41 +00001147 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001148
1149 if (I != CurModule.GlobalRefs.end()) {
1150 V = I->second; // Placeholder already exists, use it...
Chris Lattnera989b232003-12-23 20:05:15 +00001151 $2.destroy();
Chris Lattner2079fde2001-10-13 06:41:08 +00001152 } else {
Chris Lattner2079fde2001-10-13 06:41:08 +00001153 // Create a placeholder for the global variable reference...
Chris Lattner7a176752001-12-04 00:03:30 +00001154 GlobalVariable *GV = new GlobalVariable(PT->getElementType(),
Chris Lattner4ad02e72003-04-16 20:28:45 +00001155 false,
1156 GlobalValue::ExternalLinkage);
Chris Lattner2079fde2001-10-13 06:41:08 +00001157 // Keep track of the fact that we have a forward ref to recycle it
Chris Lattner9232b992003-04-22 18:42:41 +00001158 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
Chris Lattner2079fde2001-10-13 06:41:08 +00001159
1160 // Must temporarily push this value into the module table...
1161 CurModule.CurrentModule->getGlobalList().push_back(GV);
1162 V = GV;
1163 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001164 }
1165
Chris Lattner2079fde2001-10-13 06:41:08 +00001166 GlobalValue *GV = cast<GlobalValue>(V);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001167 $$ = ConstantPointerRef::get(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001168 delete $1; // Free the type handle
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001169 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001170 | Types ConstExpr {
1171 if ($1->get() != $2->getType())
1172 ThrowException("Mismatched types for constant expression!");
1173 $$ = $2;
1174 delete $1;
Chris Lattnerf4600482003-06-28 20:01:34 +00001175 }
1176 | Types ZEROINITIALIZER {
1177 $$ = Constant::getNullValue($1->get());
1178 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001179 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001180
Chris Lattnere43f40b2002-10-09 00:25:32 +00001181ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001182 if (!ConstantSInt::isValueValidForType($1, $2))
1183 ThrowException("Constant value doesn't fit in type!");
1184 $$ = ConstantSInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001185 }
1186 | UIntType EUINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001187 if (!ConstantUInt::isValueValidForType($1, $2))
1188 ThrowException("Constant value doesn't fit in type!");
1189 $$ = ConstantUInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001190 }
1191 | BOOL TRUE { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001192 $$ = ConstantBool::True;
1193 }
Chris Lattnere43f40b2002-10-09 00:25:32 +00001194 | BOOL FALSE { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001195 $$ = ConstantBool::False;
1196 }
1197 | FPType FPVAL { // Float & Double constants
1198 $$ = ConstantFP::get($1, $2);
1199 };
1200
Chris Lattner00950542001-06-06 20:29:01 +00001201
Chris Lattnerd78700d2002-08-16 21:14:40 +00001202ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattnerae711a82003-11-21 20:27:35 +00001203 if (!$3->getType()->isFirstClassType())
1204 ThrowException("cast constant expression from a non-primitive type: '" +
1205 $3->getType()->getDescription() + "'!");
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001206 if (!$5->get()->isFirstClassType())
1207 ThrowException("cast constant expression to a non-primitive type: '" +
1208 $5->get()->getDescription() + "'!");
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001209 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001210 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001211 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001212 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1213 if (!isa<PointerType>($3->getType()))
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001214 ThrowException("GetElementPtr requires a pointer operand!");
1215
1216 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001217 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001218 if (!IdxTy)
1219 ThrowException("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001220
Chris Lattner9232b992003-04-22 18:42:41 +00001221 std::vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001222 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1223 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001224 IdxVec.push_back(C);
1225 else
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001226 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001227
Chris Lattnerd78700d2002-08-16 21:14:40 +00001228 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001229
Chris Lattnerd78700d2002-08-16 21:14:40 +00001230 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001231 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001232 | BinaryOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001233 if ($3->getType() != $5->getType())
1234 ThrowException("Binary operator types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001235 $$ = ConstantExpr::get($1, $3, $5);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001236 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001237 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001238 if ($5->getType() != Type::UByteTy)
1239 ThrowException("Shift count for shift constant must be unsigned byte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001240 if (!$3->getType()->isInteger())
1241 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerb16689b2004-01-12 19:08:43 +00001242 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner699f1eb2002-08-14 17:12:33 +00001243 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001244
1245
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001246// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001247ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001248 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001249 }
1250 | ConstVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001251 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001252 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001253 };
Chris Lattner00950542001-06-06 20:29:01 +00001254
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001255
Chris Lattner1781aca2001-09-18 04:00:54 +00001256// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001257GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001258
Chris Lattner00950542001-06-06 20:29:01 +00001259
Chris Lattner0e73ce62002-05-02 19:11:13 +00001260//===----------------------------------------------------------------------===//
1261// Rules to match Modules
1262//===----------------------------------------------------------------------===//
1263
1264// Module rule: Capture the result of parsing the whole file into a result
1265// variable...
1266//
1267Module : FunctionList {
1268 $$ = ParserResult = $1;
1269 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001270};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001271
Chris Lattner7e708292002-06-25 16:13:24 +00001272// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001273//
1274FunctionList : FunctionList Function {
1275 $$ = $1;
1276 assert($2->getParent() == 0 && "Function already in module!");
1277 $1->getFunctionList().push_back($2);
Chris Lattner394f2eb2003-10-16 20:12:13 +00001278 CurFun.FunctionDone();
Chris Lattner0e73ce62002-05-02 19:11:13 +00001279 }
1280 | FunctionList FunctionProto {
1281 $$ = $1;
1282 }
1283 | FunctionList IMPLEMENTATION {
1284 $$ = $1;
1285 }
1286 | ConstPool {
1287 $$ = CurModule.CurrentModule;
1288 // Resolve circular types before we parse the body of the module
1289 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001290 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001291
Chris Lattnere98dda62001-07-14 06:10:16 +00001292// ConstPool - Constants with optional names assigned to them.
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001293ConstPool : ConstPool OptAssign CONST ConstVal {
Chris Lattneradf99702003-03-03 23:28:55 +00001294 if (!setValueName($4, $2))
1295 InsertValue($4);
Chris Lattner00950542001-06-06 20:29:01 +00001296 }
Chris Lattner30c89792001-09-07 16:35:17 +00001297 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner4a42e902001-10-22 05:56:09 +00001298 // Eagerly resolve types. This is not an optimization, this is a
1299 // requirement that is due to the fact that we could have this:
1300 //
1301 // %list = type { %list * }
1302 // %list = type { %list * } ; repeated type decl
1303 //
1304 // If types are not resolved eagerly, then the two types will not be
1305 // determined to be the same type!
1306 //
1307 ResolveTypeTo($2, $4->get());
1308
Chris Lattner1781aca2001-09-18 04:00:54 +00001309 // TODO: FIXME when Type are not const
Chris Lattnerb7474512001-10-03 15:39:04 +00001310 if (!setValueName(const_cast<Type*>($4->get()), $2)) {
1311 // If this is not a redefinition of a type...
1312 if (!$2) {
1313 InsertType($4->get(),
Chris Lattner394f2eb2003-10-16 20:12:13 +00001314 inFunctionScope() ? CurFun.Types : CurModule.Types);
Chris Lattnerb7474512001-10-03 15:39:04 +00001315 }
Chris Lattner30c89792001-09-07 16:35:17 +00001316 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001317
1318 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001319 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001320 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001321 }
Chris Lattner4ad02e72003-04-16 20:28:45 +00001322 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Chris Lattnerdda71962001-11-26 18:54:16 +00001323 const Type *Ty = $5->getType();
Chris Lattner1781aca2001-09-18 04:00:54 +00001324 // Global declarations appear in Constant Pool
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001325 Constant *Initializer = $5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001326 if (Initializer == 0)
1327 ThrowException("Global value initializer is not a constant!");
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001328
Chris Lattnerdda71962001-11-26 18:54:16 +00001329 GlobalVariable *GV = new GlobalVariable(Ty, $4, $3, Initializer);
Chris Lattnerb7474512001-10-03 15:39:04 +00001330 if (!setValueName(GV, $2)) { // If not redefining...
1331 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001332 int Slot = InsertValue(GV, CurModule.Values);
1333
1334 if (Slot != -1) {
1335 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1336 } else {
1337 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1338 (char*)GV->getName().c_str()));
1339 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001340 }
Chris Lattner1781aca2001-09-18 04:00:54 +00001341 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001342 | ConstPool OptAssign EXTERNAL GlobalType Types {
1343 const Type *Ty = *$5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001344 // Global declarations appear in Constant Pool
Chris Lattner4ad02e72003-04-16 20:28:45 +00001345 GlobalVariable *GV = new GlobalVariable(Ty,$4,GlobalValue::ExternalLinkage);
Chris Lattnerb7474512001-10-03 15:39:04 +00001346 if (!setValueName(GV, $2)) { // If not redefining...
1347 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001348 int Slot = InsertValue(GV, CurModule.Values);
1349
1350 if (Slot != -1) {
1351 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1352 } else {
1353 assert(GV->hasName() && "Not named and not numbered!?");
1354 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1355 (char*)GV->getName().c_str()));
1356 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001357 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001358 delete $5;
Chris Lattnere98dda62001-07-14 06:10:16 +00001359 }
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001360 | ConstPool TARGET TargetDefinition {
1361 }
Chris Lattner00950542001-06-06 20:29:01 +00001362 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001363 };
Chris Lattner00950542001-06-06 20:29:01 +00001364
1365
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001366
1367BigOrLittle : BIG { $$ = Module::BigEndian; };
1368BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1369
1370TargetDefinition : ENDIAN '=' BigOrLittle {
1371 CurModule.CurrentModule->setEndianness($3);
1372 }
1373 | POINTERSIZE '=' EUINT64VAL {
1374 if ($3 == 32)
1375 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1376 else if ($3 == 64)
1377 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1378 else
1379 ThrowException("Invalid pointer size: '" + utostr($3) + "'!");
1380 };
1381
1382
Chris Lattner00950542001-06-06 20:29:01 +00001383//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001384// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001385//===----------------------------------------------------------------------===//
1386
Chris Lattner6c23f572003-08-22 05:42:10 +00001387Name : VAR_ID | STRINGCONSTANT;
1388OptName : Name | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001389
Chris Lattner6c23f572003-08-22 05:42:10 +00001390ArgVal : Types OptName {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001391 if (*$1 == Type::VoidTy)
1392 ThrowException("void typed arguments are invalid!");
Chris Lattner9232b992003-04-22 18:42:41 +00001393 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
Chris Lattner51727be2002-06-04 21:58:56 +00001394};
Chris Lattner00950542001-06-06 20:29:01 +00001395
Chris Lattner69da5cf2002-10-13 20:57:00 +00001396ArgListH : ArgListH ',' ArgVal {
1397 $$ = $1;
1398 $1->push_back(*$3);
1399 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001400 }
1401 | ArgVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001402 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
Chris Lattner69da5cf2002-10-13 20:57:00 +00001403 $$->push_back(*$1);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001404 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001405 };
Chris Lattner00950542001-06-06 20:29:01 +00001406
1407ArgList : ArgListH {
1408 $$ = $1;
1409 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001410 | ArgListH ',' DOTDOTDOT {
1411 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001412 $$->push_back(std::pair<PATypeHolder*,
1413 char*>(new PATypeHolder(Type::VoidTy), 0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001414 }
1415 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001416 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1417 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001418 }
Chris Lattner00950542001-06-06 20:29:01 +00001419 | /* empty */ {
1420 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001421 };
Chris Lattner00950542001-06-06 20:29:01 +00001422
Chris Lattner6c23f572003-08-22 05:42:10 +00001423FunctionHeaderH : TypesV Name '(' ArgList ')' {
Chris Lattner1f862af2003-04-16 18:13:57 +00001424 UnEscapeLexed($2);
Chris Lattner9232b992003-04-22 18:42:41 +00001425 std::string FunctionName($2);
Chris Lattnerdda71962001-11-26 18:54:16 +00001426
Chris Lattnerc282f5a2003-11-21 22:32:23 +00001427 if (!(*$1)->isFirstClassType() && *$1 != Type::VoidTy)
1428 ThrowException("LLVM functions cannot return aggregate types!");
1429
Chris Lattner9232b992003-04-22 18:42:41 +00001430 std::vector<const Type*> ParamTypeList;
Chris Lattner1f862af2003-04-16 18:13:57 +00001431 if ($4) { // If there are arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001432 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001433 I != $4->end(); ++I)
Chris Lattner69da5cf2002-10-13 20:57:00 +00001434 ParamTypeList.push_back(I->first->get());
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001435 }
Chris Lattner00950542001-06-06 20:29:01 +00001436
Chris Lattner2079fde2001-10-13 06:41:08 +00001437 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1438 if (isVarArg) ParamTypeList.pop_back();
1439
Chris Lattner1f862af2003-04-16 18:13:57 +00001440 const FunctionType *FT = FunctionType::get(*$1, ParamTypeList, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001441 const PointerType *PFT = PointerType::get(FT);
Chris Lattner1f862af2003-04-16 18:13:57 +00001442 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001443
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001444 Function *Fn = 0;
1445 // Is the function already in symtab?
1446 if ((Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1447 // Yes it is. If this is the case, either we need to be a forward decl,
1448 // or it needs to be.
Chris Lattner394f2eb2003-10-16 20:12:13 +00001449 if (!CurFun.isDeclare && !Fn->isExternal())
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001450 ThrowException("Redefinition of function '" + FunctionName + "'!");
1451
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001452 // If we found a preexisting function prototype, remove it from the
1453 // module, so that we don't get spurious conflicts with global & local
1454 // variables.
1455 //
1456 CurModule.CurrentModule->getFunctionList().remove(Fn);
Chris Lattner34538142002-03-08 19:11:42 +00001457
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001458 // Make sure to strip off any argument names so we can't get conflicts...
1459 for (Function::aiterator AI = Fn->abegin(), AE = Fn->aend(); AI != AE; ++AI)
1460 AI->setName("");
Chris Lattner5659dd12002-07-15 00:10:33 +00001461
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001462 } else { // Not already defined?
Chris Lattner4ad02e72003-04-16 20:28:45 +00001463 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001464 InsertValue(Fn, CurModule.Values);
Chris Lattner1f862af2003-04-16 18:13:57 +00001465 CurModule.DeclareNewGlobalValue(Fn, ValID::create($2));
Chris Lattnere1815642001-07-15 06:35:53 +00001466 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001467 free($2); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001468
Chris Lattner394f2eb2003-10-16 20:12:13 +00001469 CurFun.FunctionStart(Fn);
Chris Lattner00950542001-06-06 20:29:01 +00001470
Chris Lattner7e708292002-06-25 16:13:24 +00001471 // Add all of the arguments we parsed to the function...
Chris Lattner1f862af2003-04-16 18:13:57 +00001472 if ($4) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001473 if (isVarArg) { // Nuke the last entry
Chris Lattner1f862af2003-04-16 18:13:57 +00001474 assert($4->back().first->get() == Type::VoidTy && $4->back().second == 0&&
Chris Lattner69da5cf2002-10-13 20:57:00 +00001475 "Not a varargs marker!");
Chris Lattner1f862af2003-04-16 18:13:57 +00001476 delete $4->back().first;
1477 $4->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00001478 }
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001479 Function::aiterator ArgIt = Fn->abegin();
Chris Lattner9232b992003-04-22 18:42:41 +00001480 for (std::vector<std::pair<PATypeHolder*, char*> >::iterator I =$4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001481 I != $4->end(); ++I, ++ArgIt) {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001482 delete I->first; // Delete the typeholder...
1483
1484 if (setValueName(ArgIt, I->second)) // Insert arg into symtab...
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001485 assert(0 && "No arg redef allowed!");
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001486
Chris Lattner69da5cf2002-10-13 20:57:00 +00001487 InsertValue(ArgIt);
Chris Lattner00950542001-06-06 20:29:01 +00001488 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001489
Chris Lattner1f862af2003-04-16 18:13:57 +00001490 delete $4; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00001491 }
Chris Lattner51727be2002-06-04 21:58:56 +00001492};
Chris Lattner00950542001-06-06 20:29:01 +00001493
Chris Lattner9b02cc32002-05-03 18:23:48 +00001494BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1495
Chris Lattner4ad02e72003-04-16 20:28:45 +00001496FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00001497 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001498
Chris Lattner4ad02e72003-04-16 20:28:45 +00001499 // Make sure that we keep track of the linkage type even if there was a
1500 // previous "declare".
1501 $$->setLinkage($1);
Chris Lattner1f862af2003-04-16 18:13:57 +00001502
Chris Lattner7e708292002-06-25 16:13:24 +00001503 // Resolve circular types before we parse the body of the function.
Chris Lattner394f2eb2003-10-16 20:12:13 +00001504 ResolveTypes(CurFun.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001505};
Chris Lattner00950542001-06-06 20:29:01 +00001506
Chris Lattner9b02cc32002-05-03 18:23:48 +00001507END : ENDTOK | '}'; // Allow end of '}' to end a function
1508
Chris Lattner79df7c02002-03-26 18:01:55 +00001509Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001510 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001511};
Chris Lattner00950542001-06-06 20:29:01 +00001512
Chris Lattner394f2eb2003-10-16 20:12:13 +00001513FunctionProto : DECLARE { CurFun.isDeclare = true; } FunctionHeaderH {
1514 $$ = CurFun.CurrentFunction;
Chris Lattner79df7c02002-03-26 18:01:55 +00001515 assert($$->getParent() == 0 && "Function already in module!");
1516 CurModule.CurrentModule->getFunctionList().push_back($$);
Chris Lattner394f2eb2003-10-16 20:12:13 +00001517 CurFun.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001518};
Chris Lattner00950542001-06-06 20:29:01 +00001519
1520//===----------------------------------------------------------------------===//
1521// Rules to match Basic Blocks
1522//===----------------------------------------------------------------------===//
1523
1524ConstValueRef : ESINT64VAL { // A reference to a direct constant
1525 $$ = ValID::create($1);
1526 }
1527 | EUINT64VAL {
1528 $$ = ValID::create($1);
1529 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001530 | FPVAL { // Perhaps it's an FP constant?
1531 $$ = ValID::create($1);
1532 }
Chris Lattner00950542001-06-06 20:29:01 +00001533 | TRUE {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001534 $$ = ValID::create(ConstantBool::True);
Chris Lattner00950542001-06-06 20:29:01 +00001535 }
1536 | FALSE {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001537 $$ = ValID::create(ConstantBool::False);
Chris Lattner00950542001-06-06 20:29:01 +00001538 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001539 | NULL_TOK {
1540 $$ = ValID::createNull();
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001541 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001542 | ConstExpr {
1543 $$ = ValID::create($1);
1544 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00001545
Chris Lattner2079fde2001-10-13 06:41:08 +00001546// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1547// another value.
1548//
1549SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001550 $$ = ValID::create($1);
1551 }
Chris Lattner6c23f572003-08-22 05:42:10 +00001552 | Name { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001553 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001554 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001555
1556// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001557ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001558
Chris Lattner00950542001-06-06 20:29:01 +00001559
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001560// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1561// type immediately preceeds the value reference, and allows complex constant
1562// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001563ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001564 $$ = getVal(*$1, $2); delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001565 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001566
Chris Lattner00950542001-06-06 20:29:01 +00001567BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner7e708292002-06-25 16:13:24 +00001568 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner00950542001-06-06 20:29:01 +00001569 }
Chris Lattner7e708292002-06-25 16:13:24 +00001570 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
1571 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner51727be2002-06-04 21:58:56 +00001572 };
Chris Lattner00950542001-06-06 20:29:01 +00001573
1574
1575// Basic blocks are terminated by branching instructions:
1576// br, br/cc, switch, ret
1577//
Chris Lattner2079fde2001-10-13 06:41:08 +00001578BasicBlock : InstructionList OptAssign BBTerminatorInst {
1579 if (setValueName($3, $2)) { assert(0 && "No redefn allowed!"); }
1580 InsertValue($3);
1581
1582 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001583 InsertValue($1);
1584 $$ = $1;
1585 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001586 | LABELSTR InstructionList OptAssign BBTerminatorInst {
1587 if (setValueName($4, $3)) { assert(0 && "No redefn allowed!"); }
1588 InsertValue($4);
1589
1590 $2->getInstList().push_back($4);
Chris Lattnerb7474512001-10-03 15:39:04 +00001591 if (setValueName($2, $1)) { assert(0 && "No label redef allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001592
1593 InsertValue($2);
1594 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001595 };
Chris Lattner00950542001-06-06 20:29:01 +00001596
1597InstructionList : InstructionList Inst {
1598 $1->getInstList().push_back($2);
1599 $$ = $1;
1600 }
1601 | /* empty */ {
Chris Lattner99e7ab72003-10-18 05:53:13 +00001602 $$ = CurBB = new BasicBlock();
Chris Lattner51727be2002-06-04 21:58:56 +00001603 };
Chris Lattner00950542001-06-06 20:29:01 +00001604
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001605BBTerminatorInst : RET ResolvedVal { // Return with a result...
1606 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001607 }
1608 | RET VOID { // Return with no result...
1609 $$ = new ReturnInst();
1610 }
1611 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner9636a912001-10-01 16:18:37 +00001612 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
Chris Lattner00950542001-06-06 20:29:01 +00001613 } // Conditional Branch...
1614 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner9636a912001-10-01 16:18:37 +00001615 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1616 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
Chris Lattner00950542001-06-06 20:29:01 +00001617 getVal(Type::BoolTy, $3));
1618 }
1619 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1620 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001621 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
Chris Lattner00950542001-06-06 20:29:01 +00001622 $$ = S;
1623
Chris Lattner9232b992003-04-22 18:42:41 +00001624 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00001625 E = $8->end();
1626 for (; I != E; ++I)
Chris Lattner4354f562003-08-23 23:14:52 +00001627 S->addCase(I->first, I->second);
Chris Lattner00950542001-06-06 20:29:01 +00001628 }
Chris Lattner196850c2003-05-15 21:30:00 +00001629 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
1630 SwitchInst *S = new SwitchInst(getVal($2, $3),
1631 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
1632 $$ = S;
1633 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001634 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO ResolvedVal
Chris Lattnera58e3a12004-02-08 21:48:25 +00001635 UNWIND ResolvedVal {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001636 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001637 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001638
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001639 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1640 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001641 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001642 std::vector<const Type*> ParamTypes;
Chris Lattner2079fde2001-10-13 06:41:08 +00001643 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001644 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1645 I != E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001646 ParamTypes.push_back((*I)->getType());
1647 }
1648
1649 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1650 if (isVarArg) ParamTypes.pop_back();
1651
Chris Lattner79df7c02002-03-26 18:01:55 +00001652 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001653 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00001654 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001655
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001656 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00001657
1658 BasicBlock *Normal = dyn_cast<BasicBlock>($8);
1659 BasicBlock *Except = dyn_cast<BasicBlock>($10);
1660
1661 if (Normal == 0 || Except == 0)
1662 ThrowException("Invoke instruction without label destinations!");
1663
1664 // Create the call node...
1665 if (!$5) { // Has no arguments?
Chris Lattner9232b992003-04-22 18:42:41 +00001666 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001667 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001668 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00001669 // correctly!
1670 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001671 FunctionType::param_iterator I = Ty->param_begin();
1672 FunctionType::param_iterator E = Ty->param_end();
Chris Lattner9232b992003-04-22 18:42:41 +00001673 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001674
1675 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1676 if ((*ArgI)->getType() != *I)
1677 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001678 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001679
1680 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1681 ThrowException("Invalid number of parameters detected!");
1682
Chris Lattner6cdb0112001-11-26 16:54:11 +00001683 $$ = new InvokeInst(V, Normal, Except, *$5);
Chris Lattner2079fde2001-10-13 06:41:08 +00001684 }
Chris Lattner9cd42572003-12-23 22:18:36 +00001685 delete $2;
Chris Lattner2079fde2001-10-13 06:41:08 +00001686 delete $5;
Chris Lattner36143fc2003-09-08 18:54:55 +00001687 }
1688 | UNWIND {
1689 $$ = new UnwindInst();
Chris Lattner51727be2002-06-04 21:58:56 +00001690 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001691
1692
Chris Lattner00950542001-06-06 20:29:01 +00001693
1694JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1695 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001696 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001697 if (V == 0)
1698 ThrowException("May only switch on a constant pool value!");
1699
Chris Lattner9232b992003-04-22 18:42:41 +00001700 $$->push_back(std::make_pair(V, cast<BasicBlock>(getVal($5, $6))));
Chris Lattner00950542001-06-06 20:29:01 +00001701 }
1702 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00001703 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001704 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001705
1706 if (V == 0)
1707 ThrowException("May only switch on a constant pool value!");
1708
Chris Lattner9232b992003-04-22 18:42:41 +00001709 $$->push_back(std::make_pair(V, cast<BasicBlock>(getVal($4, $5))));
Chris Lattner51727be2002-06-04 21:58:56 +00001710 };
Chris Lattner00950542001-06-06 20:29:01 +00001711
1712Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001713 // Is this definition named?? if so, assign the name...
1714 if (setValueName($2, $1)) { assert(0 && "No redefin allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001715 InsertValue($2);
1716 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001717};
Chris Lattner00950542001-06-06 20:29:01 +00001718
Chris Lattnerc24d2082001-06-11 15:04:20 +00001719PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Chris Lattner9232b992003-04-22 18:42:41 +00001720 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
1721 $$->push_back(std::make_pair(getVal(*$1, $3),
1722 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
Chris Lattner30c89792001-09-07 16:35:17 +00001723 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001724 }
1725 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1726 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001727 $1->push_back(std::make_pair(getVal($1->front().first->getType(), $4),
1728 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
Chris Lattner51727be2002-06-04 21:58:56 +00001729 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00001730
1731
Chris Lattner30c89792001-09-07 16:35:17 +00001732ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner9232b992003-04-22 18:42:41 +00001733 $$ = new std::vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001734 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001735 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001736 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001737 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001738 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00001739 };
Chris Lattner00950542001-06-06 20:29:01 +00001740
1741// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00001742ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001743
Chris Lattner4a6482b2002-09-10 19:57:26 +00001744InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1745 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint())
1746 ThrowException("Arithmetic operator requires integer or FP operands!");
1747 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1748 if ($$ == 0)
1749 ThrowException("binary operator returned null!");
1750 delete $2;
1751 }
1752 | LogicalOps Types ValueRef ',' ValueRef {
1753 if (!(*$2)->isIntegral())
1754 ThrowException("Logical operator requires integral operands!");
1755 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1756 if ($$ == 0)
1757 ThrowException("binary operator returned null!");
1758 delete $2;
1759 }
1760 | SetCondOps Types ValueRef ',' ValueRef {
Chris Lattner1cff96a2002-09-10 22:37:46 +00001761 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001762 if ($$ == 0)
1763 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001764 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001765 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00001766 | NOT ResolvedVal {
1767 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
1768 << " Replacing with 'xor'.\n";
1769
1770 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
1771 if (Ones == 0)
1772 ThrowException("Expected integral type for not instruction!");
1773
1774 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00001775 if ($$ == 0)
Chris Lattner699f1eb2002-08-14 17:12:33 +00001776 ThrowException("Could not create a xor instruction!");
Chris Lattner09083092001-07-08 04:57:15 +00001777 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001778 | ShiftOps ResolvedVal ',' ResolvedVal {
1779 if ($4->getType() != Type::UByteTy)
1780 ThrowException("Shift amount must be ubyte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001781 if (!$2->getType()->isInteger())
1782 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001783 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001784 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001785 | CAST ResolvedVal TO Types {
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001786 if (!$4->get()->isFirstClassType())
1787 ThrowException("cast instruction to a non-primitive type: '" +
1788 $4->get()->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001789 $$ = new CastInst($2, *$4);
1790 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001791 }
Chris Lattner8f77dae2003-05-08 02:44:12 +00001792 | VA_ARG ResolvedVal ',' Types {
Chris Lattner99e7ab72003-10-18 05:53:13 +00001793 // FIXME: This is emulation code for an obsolete syntax. This should be
1794 // removed at some point.
1795 if (!ObsoleteVarArgs) {
1796 std::cerr << "WARNING: this file uses obsolete features. "
1797 << "Assemble and disassemble to update it.\n";
1798 ObsoleteVarArgs = true;
1799 }
1800
1801 // First, load the valist...
1802 Instruction *CurVAList = new LoadInst($2, "");
1803 CurBB->getInstList().push_back(CurVAList);
1804
1805 // Emit the vaarg instruction.
1806 $$ = new VAArgInst(CurVAList, *$4);
1807
1808 // Now we must advance the pointer and update it in memory.
1809 Instruction *TheVANext = new VANextInst(CurVAList, *$4);
1810 CurBB->getInstList().push_back(TheVANext);
1811
1812 CurBB->getInstList().push_back(new StoreInst(TheVANext, $2));
1813 delete $4;
1814 }
1815 | VAARG ResolvedVal ',' Types {
1816 $$ = new VAArgInst($2, *$4);
1817 delete $4;
1818 }
1819 | VANEXT ResolvedVal ',' Types {
1820 $$ = new VANextInst($2, *$4);
Chris Lattner8f77dae2003-05-08 02:44:12 +00001821 delete $4;
1822 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00001823 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001824 const Type *Ty = $2->front().first->getType();
Chris Lattner8ea8f362003-10-30 01:38:18 +00001825 if (!Ty->isFirstClassType())
1826 ThrowException("PHI node operands must be of first class type!");
Chris Lattnerc24d2082001-06-11 15:04:20 +00001827 $$ = new PHINode(Ty);
Chris Lattnerbd2531f2003-10-10 16:34:58 +00001828 $$->op_reserve($2->size()*2);
Chris Lattner00950542001-06-06 20:29:01 +00001829 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001830 if ($2->front().first->getType() != Ty)
1831 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001832 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001833 $2->pop_front();
1834 }
1835 delete $2; // Free the list...
1836 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001837 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001838 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001839 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001840
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001841 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1842 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001843 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001844 std::vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001845 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001846 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1847 I != E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00001848 ParamTypes.push_back((*I)->getType());
1849 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001850
1851 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1852 if (isVarArg) ParamTypes.pop_back();
1853
Chris Lattner79df7c02002-03-26 18:01:55 +00001854 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001855 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001856 }
Chris Lattner00950542001-06-06 20:29:01 +00001857
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001858 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001859
Chris Lattner8b81bf52001-07-25 22:47:46 +00001860 // Create the call node...
1861 if (!$5) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00001862 // Make sure no arguments is a good thing!
1863 if (Ty->getNumParams() != 0)
1864 ThrowException("No arguments passed to a function that "
1865 "expects arguments!");
1866
Chris Lattner9232b992003-04-22 18:42:41 +00001867 $$ = new CallInst(V, std::vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001868 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001869 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00001870 // correctly!
1871 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001872 FunctionType::param_iterator I = Ty->param_begin();
1873 FunctionType::param_iterator E = Ty->param_end();
Chris Lattner9232b992003-04-22 18:42:41 +00001874 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001875
1876 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1877 if ((*ArgI)->getType() != *I)
1878 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001879 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001880
Chris Lattner8b81bf52001-07-25 22:47:46 +00001881 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001882 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001883
Chris Lattner6cdb0112001-11-26 16:54:11 +00001884 $$ = new CallInst(V, *$5);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001885 }
Chris Lattnerf64d57a2003-12-23 20:39:17 +00001886 delete $2;
Chris Lattner8b81bf52001-07-25 22:47:46 +00001887 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001888 }
1889 | MemoryInst {
1890 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001891 };
Chris Lattner00950542001-06-06 20:29:01 +00001892
Chris Lattner6cdb0112001-11-26 16:54:11 +00001893
1894// IndexList - List of indices for GEP based instructions...
1895IndexList : ',' ValueRefList {
Chris Lattner15c9c032003-09-08 18:20:29 +00001896 $$ = $2;
1897 } | /* empty */ {
1898 $$ = new std::vector<Value*>();
1899 };
1900
1901OptVolatile : VOLATILE {
1902 $$ = true;
1903 }
1904 | /* empty */ {
1905 $$ = false;
1906 };
1907
Chris Lattner027dcc52001-07-08 21:10:27 +00001908
Chris Lattner00950542001-06-06 20:29:01 +00001909MemoryInst : MALLOC Types {
Chris Lattner05804b72002-09-13 22:28:45 +00001910 $$ = new MallocInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00001911 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001912 }
1913 | MALLOC Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00001914 $$ = new MallocInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001915 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001916 }
1917 | ALLOCA Types {
Chris Lattner05804b72002-09-13 22:28:45 +00001918 $$ = new AllocaInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00001919 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001920 }
1921 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00001922 $$ = new AllocaInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001923 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001924 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001925 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00001926 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001927 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00001928 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001929 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001930 }
1931
Chris Lattner15c9c032003-09-08 18:20:29 +00001932 | OptVolatile LOAD Types ValueRef {
1933 if (!isa<PointerType>($3->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00001934 ThrowException("Can't load from nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00001935 (*$3)->getDescription());
Chris Lattner79ad13802003-09-08 20:29:46 +00001936 $$ = new LoadInst(getVal(*$3, $4), "", $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00001937 delete $3;
Chris Lattner027dcc52001-07-08 21:10:27 +00001938 }
Chris Lattner15c9c032003-09-08 18:20:29 +00001939 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
1940 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00001941 if (!PT)
Chris Lattner72e00252001-12-14 16:28:42 +00001942 ThrowException("Can't store to a nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00001943 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00001944 const Type *ElTy = PT->getElementType();
Chris Lattner15c9c032003-09-08 18:20:29 +00001945 if (ElTy != $3->getType())
1946 ThrowException("Can't store '" + $3->getType()->getDescription() +
Chris Lattner72e00252001-12-14 16:28:42 +00001947 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner0383cc42002-08-21 23:51:21 +00001948
Chris Lattner79ad13802003-09-08 20:29:46 +00001949 $$ = new StoreInst($3, getVal(*$5, $6), $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00001950 delete $5;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001951 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00001952 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00001953 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001954 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner30c89792001-09-07 16:35:17 +00001955 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner72e00252001-12-14 16:28:42 +00001956 ThrowException("Can't get element ptr '" + (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001957 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
1958 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00001959 };
Chris Lattner027dcc52001-07-08 21:10:27 +00001960
Brian Gaeked0fde302003-11-11 22:41:34 +00001961
Chris Lattner00950542001-06-06 20:29:01 +00001962%%
Chris Lattner09083092001-07-08 04:57:15 +00001963int yyerror(const char *ErrorMsg) {
Chris Lattner9232b992003-04-22 18:42:41 +00001964 std::string where
1965 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001966 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Chris Lattner9232b992003-04-22 18:42:41 +00001967 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001968 if (yychar == YYEMPTY)
1969 errMsg += "end-of-file.";
1970 else
Chris Lattner9232b992003-04-22 18:42:41 +00001971 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001972 ThrowException(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00001973 return 0;
1974}