blob: a1ad18ad88e2afe18555bc6bccf6cdd5750bc45a [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 Lattner026a8ce2004-02-09 18:53:54 +0000629
630 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
631 // to zero), we resolve them all together before we resolve them to Ty. At
632 // the end of the loop, if there is anything to resolve to Ty, it will be in
633 // this variable.
634 OpaqueType *TypeToResolve = 0;
635
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000636 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Chris Lattner5084d032001-11-02 07:46:26 +0000637 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Chris Lattner30c89792001-09-07 16:35:17 +0000638 << UpRefs[i].second->getDescription() << ") = "
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000639 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner3b073862004-02-09 03:19:29 +0000640 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
641 // Decrement level of upreference
642 unsigned Level = --UpRefs[i].NestingLevel;
643 UpRefs[i].LastContainedTy = Ty;
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000644 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Chris Lattner30c89792001-09-07 16:35:17 +0000645 if (Level == 0) { // Upreference should be resolved!
Chris Lattner026a8ce2004-02-09 18:53:54 +0000646 if (!TypeToResolve) {
647 TypeToResolve = UpRefs[i].UpRefTy;
648 } else {
649 UR_OUT(" * Resolving upreference for "
650 << UpRefs[i].second->getDescription() << "\n";
651 std::string OldName = UpRefs[i].UpRefTy->getDescription());
652 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
653 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
654 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
655 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000656 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
657 --i; // Do not skip the next element...
Chris Lattner30c89792001-09-07 16:35:17 +0000658 }
659 }
Chris Lattner8896eda2001-07-09 19:38:36 +0000660 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000661
Chris Lattner026a8ce2004-02-09 18:53:54 +0000662 if (TypeToResolve) {
663 UR_OUT(" * Resolving upreference for "
664 << UpRefs[i].second->getDescription() << "\n";
665 std::string OldName = UpRefs[i].UpRefTy->getDescription());
666 TypeToResolve->refineAbstractTypeTo(Ty);
667 }
668
Chris Lattner8896eda2001-07-09 19:38:36 +0000669 return Ty;
670}
671
Chris Lattner30c89792001-09-07 16:35:17 +0000672
Chris Lattner00950542001-06-06 20:29:01 +0000673//===----------------------------------------------------------------------===//
674// RunVMAsmParser - Define an interface to this parser
675//===----------------------------------------------------------------------===//
676//
Chris Lattner9232b992003-04-22 18:42:41 +0000677Module *RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000678 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000679 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000680 llvmAsmlineno = 1; // Reset the current line number...
Chris Lattner99e7ab72003-10-18 05:53:13 +0000681 ObsoleteVarArgs = false;
Chris Lattner00950542001-06-06 20:29:01 +0000682
Chris Lattner75f20532003-04-22 18:02:52 +0000683 // Allocate a new module to read
684 CurModule.CurrentModule = new Module(Filename);
Chris Lattner42570982003-11-26 07:24:58 +0000685
686 try {
687 yyparse(); // Parse the file.
688 } catch (...) {
689 // Clear the symbol table so it doesn't complain when it
690 // gets destructed
691 CurFun.LocalSymtab.clear();
692 throw;
693 }
Chris Lattner99e7ab72003-10-18 05:53:13 +0000694
Chris Lattner00950542001-06-06 20:29:01 +0000695 Module *Result = ParserResult;
Chris Lattner99e7ab72003-10-18 05:53:13 +0000696
697 // Check to see if they called va_start but not va_arg..
698 if (!ObsoleteVarArgs)
699 if (Function *F = Result->getNamedFunction("llvm.va_start"))
700 if (F->asize() == 1) {
701 std::cerr << "WARNING: this file uses obsolete features. "
702 << "Assemble and disassemble to update it.\n";
703 ObsoleteVarArgs = true;
704 }
705
706
707 if (ObsoleteVarArgs) {
708 // If the user is making use of obsolete varargs intrinsics, adjust them for
709 // the user.
710 if (Function *F = Result->getNamedFunction("llvm.va_start")) {
711 assert(F->asize() == 1 && "Obsolete va_start takes 1 argument!");
712
713 const Type *RetTy = F->getFunctionType()->getParamType(0);
714 RetTy = cast<PointerType>(RetTy)->getElementType();
715 Function *NF = Result->getOrInsertFunction("llvm.va_start", RetTy, 0);
716
717 while (!F->use_empty()) {
718 CallInst *CI = cast<CallInst>(F->use_back());
719 Value *V = new CallInst(NF, "", CI);
720 new StoreInst(V, CI->getOperand(1), CI);
721 CI->getParent()->getInstList().erase(CI);
722 }
723 Result->getFunctionList().erase(F);
724 }
725
726 if (Function *F = Result->getNamedFunction("llvm.va_end")) {
727 assert(F->asize() == 1 && "Obsolete va_end takes 1 argument!");
728 const Type *ArgTy = F->getFunctionType()->getParamType(0);
729 ArgTy = cast<PointerType>(ArgTy)->getElementType();
730 Function *NF = Result->getOrInsertFunction("llvm.va_end", Type::VoidTy,
731 ArgTy, 0);
732
733 while (!F->use_empty()) {
734 CallInst *CI = cast<CallInst>(F->use_back());
735 Value *V = new LoadInst(CI->getOperand(1), "", CI);
736 new CallInst(NF, V, "", CI);
737 CI->getParent()->getInstList().erase(CI);
738 }
739 Result->getFunctionList().erase(F);
740 }
741
742 if (Function *F = Result->getNamedFunction("llvm.va_copy")) {
743 assert(F->asize() == 2 && "Obsolete va_copy takes 2 argument!");
744 const Type *ArgTy = F->getFunctionType()->getParamType(0);
745 ArgTy = cast<PointerType>(ArgTy)->getElementType();
746 Function *NF = Result->getOrInsertFunction("llvm.va_copy", ArgTy,
747 ArgTy, 0);
748
749 while (!F->use_empty()) {
750 CallInst *CI = cast<CallInst>(F->use_back());
751 Value *V = new CallInst(NF, CI->getOperand(2), "", CI);
752 new StoreInst(V, CI->getOperand(1), CI);
753 CI->getParent()->getInstList().erase(CI);
754 }
755 Result->getFunctionList().erase(F);
756 }
757 }
758
Chris Lattner00950542001-06-06 20:29:01 +0000759 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
760 ParserResult = 0;
761
762 return Result;
763}
764
Brian Gaeked0fde302003-11-11 22:41:34 +0000765} // End llvm namespace
766
767using namespace llvm;
768
Chris Lattner00950542001-06-06 20:29:01 +0000769%}
770
771%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000772 llvm::Module *ModuleVal;
773 llvm::Function *FunctionVal;
774 std::pair<llvm::PATypeHolder*, char*> *ArgVal;
775 llvm::BasicBlock *BasicBlockVal;
776 llvm::TerminatorInst *TermInstVal;
777 llvm::Instruction *InstVal;
778 llvm::Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000779
Brian Gaeked0fde302003-11-11 22:41:34 +0000780 const llvm::Type *PrimType;
781 llvm::PATypeHolder *TypeVal;
782 llvm::Value *ValueVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000783
Brian Gaeked0fde302003-11-11 22:41:34 +0000784 std::vector<std::pair<llvm::PATypeHolder*,char*> > *ArgList;
785 std::vector<llvm::Value*> *ValueList;
786 std::list<llvm::PATypeHolder> *TypeList;
787 std::list<std::pair<llvm::Value*,
788 llvm::BasicBlock*> > *PHIList; // Represent the RHS of PHI node
789 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
790 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000791
Brian Gaeked0fde302003-11-11 22:41:34 +0000792 llvm::GlobalValue::LinkageTypes Linkage;
Chris Lattner30c89792001-09-07 16:35:17 +0000793 int64_t SInt64Val;
794 uint64_t UInt64Val;
795 int SIntVal;
796 unsigned UIntVal;
797 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000798 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000799
Chris Lattner30c89792001-09-07 16:35:17 +0000800 char *StrVal; // This memory is strdup'd!
Brian Gaeked0fde302003-11-11 22:41:34 +0000801 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000802
Brian Gaeked0fde302003-11-11 22:41:34 +0000803 llvm::Instruction::BinaryOps BinaryOpVal;
804 llvm::Instruction::TermOps TermOpVal;
805 llvm::Instruction::MemoryOps MemOpVal;
806 llvm::Instruction::OtherOps OtherOpVal;
807 llvm::Module::Endianness Endianness;
Chris Lattner00950542001-06-06 20:29:01 +0000808}
809
Chris Lattner79df7c02002-03-26 18:01:55 +0000810%type <ModuleVal> Module FunctionList
811%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000812%type <BasicBlockVal> BasicBlock InstructionList
813%type <TermInstVal> BBTerminatorInst
814%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000815%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000816%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000817%type <ArgList> ArgList ArgListH
818%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000819%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000820%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000821%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000822%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000823%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +0000824%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner15c9c032003-09-08 18:20:29 +0000825%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattner4ad02e72003-04-16 20:28:45 +0000826%type <Linkage> OptLinkage
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000827%type <Endianness> BigOrLittle
Chris Lattner00950542001-06-06 20:29:01 +0000828
Chris Lattner2079fde2001-10-13 06:41:08 +0000829// ValueRef - Unresolved reference to a definition or BB
830%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000831%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000832// Tokens and types for handling constant integer values
833//
834// ESINT64VAL - A negative number within long long range
835%token <SInt64Val> ESINT64VAL
836
837// EUINT64VAL - A positive number within uns. long long range
838%token <UInt64Val> EUINT64VAL
839%type <SInt64Val> EINT64VAL
840
841%token <SIntVal> SINTVAL // Signed 32 bit ints...
842%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
843%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000844%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000845
846// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000847%type <TypeVal> Types TypesV UpRTypes UpRTypesV
848%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000849%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
850%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000851
Chris Lattner6c23f572003-08-22 05:42:10 +0000852%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
853%type <StrVal> Name OptName OptAssign
Chris Lattner00950542001-06-06 20:29:01 +0000854
855
Chris Lattnerf4600482003-06-28 20:01:34 +0000856%token IMPLEMENTATION ZEROINITIALIZER TRUE FALSE BEGINTOK ENDTOK
Chris Lattner15c9c032003-09-08 18:20:29 +0000857%token DECLARE GLOBAL CONSTANT VOLATILE
Chris Lattnera58e3a12004-02-08 21:48:25 +0000858%token TO DOTDOTDOT NULL_TOK CONST INTERNAL LINKONCE WEAK APPENDING
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000859%token OPAQUE NOT EXTERNAL TARGET ENDIAN POINTERSIZE LITTLE BIG
Chris Lattner00950542001-06-06 20:29:01 +0000860
861// Basic Block Terminating Operators
Chris Lattner36143fc2003-09-08 18:54:55 +0000862%token <TermOpVal> RET BR SWITCH INVOKE UNWIND
Chris Lattner00950542001-06-06 20:29:01 +0000863
Chris Lattner00950542001-06-06 20:29:01 +0000864// Binary Operators
865%type <BinaryOpVal> BinaryOps // all the binary operators
Chris Lattner4a6482b2002-09-10 19:57:26 +0000866%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattner42c9e772001-10-20 09:32:59 +0000867%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000868%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000869
870// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000871%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000872
Chris Lattner027dcc52001-07-08 21:10:27 +0000873// Other Operators
874%type <OtherOpVal> ShiftOps
Chris Lattner3b237fc2003-10-19 21:34:28 +0000875%token <OtherOpVal> PHI_TOK CALL CAST SHL SHR VAARG VANEXT
Chris Lattner99e7ab72003-10-18 05:53:13 +0000876%token VA_ARG // FIXME: OBSOLETE
Chris Lattner027dcc52001-07-08 21:10:27 +0000877
Chris Lattner00950542001-06-06 20:29:01 +0000878%start Module
879%%
880
881// Handle constant integer size restriction and conversion...
882//
Chris Lattner51727be2002-06-04 21:58:56 +0000883INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +0000884INTVAL : UINTVAL {
885 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
886 ThrowException("Value too large for type!");
887 $$ = (int32_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000888};
Chris Lattner00950542001-06-06 20:29:01 +0000889
890
Chris Lattner51727be2002-06-04 21:58:56 +0000891EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +0000892EINT64VAL : EUINT64VAL {
893 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
894 ThrowException("Value too large for type!");
895 $$ = (int64_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000896};
Chris Lattner00950542001-06-06 20:29:01 +0000897
Chris Lattner00950542001-06-06 20:29:01 +0000898// Operations that are notably excluded from this list include:
899// RET, BR, & SWITCH because they end basic blocks and are treated specially.
900//
Chris Lattner4a6482b2002-09-10 19:57:26 +0000901ArithmeticOps: ADD | SUB | MUL | DIV | REM;
902LogicalOps : AND | OR | XOR;
903SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
904BinaryOps : ArithmeticOps | LogicalOps | SetCondOps;
905
Chris Lattner51727be2002-06-04 21:58:56 +0000906ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +0000907
Chris Lattnere98dda62001-07-14 06:10:16 +0000908// These are some types that allow classification if we only want a particular
909// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +0000910SIntType : LONG | INT | SHORT | SBYTE;
911UIntType : ULONG | UINT | USHORT | UBYTE;
912IntType : SIntType | UIntType;
913FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +0000914
Chris Lattnere98dda62001-07-14 06:10:16 +0000915// OptAssign - Value producing statements have an optional assignment component
Chris Lattner6c23f572003-08-22 05:42:10 +0000916OptAssign : Name '=' {
Chris Lattner00950542001-06-06 20:29:01 +0000917 $$ = $1;
918 }
919 | /*empty*/ {
920 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +0000921 };
Chris Lattner00950542001-06-06 20:29:01 +0000922
Chris Lattner4ad02e72003-04-16 20:28:45 +0000923OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
924 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
Chris Lattner72ac148d2003-10-16 18:29:00 +0000925 WEAK { $$ = GlobalValue::WeakLinkage; } |
Chris Lattner4ad02e72003-04-16 20:28:45 +0000926 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
927 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattner30c89792001-09-07 16:35:17 +0000928
929//===----------------------------------------------------------------------===//
930// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +0000931// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +0000932// access to it, a user must explicitly use TypesV.
933//
934
935// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +0000936TypesV : Types | VOID { $$ = new PATypeHolder($1); };
937UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +0000938
939Types : UpRTypes {
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000940 if (!UpRefs.empty())
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000941 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
942 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000943 };
Chris Lattner30c89792001-09-07 16:35:17 +0000944
945
946// Derived types are added later...
947//
Chris Lattner51727be2002-06-04 21:58:56 +0000948PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
949PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000950UpRTypes : OPAQUE {
951 $$ = new PATypeHolder(OpaqueType::get());
952 }
953 | PrimType {
954 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +0000955 };
Chris Lattnerd78700d2002-08-16 21:14:40 +0000956UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000957 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +0000958};
Chris Lattner30c89792001-09-07 16:35:17 +0000959
Chris Lattner30c89792001-09-07 16:35:17 +0000960// Include derived types in the Types production.
961//
962UpRTypes : '\\' EUINT64VAL { // Type UpReference
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000963 if ($2 > (uint64_t)~0U) ThrowException("Value out of range!");
Chris Lattner30c89792001-09-07 16:35:17 +0000964 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3b073862004-02-09 03:19:29 +0000965 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000966 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +0000967 UR_OUT("New Upreference!\n");
968 }
Chris Lattner79df7c02002-03-26 18:01:55 +0000969 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner9232b992003-04-22 18:42:41 +0000970 std::vector<const Type*> Params;
Chris Lattner697954c2002-01-20 22:54:45 +0000971 mapto($3->begin(), $3->end(), std::back_inserter(Params),
Chris Lattner876dc572003-10-02 19:00:34 +0000972 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner2079fde2001-10-13 06:41:08 +0000973 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
974 if (isVarArg) Params.pop_back();
975
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000976 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +0000977 delete $3; // Delete the argument list
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000978 delete $1; // Delete the return type handle
Chris Lattner30c89792001-09-07 16:35:17 +0000979 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000980 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000981 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000982 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +0000983 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000984 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +0000985 std::vector<const Type*> Elements;
Chris Lattner697954c2002-01-20 22:54:45 +0000986 mapto($2->begin(), $2->end(), std::back_inserter(Elements),
Chris Lattner876dc572003-10-02 19:00:34 +0000987 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner30c89792001-09-07 16:35:17 +0000988
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000989 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000990 delete $2;
991 }
992 | '{' '}' { // Empty structure type?
Chris Lattner9232b992003-04-22 18:42:41 +0000993 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000994 }
995 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000996 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000997 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000998 };
Chris Lattner30c89792001-09-07 16:35:17 +0000999
Chris Lattner7e708292002-06-25 16:13:24 +00001000// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +00001001// declaration type lists
1002//
1003TypeListI : UpRTypes {
Chris Lattner9232b992003-04-22 18:42:41 +00001004 $$ = new std::list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +00001005 $$->push_back(*$1); delete $1;
1006 }
1007 | TypeListI ',' UpRTypes {
1008 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +00001009 };
Chris Lattner30c89792001-09-07 16:35:17 +00001010
Chris Lattner7e708292002-06-25 16:13:24 +00001011// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +00001012ArgTypeListI : TypeListI
1013 | TypeListI ',' DOTDOTDOT {
1014 ($$=$1)->push_back(Type::VoidTy);
1015 }
1016 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001017 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +00001018 }
1019 | /*empty*/ {
Chris Lattner9232b992003-04-22 18:42:41 +00001020 $$ = new std::list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +00001021 };
Chris Lattner30c89792001-09-07 16:35:17 +00001022
Chris Lattnere98dda62001-07-14 06:10:16 +00001023// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +00001024// production is used ONLY to represent constants that show up AFTER a 'const',
1025// 'constant' or 'global' token at global scope. Constants that can be inlined
1026// into other expressions (such as integers and constexprs) are handled by the
1027// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +00001028//
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001029ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Chris Lattner949a3622003-07-23 15:30:06 +00001030 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001031 if (ATy == 0)
1032 ThrowException("Cannot make array constant with type: '" +
1033 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001034 const Type *ETy = ATy->getElementType();
1035 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +00001036
Chris Lattner30c89792001-09-07 16:35:17 +00001037 // Verify that we have the correct size...
1038 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +00001039 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +00001040 utostr($3->size()) + " arguments, but has size of " +
1041 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +00001042
Chris Lattner30c89792001-09-07 16:35:17 +00001043 // Verify all elements are correct type!
1044 for (unsigned i = 0; i < $3->size(); i++) {
1045 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +00001046 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001047 ETy->getDescription() +"' as required!\nIt is of type '"+
1048 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +00001049 }
1050
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001051 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001052 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001053 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001054 | Types '[' ']' {
Chris Lattner949a3622003-07-23 15:30:06 +00001055 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001056 if (ATy == 0)
1057 ThrowException("Cannot make array constant with type: '" +
1058 (*$1)->getDescription() + "'!");
1059
1060 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +00001061 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +00001062 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +00001063 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattner9232b992003-04-22 18:42:41 +00001064 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +00001065 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001066 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001067 | Types 'c' STRINGCONSTANT {
Chris Lattner949a3622003-07-23 15:30:06 +00001068 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001069 if (ATy == 0)
1070 ThrowException("Cannot make array constant with type: '" +
1071 (*$1)->getDescription() + "'!");
1072
Chris Lattner30c89792001-09-07 16:35:17 +00001073 int NumElements = ATy->getNumElements();
1074 const Type *ETy = ATy->getElementType();
1075 char *EndStr = UnEscapeLexed($3, true);
1076 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +00001077 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +00001078 itostr((int)(EndStr-$3)) +
1079 " when array has size " + itostr(NumElements) + "!");
Chris Lattner9232b992003-04-22 18:42:41 +00001080 std::vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +00001081 if (ETy == Type::SByteTy) {
1082 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001083 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +00001084 } else if (ETy == Type::UByteTy) {
1085 for (char *C = $3; C != EndStr; ++C)
Chris Lattnerbae362f2003-01-30 22:24:26 +00001086 Vals.push_back(ConstantUInt::get(ETy, (unsigned char)*C));
Chris Lattner93750fa2001-07-28 17:48:55 +00001087 } else {
Chris Lattner30c89792001-09-07 16:35:17 +00001088 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +00001089 ThrowException("Cannot build string arrays of non byte sized elements!");
1090 }
Chris Lattner30c89792001-09-07 16:35:17 +00001091 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001092 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +00001093 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001094 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001095 | Types '{' ConstVector '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001096 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001097 if (STy == 0)
1098 ThrowException("Cannot make struct constant with type: '" +
1099 (*$1)->getDescription() + "'!");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001100
Chris Lattnerc6212f12003-05-21 16:06:56 +00001101 if ($3->size() != STy->getNumContainedTypes())
1102 ThrowException("Illegal number of initializers for structure type!");
1103
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001104 // Check to ensure that constants are compatible with the type initializer!
1105 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Chris Lattnerd21cd802004-02-09 04:37:31 +00001106 if ((*$3)[i]->getType() != STy->getElementType(i))
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001107 ThrowException("Expected type '" +
Chris Lattnerd21cd802004-02-09 04:37:31 +00001108 STy->getElementType(i)->getDescription() +
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001109 "' for element #" + utostr(i) +
1110 " of structure initializer!");
1111
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001112 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001113 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001114 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001115 | Types '{' '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001116 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001117 if (STy == 0)
1118 ThrowException("Cannot make struct constant with type: '" +
1119 (*$1)->getDescription() + "'!");
1120
1121 if (STy->getNumContainedTypes() != 0)
1122 ThrowException("Illegal number of initializers for structure type!");
1123
1124 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1125 delete $1;
1126 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001127 | Types NULL_TOK {
Chris Lattner949a3622003-07-23 15:30:06 +00001128 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001129 if (PTy == 0)
1130 ThrowException("Cannot make null pointer constant with type: '" +
1131 (*$1)->getDescription() + "'!");
1132
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001133 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001134 delete $1;
1135 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001136 | Types SymbolicValueRef {
Chris Lattner949a3622003-07-23 15:30:06 +00001137 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001138 if (Ty == 0)
1139 ThrowException("Global const reference must be a pointer type!");
1140
Chris Lattner3101c252002-08-15 17:58:33 +00001141 // ConstExprs can exist in the body of a function, thus creating
1142 // ConstantPointerRefs whenever they refer to a variable. Because we are in
1143 // the context of a function, getValNonImprovising will search the functions
1144 // symbol table instead of the module symbol table for the global symbol,
1145 // which throws things all off. To get around this, we just tell
1146 // getValNonImprovising that we are at global scope here.
1147 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001148 Function *SavedCurFn = CurFun.CurrentFunction;
1149 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001150
Chris Lattner2079fde2001-10-13 06:41:08 +00001151 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001152
Chris Lattner394f2eb2003-10-16 20:12:13 +00001153 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001154
Chris Lattner2079fde2001-10-13 06:41:08 +00001155 // If this is an initializer for a constant pointer, which is referencing a
1156 // (currently) undefined variable, create a stub now that shall be replaced
1157 // in the future with the right type of variable.
1158 //
1159 if (V == 0) {
1160 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1161 const PointerType *PT = cast<PointerType>(Ty);
1162
1163 // First check to see if the forward references value is already created!
1164 PerModuleInfo::GlobalRefsType::iterator I =
Chris Lattner9232b992003-04-22 18:42:41 +00001165 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001166
1167 if (I != CurModule.GlobalRefs.end()) {
1168 V = I->second; // Placeholder already exists, use it...
Chris Lattnera989b232003-12-23 20:05:15 +00001169 $2.destroy();
Chris Lattner2079fde2001-10-13 06:41:08 +00001170 } else {
Chris Lattner2079fde2001-10-13 06:41:08 +00001171 // Create a placeholder for the global variable reference...
Chris Lattner7a176752001-12-04 00:03:30 +00001172 GlobalVariable *GV = new GlobalVariable(PT->getElementType(),
Chris Lattner4ad02e72003-04-16 20:28:45 +00001173 false,
1174 GlobalValue::ExternalLinkage);
Chris Lattner2079fde2001-10-13 06:41:08 +00001175 // Keep track of the fact that we have a forward ref to recycle it
Chris Lattner9232b992003-04-22 18:42:41 +00001176 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
Chris Lattner2079fde2001-10-13 06:41:08 +00001177
1178 // Must temporarily push this value into the module table...
1179 CurModule.CurrentModule->getGlobalList().push_back(GV);
1180 V = GV;
1181 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001182 }
1183
Chris Lattner2079fde2001-10-13 06:41:08 +00001184 GlobalValue *GV = cast<GlobalValue>(V);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001185 $$ = ConstantPointerRef::get(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001186 delete $1; // Free the type handle
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001187 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001188 | Types ConstExpr {
1189 if ($1->get() != $2->getType())
1190 ThrowException("Mismatched types for constant expression!");
1191 $$ = $2;
1192 delete $1;
Chris Lattnerf4600482003-06-28 20:01:34 +00001193 }
1194 | Types ZEROINITIALIZER {
1195 $$ = Constant::getNullValue($1->get());
1196 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001197 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001198
Chris Lattnere43f40b2002-10-09 00:25:32 +00001199ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001200 if (!ConstantSInt::isValueValidForType($1, $2))
1201 ThrowException("Constant value doesn't fit in type!");
1202 $$ = ConstantSInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001203 }
1204 | UIntType EUINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001205 if (!ConstantUInt::isValueValidForType($1, $2))
1206 ThrowException("Constant value doesn't fit in type!");
1207 $$ = ConstantUInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001208 }
1209 | BOOL TRUE { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001210 $$ = ConstantBool::True;
1211 }
Chris Lattnere43f40b2002-10-09 00:25:32 +00001212 | BOOL FALSE { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001213 $$ = ConstantBool::False;
1214 }
1215 | FPType FPVAL { // Float & Double constants
1216 $$ = ConstantFP::get($1, $2);
1217 };
1218
Chris Lattner00950542001-06-06 20:29:01 +00001219
Chris Lattnerd78700d2002-08-16 21:14:40 +00001220ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattnerae711a82003-11-21 20:27:35 +00001221 if (!$3->getType()->isFirstClassType())
1222 ThrowException("cast constant expression from a non-primitive type: '" +
1223 $3->getType()->getDescription() + "'!");
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001224 if (!$5->get()->isFirstClassType())
1225 ThrowException("cast constant expression to a non-primitive type: '" +
1226 $5->get()->getDescription() + "'!");
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001227 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001228 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001229 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001230 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1231 if (!isa<PointerType>($3->getType()))
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001232 ThrowException("GetElementPtr requires a pointer operand!");
1233
1234 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001235 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001236 if (!IdxTy)
1237 ThrowException("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001238
Chris Lattner9232b992003-04-22 18:42:41 +00001239 std::vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001240 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1241 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001242 IdxVec.push_back(C);
1243 else
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001244 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001245
Chris Lattnerd78700d2002-08-16 21:14:40 +00001246 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001247
Chris Lattnerd78700d2002-08-16 21:14:40 +00001248 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001249 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001250 | BinaryOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001251 if ($3->getType() != $5->getType())
1252 ThrowException("Binary operator types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001253 $$ = ConstantExpr::get($1, $3, $5);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001254 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001255 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001256 if ($5->getType() != Type::UByteTy)
1257 ThrowException("Shift count for shift constant must be unsigned byte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001258 if (!$3->getType()->isInteger())
1259 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerb16689b2004-01-12 19:08:43 +00001260 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner699f1eb2002-08-14 17:12:33 +00001261 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001262
1263
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001264// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001265ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001266 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001267 }
1268 | ConstVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001269 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001270 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001271 };
Chris Lattner00950542001-06-06 20:29:01 +00001272
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001273
Chris Lattner1781aca2001-09-18 04:00:54 +00001274// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001275GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001276
Chris Lattner00950542001-06-06 20:29:01 +00001277
Chris Lattner0e73ce62002-05-02 19:11:13 +00001278//===----------------------------------------------------------------------===//
1279// Rules to match Modules
1280//===----------------------------------------------------------------------===//
1281
1282// Module rule: Capture the result of parsing the whole file into a result
1283// variable...
1284//
1285Module : FunctionList {
1286 $$ = ParserResult = $1;
1287 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001288};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001289
Chris Lattner7e708292002-06-25 16:13:24 +00001290// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001291//
1292FunctionList : FunctionList Function {
1293 $$ = $1;
1294 assert($2->getParent() == 0 && "Function already in module!");
1295 $1->getFunctionList().push_back($2);
Chris Lattner394f2eb2003-10-16 20:12:13 +00001296 CurFun.FunctionDone();
Chris Lattner0e73ce62002-05-02 19:11:13 +00001297 }
1298 | FunctionList FunctionProto {
1299 $$ = $1;
1300 }
1301 | FunctionList IMPLEMENTATION {
1302 $$ = $1;
1303 }
1304 | ConstPool {
1305 $$ = CurModule.CurrentModule;
1306 // Resolve circular types before we parse the body of the module
1307 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001308 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001309
Chris Lattnere98dda62001-07-14 06:10:16 +00001310// ConstPool - Constants with optional names assigned to them.
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001311ConstPool : ConstPool OptAssign CONST ConstVal {
Chris Lattneradf99702003-03-03 23:28:55 +00001312 if (!setValueName($4, $2))
1313 InsertValue($4);
Chris Lattner00950542001-06-06 20:29:01 +00001314 }
Chris Lattner30c89792001-09-07 16:35:17 +00001315 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner4a42e902001-10-22 05:56:09 +00001316 // Eagerly resolve types. This is not an optimization, this is a
1317 // requirement that is due to the fact that we could have this:
1318 //
1319 // %list = type { %list * }
1320 // %list = type { %list * } ; repeated type decl
1321 //
1322 // If types are not resolved eagerly, then the two types will not be
1323 // determined to be the same type!
1324 //
1325 ResolveTypeTo($2, $4->get());
1326
Chris Lattner1781aca2001-09-18 04:00:54 +00001327 // TODO: FIXME when Type are not const
Chris Lattnerb7474512001-10-03 15:39:04 +00001328 if (!setValueName(const_cast<Type*>($4->get()), $2)) {
1329 // If this is not a redefinition of a type...
1330 if (!$2) {
1331 InsertType($4->get(),
Chris Lattner394f2eb2003-10-16 20:12:13 +00001332 inFunctionScope() ? CurFun.Types : CurModule.Types);
Chris Lattnerb7474512001-10-03 15:39:04 +00001333 }
Chris Lattner30c89792001-09-07 16:35:17 +00001334 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001335
1336 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001337 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001338 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001339 }
Chris Lattner4ad02e72003-04-16 20:28:45 +00001340 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Chris Lattnerdda71962001-11-26 18:54:16 +00001341 const Type *Ty = $5->getType();
Chris Lattner1781aca2001-09-18 04:00:54 +00001342 // Global declarations appear in Constant Pool
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001343 Constant *Initializer = $5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001344 if (Initializer == 0)
1345 ThrowException("Global value initializer is not a constant!");
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001346
Chris Lattnerdda71962001-11-26 18:54:16 +00001347 GlobalVariable *GV = new GlobalVariable(Ty, $4, $3, Initializer);
Chris Lattnerb7474512001-10-03 15:39:04 +00001348 if (!setValueName(GV, $2)) { // If not redefining...
1349 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001350 int Slot = InsertValue(GV, CurModule.Values);
1351
1352 if (Slot != -1) {
1353 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1354 } else {
1355 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1356 (char*)GV->getName().c_str()));
1357 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001358 }
Chris Lattner1781aca2001-09-18 04:00:54 +00001359 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001360 | ConstPool OptAssign EXTERNAL GlobalType Types {
1361 const Type *Ty = *$5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001362 // Global declarations appear in Constant Pool
Chris Lattner4ad02e72003-04-16 20:28:45 +00001363 GlobalVariable *GV = new GlobalVariable(Ty,$4,GlobalValue::ExternalLinkage);
Chris Lattnerb7474512001-10-03 15:39:04 +00001364 if (!setValueName(GV, $2)) { // If not redefining...
1365 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001366 int Slot = InsertValue(GV, CurModule.Values);
1367
1368 if (Slot != -1) {
1369 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1370 } else {
1371 assert(GV->hasName() && "Not named and not numbered!?");
1372 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1373 (char*)GV->getName().c_str()));
1374 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001375 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001376 delete $5;
Chris Lattnere98dda62001-07-14 06:10:16 +00001377 }
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001378 | ConstPool TARGET TargetDefinition {
1379 }
Chris Lattner00950542001-06-06 20:29:01 +00001380 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001381 };
Chris Lattner00950542001-06-06 20:29:01 +00001382
1383
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001384
1385BigOrLittle : BIG { $$ = Module::BigEndian; };
1386BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1387
1388TargetDefinition : ENDIAN '=' BigOrLittle {
1389 CurModule.CurrentModule->setEndianness($3);
1390 }
1391 | POINTERSIZE '=' EUINT64VAL {
1392 if ($3 == 32)
1393 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1394 else if ($3 == 64)
1395 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1396 else
1397 ThrowException("Invalid pointer size: '" + utostr($3) + "'!");
1398 };
1399
1400
Chris Lattner00950542001-06-06 20:29:01 +00001401//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001402// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001403//===----------------------------------------------------------------------===//
1404
Chris Lattner6c23f572003-08-22 05:42:10 +00001405Name : VAR_ID | STRINGCONSTANT;
1406OptName : Name | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001407
Chris Lattner6c23f572003-08-22 05:42:10 +00001408ArgVal : Types OptName {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001409 if (*$1 == Type::VoidTy)
1410 ThrowException("void typed arguments are invalid!");
Chris Lattner9232b992003-04-22 18:42:41 +00001411 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
Chris Lattner51727be2002-06-04 21:58:56 +00001412};
Chris Lattner00950542001-06-06 20:29:01 +00001413
Chris Lattner69da5cf2002-10-13 20:57:00 +00001414ArgListH : ArgListH ',' ArgVal {
1415 $$ = $1;
1416 $1->push_back(*$3);
1417 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001418 }
1419 | ArgVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001420 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
Chris Lattner69da5cf2002-10-13 20:57:00 +00001421 $$->push_back(*$1);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001422 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001423 };
Chris Lattner00950542001-06-06 20:29:01 +00001424
1425ArgList : ArgListH {
1426 $$ = $1;
1427 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001428 | ArgListH ',' DOTDOTDOT {
1429 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001430 $$->push_back(std::pair<PATypeHolder*,
1431 char*>(new PATypeHolder(Type::VoidTy), 0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001432 }
1433 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001434 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1435 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001436 }
Chris Lattner00950542001-06-06 20:29:01 +00001437 | /* empty */ {
1438 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001439 };
Chris Lattner00950542001-06-06 20:29:01 +00001440
Chris Lattner6c23f572003-08-22 05:42:10 +00001441FunctionHeaderH : TypesV Name '(' ArgList ')' {
Chris Lattner1f862af2003-04-16 18:13:57 +00001442 UnEscapeLexed($2);
Chris Lattner9232b992003-04-22 18:42:41 +00001443 std::string FunctionName($2);
Chris Lattnerdda71962001-11-26 18:54:16 +00001444
Chris Lattnerc282f5a2003-11-21 22:32:23 +00001445 if (!(*$1)->isFirstClassType() && *$1 != Type::VoidTy)
1446 ThrowException("LLVM functions cannot return aggregate types!");
1447
Chris Lattner9232b992003-04-22 18:42:41 +00001448 std::vector<const Type*> ParamTypeList;
Chris Lattner1f862af2003-04-16 18:13:57 +00001449 if ($4) { // If there are arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001450 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001451 I != $4->end(); ++I)
Chris Lattner69da5cf2002-10-13 20:57:00 +00001452 ParamTypeList.push_back(I->first->get());
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001453 }
Chris Lattner00950542001-06-06 20:29:01 +00001454
Chris Lattner2079fde2001-10-13 06:41:08 +00001455 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1456 if (isVarArg) ParamTypeList.pop_back();
1457
Chris Lattner1f862af2003-04-16 18:13:57 +00001458 const FunctionType *FT = FunctionType::get(*$1, ParamTypeList, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001459 const PointerType *PFT = PointerType::get(FT);
Chris Lattner1f862af2003-04-16 18:13:57 +00001460 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001461
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001462 Function *Fn = 0;
1463 // Is the function already in symtab?
1464 if ((Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1465 // Yes it is. If this is the case, either we need to be a forward decl,
1466 // or it needs to be.
Chris Lattner394f2eb2003-10-16 20:12:13 +00001467 if (!CurFun.isDeclare && !Fn->isExternal())
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001468 ThrowException("Redefinition of function '" + FunctionName + "'!");
1469
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001470 // If we found a preexisting function prototype, remove it from the
1471 // module, so that we don't get spurious conflicts with global & local
1472 // variables.
1473 //
1474 CurModule.CurrentModule->getFunctionList().remove(Fn);
Chris Lattner34538142002-03-08 19:11:42 +00001475
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001476 // Make sure to strip off any argument names so we can't get conflicts...
1477 for (Function::aiterator AI = Fn->abegin(), AE = Fn->aend(); AI != AE; ++AI)
1478 AI->setName("");
Chris Lattner5659dd12002-07-15 00:10:33 +00001479
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001480 } else { // Not already defined?
Chris Lattner4ad02e72003-04-16 20:28:45 +00001481 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001482 InsertValue(Fn, CurModule.Values);
Chris Lattner1f862af2003-04-16 18:13:57 +00001483 CurModule.DeclareNewGlobalValue(Fn, ValID::create($2));
Chris Lattnere1815642001-07-15 06:35:53 +00001484 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001485 free($2); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001486
Chris Lattner394f2eb2003-10-16 20:12:13 +00001487 CurFun.FunctionStart(Fn);
Chris Lattner00950542001-06-06 20:29:01 +00001488
Chris Lattner7e708292002-06-25 16:13:24 +00001489 // Add all of the arguments we parsed to the function...
Chris Lattner1f862af2003-04-16 18:13:57 +00001490 if ($4) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001491 if (isVarArg) { // Nuke the last entry
Chris Lattner1f862af2003-04-16 18:13:57 +00001492 assert($4->back().first->get() == Type::VoidTy && $4->back().second == 0&&
Chris Lattner69da5cf2002-10-13 20:57:00 +00001493 "Not a varargs marker!");
Chris Lattner1f862af2003-04-16 18:13:57 +00001494 delete $4->back().first;
1495 $4->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00001496 }
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001497 Function::aiterator ArgIt = Fn->abegin();
Chris Lattner9232b992003-04-22 18:42:41 +00001498 for (std::vector<std::pair<PATypeHolder*, char*> >::iterator I =$4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001499 I != $4->end(); ++I, ++ArgIt) {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001500 delete I->first; // Delete the typeholder...
1501
1502 if (setValueName(ArgIt, I->second)) // Insert arg into symtab...
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001503 assert(0 && "No arg redef allowed!");
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001504
Chris Lattner69da5cf2002-10-13 20:57:00 +00001505 InsertValue(ArgIt);
Chris Lattner00950542001-06-06 20:29:01 +00001506 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001507
Chris Lattner1f862af2003-04-16 18:13:57 +00001508 delete $4; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00001509 }
Chris Lattner51727be2002-06-04 21:58:56 +00001510};
Chris Lattner00950542001-06-06 20:29:01 +00001511
Chris Lattner9b02cc32002-05-03 18:23:48 +00001512BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1513
Chris Lattner4ad02e72003-04-16 20:28:45 +00001514FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00001515 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001516
Chris Lattner4ad02e72003-04-16 20:28:45 +00001517 // Make sure that we keep track of the linkage type even if there was a
1518 // previous "declare".
1519 $$->setLinkage($1);
Chris Lattner1f862af2003-04-16 18:13:57 +00001520
Chris Lattner7e708292002-06-25 16:13:24 +00001521 // Resolve circular types before we parse the body of the function.
Chris Lattner394f2eb2003-10-16 20:12:13 +00001522 ResolveTypes(CurFun.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001523};
Chris Lattner00950542001-06-06 20:29:01 +00001524
Chris Lattner9b02cc32002-05-03 18:23:48 +00001525END : ENDTOK | '}'; // Allow end of '}' to end a function
1526
Chris Lattner79df7c02002-03-26 18:01:55 +00001527Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001528 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001529};
Chris Lattner00950542001-06-06 20:29:01 +00001530
Chris Lattner394f2eb2003-10-16 20:12:13 +00001531FunctionProto : DECLARE { CurFun.isDeclare = true; } FunctionHeaderH {
1532 $$ = CurFun.CurrentFunction;
Chris Lattner79df7c02002-03-26 18:01:55 +00001533 assert($$->getParent() == 0 && "Function already in module!");
1534 CurModule.CurrentModule->getFunctionList().push_back($$);
Chris Lattner394f2eb2003-10-16 20:12:13 +00001535 CurFun.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001536};
Chris Lattner00950542001-06-06 20:29:01 +00001537
1538//===----------------------------------------------------------------------===//
1539// Rules to match Basic Blocks
1540//===----------------------------------------------------------------------===//
1541
1542ConstValueRef : ESINT64VAL { // A reference to a direct constant
1543 $$ = ValID::create($1);
1544 }
1545 | EUINT64VAL {
1546 $$ = ValID::create($1);
1547 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001548 | FPVAL { // Perhaps it's an FP constant?
1549 $$ = ValID::create($1);
1550 }
Chris Lattner00950542001-06-06 20:29:01 +00001551 | TRUE {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001552 $$ = ValID::create(ConstantBool::True);
Chris Lattner00950542001-06-06 20:29:01 +00001553 }
1554 | FALSE {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001555 $$ = ValID::create(ConstantBool::False);
Chris Lattner00950542001-06-06 20:29:01 +00001556 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001557 | NULL_TOK {
1558 $$ = ValID::createNull();
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001559 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001560 | ConstExpr {
1561 $$ = ValID::create($1);
1562 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00001563
Chris Lattner2079fde2001-10-13 06:41:08 +00001564// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1565// another value.
1566//
1567SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001568 $$ = ValID::create($1);
1569 }
Chris Lattner6c23f572003-08-22 05:42:10 +00001570 | Name { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001571 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001572 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001573
1574// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001575ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001576
Chris Lattner00950542001-06-06 20:29:01 +00001577
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001578// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1579// type immediately preceeds the value reference, and allows complex constant
1580// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001581ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001582 $$ = getVal(*$1, $2); delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001583 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001584
Chris Lattner00950542001-06-06 20:29:01 +00001585BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner7e708292002-06-25 16:13:24 +00001586 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner00950542001-06-06 20:29:01 +00001587 }
Chris Lattner7e708292002-06-25 16:13:24 +00001588 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
1589 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner51727be2002-06-04 21:58:56 +00001590 };
Chris Lattner00950542001-06-06 20:29:01 +00001591
1592
1593// Basic blocks are terminated by branching instructions:
1594// br, br/cc, switch, ret
1595//
Chris Lattner2079fde2001-10-13 06:41:08 +00001596BasicBlock : InstructionList OptAssign BBTerminatorInst {
1597 if (setValueName($3, $2)) { assert(0 && "No redefn allowed!"); }
1598 InsertValue($3);
1599
1600 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001601 InsertValue($1);
1602 $$ = $1;
1603 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001604 | LABELSTR InstructionList OptAssign BBTerminatorInst {
1605 if (setValueName($4, $3)) { assert(0 && "No redefn allowed!"); }
1606 InsertValue($4);
1607
1608 $2->getInstList().push_back($4);
Chris Lattnerb7474512001-10-03 15:39:04 +00001609 if (setValueName($2, $1)) { assert(0 && "No label redef allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001610
1611 InsertValue($2);
1612 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001613 };
Chris Lattner00950542001-06-06 20:29:01 +00001614
1615InstructionList : InstructionList Inst {
1616 $1->getInstList().push_back($2);
1617 $$ = $1;
1618 }
1619 | /* empty */ {
Chris Lattner99e7ab72003-10-18 05:53:13 +00001620 $$ = CurBB = new BasicBlock();
Chris Lattner51727be2002-06-04 21:58:56 +00001621 };
Chris Lattner00950542001-06-06 20:29:01 +00001622
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001623BBTerminatorInst : RET ResolvedVal { // Return with a result...
1624 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001625 }
1626 | RET VOID { // Return with no result...
1627 $$ = new ReturnInst();
1628 }
1629 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner9636a912001-10-01 16:18:37 +00001630 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
Chris Lattner00950542001-06-06 20:29:01 +00001631 } // Conditional Branch...
1632 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner9636a912001-10-01 16:18:37 +00001633 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1634 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
Chris Lattner00950542001-06-06 20:29:01 +00001635 getVal(Type::BoolTy, $3));
1636 }
1637 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1638 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001639 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
Chris Lattner00950542001-06-06 20:29:01 +00001640 $$ = S;
1641
Chris Lattner9232b992003-04-22 18:42:41 +00001642 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00001643 E = $8->end();
1644 for (; I != E; ++I)
Chris Lattner4354f562003-08-23 23:14:52 +00001645 S->addCase(I->first, I->second);
Chris Lattner00950542001-06-06 20:29:01 +00001646 }
Chris Lattner196850c2003-05-15 21:30:00 +00001647 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
1648 SwitchInst *S = new SwitchInst(getVal($2, $3),
1649 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
1650 $$ = S;
1651 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001652 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO ResolvedVal
Chris Lattnera58e3a12004-02-08 21:48:25 +00001653 UNWIND ResolvedVal {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001654 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001655 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001656
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001657 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1658 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001659 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001660 std::vector<const Type*> ParamTypes;
Chris Lattner2079fde2001-10-13 06:41:08 +00001661 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001662 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1663 I != E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001664 ParamTypes.push_back((*I)->getType());
1665 }
1666
1667 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1668 if (isVarArg) ParamTypes.pop_back();
1669
Chris Lattner79df7c02002-03-26 18:01:55 +00001670 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001671 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00001672 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001673
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001674 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00001675
1676 BasicBlock *Normal = dyn_cast<BasicBlock>($8);
1677 BasicBlock *Except = dyn_cast<BasicBlock>($10);
1678
1679 if (Normal == 0 || Except == 0)
1680 ThrowException("Invoke instruction without label destinations!");
1681
1682 // Create the call node...
1683 if (!$5) { // Has no arguments?
Chris Lattner9232b992003-04-22 18:42:41 +00001684 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001685 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001686 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00001687 // correctly!
1688 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001689 FunctionType::param_iterator I = Ty->param_begin();
1690 FunctionType::param_iterator E = Ty->param_end();
Chris Lattner9232b992003-04-22 18:42:41 +00001691 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001692
1693 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1694 if ((*ArgI)->getType() != *I)
1695 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001696 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001697
1698 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1699 ThrowException("Invalid number of parameters detected!");
1700
Chris Lattner6cdb0112001-11-26 16:54:11 +00001701 $$ = new InvokeInst(V, Normal, Except, *$5);
Chris Lattner2079fde2001-10-13 06:41:08 +00001702 }
Chris Lattner9cd42572003-12-23 22:18:36 +00001703 delete $2;
Chris Lattner2079fde2001-10-13 06:41:08 +00001704 delete $5;
Chris Lattner36143fc2003-09-08 18:54:55 +00001705 }
1706 | UNWIND {
1707 $$ = new UnwindInst();
Chris Lattner51727be2002-06-04 21:58:56 +00001708 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001709
1710
Chris Lattner00950542001-06-06 20:29:01 +00001711
1712JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1713 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001714 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001715 if (V == 0)
1716 ThrowException("May only switch on a constant pool value!");
1717
Chris Lattner9232b992003-04-22 18:42:41 +00001718 $$->push_back(std::make_pair(V, cast<BasicBlock>(getVal($5, $6))));
Chris Lattner00950542001-06-06 20:29:01 +00001719 }
1720 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00001721 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001722 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001723
1724 if (V == 0)
1725 ThrowException("May only switch on a constant pool value!");
1726
Chris Lattner9232b992003-04-22 18:42:41 +00001727 $$->push_back(std::make_pair(V, cast<BasicBlock>(getVal($4, $5))));
Chris Lattner51727be2002-06-04 21:58:56 +00001728 };
Chris Lattner00950542001-06-06 20:29:01 +00001729
1730Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001731 // Is this definition named?? if so, assign the name...
1732 if (setValueName($2, $1)) { assert(0 && "No redefin allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001733 InsertValue($2);
1734 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001735};
Chris Lattner00950542001-06-06 20:29:01 +00001736
Chris Lattnerc24d2082001-06-11 15:04:20 +00001737PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Chris Lattner9232b992003-04-22 18:42:41 +00001738 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
1739 $$->push_back(std::make_pair(getVal(*$1, $3),
1740 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
Chris Lattner30c89792001-09-07 16:35:17 +00001741 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001742 }
1743 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1744 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001745 $1->push_back(std::make_pair(getVal($1->front().first->getType(), $4),
1746 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
Chris Lattner51727be2002-06-04 21:58:56 +00001747 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00001748
1749
Chris Lattner30c89792001-09-07 16:35:17 +00001750ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner9232b992003-04-22 18:42:41 +00001751 $$ = new std::vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001752 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001753 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001754 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001755 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001756 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00001757 };
Chris Lattner00950542001-06-06 20:29:01 +00001758
1759// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00001760ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001761
Chris Lattner4a6482b2002-09-10 19:57:26 +00001762InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1763 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint())
1764 ThrowException("Arithmetic operator requires integer or FP operands!");
1765 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1766 if ($$ == 0)
1767 ThrowException("binary operator returned null!");
1768 delete $2;
1769 }
1770 | LogicalOps Types ValueRef ',' ValueRef {
1771 if (!(*$2)->isIntegral())
1772 ThrowException("Logical operator requires integral operands!");
1773 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1774 if ($$ == 0)
1775 ThrowException("binary operator returned null!");
1776 delete $2;
1777 }
1778 | SetCondOps Types ValueRef ',' ValueRef {
Chris Lattner1cff96a2002-09-10 22:37:46 +00001779 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001780 if ($$ == 0)
1781 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001782 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001783 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00001784 | NOT ResolvedVal {
1785 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
1786 << " Replacing with 'xor'.\n";
1787
1788 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
1789 if (Ones == 0)
1790 ThrowException("Expected integral type for not instruction!");
1791
1792 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00001793 if ($$ == 0)
Chris Lattner699f1eb2002-08-14 17:12:33 +00001794 ThrowException("Could not create a xor instruction!");
Chris Lattner09083092001-07-08 04:57:15 +00001795 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001796 | ShiftOps ResolvedVal ',' ResolvedVal {
1797 if ($4->getType() != Type::UByteTy)
1798 ThrowException("Shift amount must be ubyte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001799 if (!$2->getType()->isInteger())
1800 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001801 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001802 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001803 | CAST ResolvedVal TO Types {
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001804 if (!$4->get()->isFirstClassType())
1805 ThrowException("cast instruction to a non-primitive type: '" +
1806 $4->get()->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001807 $$ = new CastInst($2, *$4);
1808 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001809 }
Chris Lattner8f77dae2003-05-08 02:44:12 +00001810 | VA_ARG ResolvedVal ',' Types {
Chris Lattner99e7ab72003-10-18 05:53:13 +00001811 // FIXME: This is emulation code for an obsolete syntax. This should be
1812 // removed at some point.
1813 if (!ObsoleteVarArgs) {
1814 std::cerr << "WARNING: this file uses obsolete features. "
1815 << "Assemble and disassemble to update it.\n";
1816 ObsoleteVarArgs = true;
1817 }
1818
1819 // First, load the valist...
1820 Instruction *CurVAList = new LoadInst($2, "");
1821 CurBB->getInstList().push_back(CurVAList);
1822
1823 // Emit the vaarg instruction.
1824 $$ = new VAArgInst(CurVAList, *$4);
1825
1826 // Now we must advance the pointer and update it in memory.
1827 Instruction *TheVANext = new VANextInst(CurVAList, *$4);
1828 CurBB->getInstList().push_back(TheVANext);
1829
1830 CurBB->getInstList().push_back(new StoreInst(TheVANext, $2));
1831 delete $4;
1832 }
1833 | VAARG ResolvedVal ',' Types {
1834 $$ = new VAArgInst($2, *$4);
1835 delete $4;
1836 }
1837 | VANEXT ResolvedVal ',' Types {
1838 $$ = new VANextInst($2, *$4);
Chris Lattner8f77dae2003-05-08 02:44:12 +00001839 delete $4;
1840 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00001841 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001842 const Type *Ty = $2->front().first->getType();
Chris Lattner8ea8f362003-10-30 01:38:18 +00001843 if (!Ty->isFirstClassType())
1844 ThrowException("PHI node operands must be of first class type!");
Chris Lattnerc24d2082001-06-11 15:04:20 +00001845 $$ = new PHINode(Ty);
Chris Lattnerbd2531f2003-10-10 16:34:58 +00001846 $$->op_reserve($2->size()*2);
Chris Lattner00950542001-06-06 20:29:01 +00001847 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001848 if ($2->front().first->getType() != Ty)
1849 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001850 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001851 $2->pop_front();
1852 }
1853 delete $2; // Free the list...
1854 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001855 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001856 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001857 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001858
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001859 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1860 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001861 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001862 std::vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001863 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001864 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1865 I != E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00001866 ParamTypes.push_back((*I)->getType());
1867 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001868
1869 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1870 if (isVarArg) ParamTypes.pop_back();
1871
Chris Lattner79df7c02002-03-26 18:01:55 +00001872 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001873 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001874 }
Chris Lattner00950542001-06-06 20:29:01 +00001875
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001876 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001877
Chris Lattner8b81bf52001-07-25 22:47:46 +00001878 // Create the call node...
1879 if (!$5) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00001880 // Make sure no arguments is a good thing!
1881 if (Ty->getNumParams() != 0)
1882 ThrowException("No arguments passed to a function that "
1883 "expects arguments!");
1884
Chris Lattner9232b992003-04-22 18:42:41 +00001885 $$ = new CallInst(V, std::vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001886 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001887 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00001888 // correctly!
1889 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001890 FunctionType::param_iterator I = Ty->param_begin();
1891 FunctionType::param_iterator E = Ty->param_end();
Chris Lattner9232b992003-04-22 18:42:41 +00001892 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001893
1894 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1895 if ((*ArgI)->getType() != *I)
1896 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001897 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001898
Chris Lattner8b81bf52001-07-25 22:47:46 +00001899 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001900 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001901
Chris Lattner6cdb0112001-11-26 16:54:11 +00001902 $$ = new CallInst(V, *$5);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001903 }
Chris Lattnerf64d57a2003-12-23 20:39:17 +00001904 delete $2;
Chris Lattner8b81bf52001-07-25 22:47:46 +00001905 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001906 }
1907 | MemoryInst {
1908 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001909 };
Chris Lattner00950542001-06-06 20:29:01 +00001910
Chris Lattner6cdb0112001-11-26 16:54:11 +00001911
1912// IndexList - List of indices for GEP based instructions...
1913IndexList : ',' ValueRefList {
Chris Lattner15c9c032003-09-08 18:20:29 +00001914 $$ = $2;
1915 } | /* empty */ {
1916 $$ = new std::vector<Value*>();
1917 };
1918
1919OptVolatile : VOLATILE {
1920 $$ = true;
1921 }
1922 | /* empty */ {
1923 $$ = false;
1924 };
1925
Chris Lattner027dcc52001-07-08 21:10:27 +00001926
Chris Lattner00950542001-06-06 20:29:01 +00001927MemoryInst : MALLOC Types {
Chris Lattner05804b72002-09-13 22:28:45 +00001928 $$ = new MallocInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00001929 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001930 }
1931 | MALLOC Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00001932 $$ = new MallocInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001933 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001934 }
1935 | ALLOCA Types {
Chris Lattner05804b72002-09-13 22:28:45 +00001936 $$ = new AllocaInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00001937 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001938 }
1939 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00001940 $$ = new AllocaInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001941 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001942 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001943 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00001944 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001945 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00001946 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001947 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001948 }
1949
Chris Lattner15c9c032003-09-08 18:20:29 +00001950 | OptVolatile LOAD Types ValueRef {
1951 if (!isa<PointerType>($3->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00001952 ThrowException("Can't load from nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00001953 (*$3)->getDescription());
Chris Lattner79ad13802003-09-08 20:29:46 +00001954 $$ = new LoadInst(getVal(*$3, $4), "", $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00001955 delete $3;
Chris Lattner027dcc52001-07-08 21:10:27 +00001956 }
Chris Lattner15c9c032003-09-08 18:20:29 +00001957 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
1958 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00001959 if (!PT)
Chris Lattner72e00252001-12-14 16:28:42 +00001960 ThrowException("Can't store to a nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00001961 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00001962 const Type *ElTy = PT->getElementType();
Chris Lattner15c9c032003-09-08 18:20:29 +00001963 if (ElTy != $3->getType())
1964 ThrowException("Can't store '" + $3->getType()->getDescription() +
Chris Lattner72e00252001-12-14 16:28:42 +00001965 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner0383cc42002-08-21 23:51:21 +00001966
Chris Lattner79ad13802003-09-08 20:29:46 +00001967 $$ = new StoreInst($3, getVal(*$5, $6), $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00001968 delete $5;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001969 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00001970 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00001971 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001972 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner30c89792001-09-07 16:35:17 +00001973 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner72e00252001-12-14 16:28:42 +00001974 ThrowException("Can't get element ptr '" + (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001975 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
1976 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00001977 };
Chris Lattner027dcc52001-07-08 21:10:27 +00001978
Brian Gaeked0fde302003-11-11 22:41:34 +00001979
Chris Lattner00950542001-06-06 20:29:01 +00001980%%
Chris Lattner09083092001-07-08 04:57:15 +00001981int yyerror(const char *ErrorMsg) {
Chris Lattner9232b992003-04-22 18:42:41 +00001982 std::string where
1983 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001984 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Chris Lattner9232b992003-04-22 18:42:41 +00001985 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001986 if (yychar == YYEMPTY)
1987 errMsg += "end-of-file.";
1988 else
Chris Lattner9232b992003-04-22 18:42:41 +00001989 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001990 ThrowException(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00001991 return 0;
1992}