blob: d7efe6bd0fb6452e771ede23a37f63556da1f6d1 [file] [log] [blame]
Chris Lattner44d2c352003-10-13 03:32:08 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files --------*- C++ -*-===//
John Criswell29265fe2003-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 Lattner2f7c9632001-06-06 20:29:01 +00009//
10// This file implements the bison parser for LLVM assembly languages files.
11//
Chris Lattner44d2c352003-10-13 03:32:08 +000012//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +000013
Chris Lattner2f7c9632001-06-06 20:29:01 +000014%{
15#include "ParserInternals.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000016#include "llvm/SymbolTable.h"
17#include "llvm/Module.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000018#include "llvm/iTerminators.h"
19#include "llvm/iMemory.h"
Chris Lattnera6834c12002-09-10 22:37:46 +000020#include "llvm/iOperators.h"
Chris Lattnerfb5ae022001-12-03 18:02:31 +000021#include "llvm/iPHINode.h"
Chris Lattner5de22042001-11-27 00:03:19 +000022#include "Support/STLExtras.h"
23#include "Support/DepthFirstIterator.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000024#include <list>
Chris Lattner3cd8c562002-07-30 18:54:25 +000025#include <utility>
Chris Lattneraa534bf2001-09-07 16:35:17 +000026#include <algorithm>
Chris Lattner2f7c9632001-06-06 20:29:01 +000027
Chris Lattner322e49a2001-10-16 19:54:17 +000028int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
Chris Lattnera6821822001-07-08 04:57:15 +000029int yylex(); // declaration" of xxx warnings.
Chris Lattner2f7c9632001-06-06 20:29:01 +000030int yyparse();
31
Brian Gaeke960707c2003-11-11 22:41:34 +000032namespace llvm {
33
Chris Lattner2f7c9632001-06-06 20:29:01 +000034static Module *ParserResult;
Chris Lattner89da8a32003-04-22 18:42:41 +000035std::string CurFilename;
Chris Lattner2f7c9632001-06-06 20:29:01 +000036
Chris Lattneraa534bf2001-09-07 16:35:17 +000037// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
38// relating to upreferences in the input stream.
39//
40//#define DEBUG_UPREFS 1
41#ifdef DEBUG_UPREFS
Chris Lattnerdb3b2022002-08-14 17:12:33 +000042#define UR_OUT(X) std::cerr << X
Chris Lattneraa534bf2001-09-07 16:35:17 +000043#else
44#define UR_OUT(X)
45#endif
46
Vikram S. Adve7064eaf2002-07-14 22:59:28 +000047#define YYERROR_VERBOSE 1
48
Chris Lattner0079e7d2003-10-18 05:53:13 +000049// HACK ALERT: This variable is used to implement the automatic conversion of
50// variable argument instructions from their old to new forms. When this
51// compatiblity "Feature" is removed, this should be too.
52//
53static BasicBlock *CurBB;
54static bool ObsoleteVarArgs;
55
56
Chris Lattner113f4f42002-06-25 16:13:24 +000057// This contains info used when building the body of a function. It is
58// destroyed when the function is completed.
Chris Lattner2f7c9632001-06-06 20:29:01 +000059//
Chris Lattner89da8a32003-04-22 18:42:41 +000060typedef std::vector<Value *> ValueList; // Numbered defs
61static void ResolveDefinitions(std::vector<ValueList> &LateResolvers,
62 std::vector<ValueList> *FutureLateResolvers = 0);
Chris Lattner2f7c9632001-06-06 20:29:01 +000063
64static struct PerModuleInfo {
65 Module *CurrentModule;
Chris Lattner89da8a32003-04-22 18:42:41 +000066 std::vector<ValueList> Values; // Module level numbered definitions
67 std::vector<ValueList> LateResolveValues;
68 std::vector<PATypeHolder> Types;
69 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner2f7c9632001-06-06 20:29:01 +000070
Chris Lattnerf26e0d92001-10-13 06:41:08 +000071 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
72 // references to global values. Global values may be referenced before they
73 // are defined, and if so, the temporary object that they represent is held
Chris Lattner3462ae32001-12-03 22:26:30 +000074 // here. This is used for forward references of ConstantPointerRefs.
Chris Lattnerf26e0d92001-10-13 06:41:08 +000075 //
Chris Lattner89da8a32003-04-22 18:42:41 +000076 typedef std::map<std::pair<const PointerType *,
77 ValID>, GlobalVariable*> GlobalRefsType;
Chris Lattnerf26e0d92001-10-13 06:41:08 +000078 GlobalRefsType GlobalRefs;
79
Chris Lattner2f7c9632001-06-06 20:29:01 +000080 void ModuleDone() {
Chris Lattner113f4f42002-06-25 16:13:24 +000081 // If we could not resolve some functions at function compilation time
82 // (calls to functions before they are defined), resolve them now... Types
83 // are resolved when the constant pool has been completely parsed.
Chris Lattneraa534bf2001-09-07 16:35:17 +000084 //
Chris Lattner2f7c9632001-06-06 20:29:01 +000085 ResolveDefinitions(LateResolveValues);
86
Chris Lattnerf26e0d92001-10-13 06:41:08 +000087 // Check to make sure that all global value forward references have been
88 // resolved!
89 //
90 if (!GlobalRefs.empty()) {
Chris Lattner89da8a32003-04-22 18:42:41 +000091 std::string UndefinedReferences = "Unresolved global references exist:\n";
Chris Lattner1a8ea8a2002-03-11 22:12:39 +000092
93 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
94 I != E; ++I) {
95 UndefinedReferences += " " + I->first.first->getDescription() + " " +
96 I->first.second.getName() + "\n";
97 }
98 ThrowException(UndefinedReferences);
Chris Lattnerf26e0d92001-10-13 06:41:08 +000099 }
100
Chris Lattner113f4f42002-06-25 16:13:24 +0000101 Values.clear(); // Clear out function local definitions
Chris Lattneraa534bf2001-09-07 16:35:17 +0000102 Types.clear();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000103 CurrentModule = 0;
104 }
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000105
106
Vikram S. Adve7064eaf2002-07-14 22:59:28 +0000107 // DeclareNewGlobalValue - Called every time a new GV has been defined. This
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000108 // is used to remove things from the forward declaration map, resolving them
109 // to the correct thing as needed.
110 //
111 void DeclareNewGlobalValue(GlobalValue *GV, ValID D) {
112 // Check to see if there is a forward reference to this global variable...
113 // if there is, eliminate it and patch the reference to use the new def'n.
Chris Lattner89da8a32003-04-22 18:42:41 +0000114 GlobalRefsType::iterator I =
115 GlobalRefs.find(std::make_pair(GV->getType(), D));
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000116
117 if (I != GlobalRefs.end()) {
118 GlobalVariable *OldGV = I->second; // Get the placeholder...
Misha Brukman7eb05a12003-08-18 14:43:39 +0000119 I->first.second.destroy(); // Free string memory if necessary
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000120
121 // Loop over all of the uses of the GlobalValue. The only thing they are
Vikram S. Adve7064eaf2002-07-14 22:59:28 +0000122 // allowed to be is ConstantPointerRef's.
Chris Lattnerf95d9b92003-10-15 16:48:29 +0000123 assert(OldGV->hasOneUse() && "Only one reference should exist!");
Chris Lattner8022a0f2002-10-14 03:28:42 +0000124 User *U = OldGV->use_back(); // Must be a ConstantPointerRef...
125 ConstantPointerRef *CPR = cast<ConstantPointerRef>(U);
Vikram S. Adve7064eaf2002-07-14 22:59:28 +0000126
Chris Lattner8022a0f2002-10-14 03:28:42 +0000127 // Change the const pool reference to point to the real global variable
128 // now. This should drop a use from the OldGV.
129 CPR->mutateReferences(OldGV, GV);
130 assert(OldGV->use_empty() && "All uses should be gone now!");
Vikram S. Adve7064eaf2002-07-14 22:59:28 +0000131
132 // Remove OldGV from the module...
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000133 CurrentModule->getGlobalList().remove(OldGV);
134 delete OldGV; // Delete the old placeholder
Vikram S. Adve7064eaf2002-07-14 22:59:28 +0000135
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000136 // Remove the map entry for the global now that it has been created...
137 GlobalRefs.erase(I);
138 }
139 }
140
Chris Lattner2f7c9632001-06-06 20:29:01 +0000141} CurModule;
142
Chris Lattner57698e22002-03-26 18:01:55 +0000143static struct PerFunctionInfo {
Chris Lattner113f4f42002-06-25 16:13:24 +0000144 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner2f7c9632001-06-06 20:29:01 +0000145
Chris Lattner89da8a32003-04-22 18:42:41 +0000146 std::vector<ValueList> Values; // Keep track of numbered definitions
147 std::vector<ValueList> LateResolveValues;
148 std::vector<PATypeHolder> Types;
149 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattnerebcf5a52003-11-12 04:40:30 +0000150 SymbolTable LocalSymtab;
Chris Lattner113f4f42002-06-25 16:13:24 +0000151 bool isDeclare; // Is this function a forward declararation?
Chris Lattner2f7c9632001-06-06 20:29:01 +0000152
Chris Lattner57698e22002-03-26 18:01:55 +0000153 inline PerFunctionInfo() {
154 CurrentFunction = 0;
Chris Lattner17f729e2001-07-15 06:35:53 +0000155 isDeclare = false;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000156 }
157
Chris Lattner57698e22002-03-26 18:01:55 +0000158 inline void FunctionStart(Function *M) {
159 CurrentFunction = M;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000160 }
161
Chris Lattner57698e22002-03-26 18:01:55 +0000162 void FunctionDone() {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000163 // If we could not resolve some blocks at parsing time (forward branches)
164 // resolve the branches now...
Chris Lattner322e49a2001-10-16 19:54:17 +0000165 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000166
Chris Lattner1fcfaf52003-04-25 21:47:33 +0000167 // Make sure to resolve any constant expr references that might exist within
168 // the function we just declared itself.
169 ValID FID;
170 if (CurrentFunction->hasName()) {
171 FID = ValID::create((char*)CurrentFunction->getName().c_str());
172 } else {
173 unsigned Slot = CurrentFunction->getType()->getUniqueID();
174 assert(CurModule.Values.size() > Slot && "Function not inserted?");
175 // Figure out which slot number if is...
176 for (unsigned i = 0; ; ++i) {
177 assert(i < CurModule.Values[Slot].size() && "Function not found!");
178 if (CurModule.Values[Slot][i] == CurrentFunction) {
179 FID = ValID::create((int)i);
180 break;
181 }
182 }
183 }
184 CurModule.DeclareNewGlobalValue(CurrentFunction, FID);
185
Chris Lattner113f4f42002-06-25 16:13:24 +0000186 Values.clear(); // Clear out function local definitions
Chris Lattnerebcf5a52003-11-12 04:40:30 +0000187 Types.clear(); // Clear out function local types
188 LocalSymtab.clear(); // Clear out function local symbol table
Chris Lattner57698e22002-03-26 18:01:55 +0000189 CurrentFunction = 0;
Chris Lattner17f729e2001-07-15 06:35:53 +0000190 isDeclare = false;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000191 }
Chris Lattner61877742003-10-16 20:12:13 +0000192} CurFun; // Info for the current function...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000193
Chris Lattner61877742003-10-16 20:12:13 +0000194static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
Chris Lattnerfb3e5492001-10-03 15:39:04 +0000195
Chris Lattner2f7c9632001-06-06 20:29:01 +0000196
197//===----------------------------------------------------------------------===//
198// Code to handle definitions of all the types
199//===----------------------------------------------------------------------===//
200
Chris Lattner89da8a32003-04-22 18:42:41 +0000201static int InsertValue(Value *D,
Chris Lattner61877742003-10-16 20:12:13 +0000202 std::vector<ValueList> &ValueTab = CurFun.Values) {
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000203 if (D->hasName()) return -1; // Is this a numbered definition?
204
205 // Yes, insert the value into the value table...
206 unsigned type = D->getType()->getUniqueID();
207 if (ValueTab.size() <= type)
208 ValueTab.resize(type+1, ValueList());
209 //printf("Values[%d][%d] = %d\n", type, ValueTab[type].size(), D);
210 ValueTab[type].push_back(D);
211 return ValueTab[type].size()-1;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000212}
213
Chris Lattneraa534bf2001-09-07 16:35:17 +0000214// TODO: FIXME when Type are not const
Chris Lattner89da8a32003-04-22 18:42:41 +0000215static void InsertType(const Type *Ty, std::vector<PATypeHolder> &Types) {
Chris Lattneraa534bf2001-09-07 16:35:17 +0000216 Types.push_back(Ty);
217}
218
219static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000220 switch (D.Type) {
Chris Lattner8eedb942002-07-18 05:18:37 +0000221 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattneraa534bf2001-09-07 16:35:17 +0000222 unsigned Num = (unsigned)D.Num;
223
224 // Module constants occupy the lowest numbered slots...
225 if (Num < CurModule.Types.size())
226 return CurModule.Types[Num];
227
228 Num -= CurModule.Types.size();
229
230 // Check that the number is within bounds...
Chris Lattner61877742003-10-16 20:12:13 +0000231 if (Num <= CurFun.Types.size())
232 return CurFun.Types[Num];
Chris Lattnerc27b1d72001-10-20 09:32:59 +0000233 break;
Chris Lattneraa534bf2001-09-07 16:35:17 +0000234 }
Chris Lattner8eedb942002-07-18 05:18:37 +0000235 case ValID::NameVal: { // Is it a named definition?
Chris Lattner89da8a32003-04-22 18:42:41 +0000236 std::string Name(D.Name);
Chris Lattneraa534bf2001-09-07 16:35:17 +0000237 SymbolTable *SymTab = 0;
Chris Lattner98cf1f52002-11-20 18:36:02 +0000238 Value *N = 0;
239 if (inFunctionScope()) {
Chris Lattner61877742003-10-16 20:12:13 +0000240 SymTab = &CurFun.CurrentFunction->getSymbolTable();
Chris Lattner98cf1f52002-11-20 18:36:02 +0000241 N = SymTab->lookup(Type::TypeTy, Name);
242 }
Chris Lattneraa534bf2001-09-07 16:35:17 +0000243
244 if (N == 0) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000245 // Symbol table doesn't automatically chain yet... because the function
Chris Lattneraa534bf2001-09-07 16:35:17 +0000246 // hasn't been added to the module...
247 //
Chris Lattner98cf1f52002-11-20 18:36:02 +0000248 SymTab = &CurModule.CurrentModule->getSymbolTable();
249 N = SymTab->lookup(Type::TypeTy, Name);
Chris Lattneraa534bf2001-09-07 16:35:17 +0000250 if (N == 0) break;
251 }
252
253 D.destroy(); // Free old strdup'd memory...
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000254 return cast<Type>(N);
Chris Lattneraa534bf2001-09-07 16:35:17 +0000255 }
256 default:
Vikram S. Adve7064eaf2002-07-14 22:59:28 +0000257 ThrowException("Internal parser error: Invalid symbol type reference!");
Chris Lattneraa534bf2001-09-07 16:35:17 +0000258 }
259
260 // If we reached here, we referenced either a symbol that we don't know about
261 // or an id number that hasn't been read yet. We may be referencing something
262 // forward, so just create an entry to be resolved later and get to it...
263 //
264 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
265
Chris Lattner89da8a32003-04-22 18:42:41 +0000266 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner61877742003-10-16 20:12:13 +0000267 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
Chris Lattnerd50fa5a2001-10-22 05:56:09 +0000268
Chris Lattner89da8a32003-04-22 18:42:41 +0000269 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattnerd50fa5a2001-10-22 05:56:09 +0000270 if (I != LateResolver.end()) {
271 return I->second;
272 }
Chris Lattneraa534bf2001-09-07 16:35:17 +0000273
Chris Lattnere6b470c2001-10-22 06:01:08 +0000274 Type *Typ = OpaqueType::get();
Chris Lattner89da8a32003-04-22 18:42:41 +0000275 LateResolver.insert(std::make_pair(D, Typ));
Chris Lattneraa534bf2001-09-07 16:35:17 +0000276 return Typ;
277}
278
Chris Lattner89da8a32003-04-22 18:42:41 +0000279static Value *lookupInSymbolTable(const Type *Ty, const std::string &Name) {
Chris Lattner98cf1f52002-11-20 18:36:02 +0000280 SymbolTable &SymTab =
Chris Lattner61877742003-10-16 20:12:13 +0000281 inFunctionScope() ? CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner18489fb62002-05-02 19:27:42 +0000282 CurModule.CurrentModule->getSymbolTable();
Chris Lattner98cf1f52002-11-20 18:36:02 +0000283 return SymTab.lookup(Ty, Name);
Chris Lattner60e0dd72001-10-03 06:12:09 +0000284}
285
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000286// getValNonImprovising - Look up the value specified by the provided type and
287// the provided ValID. If the value exists and has already been defined, return
288// it. Otherwise return null.
289//
290static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner57698e22002-03-26 18:01:55 +0000291 if (isa<FunctionType>(Ty))
292 ThrowException("Functions are not values and "
293 "must be referenced as pointers");
Chris Lattner322e49a2001-10-16 19:54:17 +0000294
Chris Lattneraa534bf2001-09-07 16:35:17 +0000295 switch (D.Type) {
Chris Lattnerfbdec252001-09-30 22:46:54 +0000296 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattneraa534bf2001-09-07 16:35:17 +0000297 unsigned type = Ty->getUniqueID();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000298 unsigned Num = (unsigned)D.Num;
299
300 // Module constants occupy the lowest numbered slots...
301 if (type < CurModule.Values.size()) {
302 if (Num < CurModule.Values[type].size())
303 return CurModule.Values[type][Num];
304
305 Num -= CurModule.Values[type].size();
306 }
307
308 // Make sure that our type is within bounds
Chris Lattner61877742003-10-16 20:12:13 +0000309 if (CurFun.Values.size() <= type) return 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000310
311 // Check that the number is within bounds...
Chris Lattner61877742003-10-16 20:12:13 +0000312 if (CurFun.Values[type].size() <= Num) return 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000313
Chris Lattner61877742003-10-16 20:12:13 +0000314 return CurFun.Values[type][Num];
Chris Lattner2f7c9632001-06-06 20:29:01 +0000315 }
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000316
Chris Lattnerfbdec252001-09-30 22:46:54 +0000317 case ValID::NameVal: { // Is it a named definition?
Chris Lattner89da8a32003-04-22 18:42:41 +0000318 Value *N = lookupInSymbolTable(Ty, std::string(D.Name));
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000319 if (N == 0) return 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000320
321 D.destroy(); // Free old strdup'd memory...
322 return N;
323 }
324
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000325 // Check to make sure that "Ty" is an integral type, and that our
326 // value will fit into the specified type...
327 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattner7f325cd2002-08-16 21:14:40 +0000328 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
329 ThrowException("Signed integral constant '" +
330 itostr(D.ConstPool64) + "' is invalid for type '" +
331 Ty->getDescription() + "'!");
332 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000333
334 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattner3462ae32001-12-03 22:26:30 +0000335 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
336 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Chris Lattner8eedb942002-07-18 05:18:37 +0000337 ThrowException("Integral constant '" + utostr(D.UConstPool64) +
338 "' is invalid or out of range!");
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000339 } else { // This is really a signed reference. Transmogrify.
Chris Lattner3462ae32001-12-03 22:26:30 +0000340 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000341 }
342 } else {
Chris Lattner3462ae32001-12-03 22:26:30 +0000343 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000344 }
345
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000346 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattner3462ae32001-12-03 22:26:30 +0000347 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000348 ThrowException("FP constant invalid for type!!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000349 return ConstantFP::get(Ty, D.ConstPoolFP);
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000350
351 case ValID::ConstNullVal: // Is it a null value?
Chris Lattner181cc322002-05-06 16:15:30 +0000352 if (!isa<PointerType>(Ty))
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000353 ThrowException("Cannot create a a non pointer null!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000354 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000355
Chris Lattner7f325cd2002-08-16 21:14:40 +0000356 case ValID::ConstantVal: // Fully resolved constant?
357 if (D.ConstantValue->getType() != Ty)
358 ThrowException("Constant expression type different from required type!");
359 return D.ConstantValue;
360
Chris Lattneraa534bf2001-09-07 16:35:17 +0000361 default:
362 assert(0 && "Unhandled case!");
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000363 return 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000364 } // End of switch
365
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000366 assert(0 && "Unhandled case!");
367 return 0;
368}
369
370
371// getVal - This function is identical to getValNonImprovising, except that if a
372// value is not already defined, it "improvises" by creating a placeholder var
373// that looks and acts just like the requested variable. When the value is
374// defined later, all uses of the placeholder variable are replaced with the
375// real thing.
376//
377static Value *getVal(const Type *Ty, const ValID &D) {
378 assert(Ty != Type::TypeTy && "Should use getTypeVal for types!");
379
380 // See if the value has already been defined...
381 Value *V = getValNonImprovising(Ty, D);
382 if (V) return V;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000383
384 // If we reached here, we referenced either a symbol that we don't know about
385 // or an id number that hasn't been read yet. We may be referencing something
386 // forward, so just create an entry to be resolved later and get to it...
387 //
Chris Lattner2f7c9632001-06-06 20:29:01 +0000388 Value *d = 0;
Chris Lattneraa534bf2001-09-07 16:35:17 +0000389 switch (Ty->getPrimitiveID()) {
390 case Type::LabelTyID: d = new BBPlaceHolder(Ty, D); break;
Chris Lattneraa534bf2001-09-07 16:35:17 +0000391 default: d = new ValuePlaceHolder(Ty, D); break;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000392 }
393
394 assert(d != 0 && "How did we not make something?");
Chris Lattner57698e22002-03-26 18:01:55 +0000395 if (inFunctionScope())
Chris Lattner61877742003-10-16 20:12:13 +0000396 InsertValue(d, CurFun.LateResolveValues);
Chris Lattner322e49a2001-10-16 19:54:17 +0000397 else
398 InsertValue(d, CurModule.LateResolveValues);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000399 return d;
400}
401
402
403//===----------------------------------------------------------------------===//
404// Code to handle forward references in instructions
405//===----------------------------------------------------------------------===//
406//
407// This code handles the late binding needed with statements that reference
408// values not defined yet... for example, a forward branch, or the PHI node for
409// a loop body.
410//
Chris Lattner61877742003-10-16 20:12:13 +0000411// This keeps a table (CurFun.LateResolveValues) of all such forward references
Chris Lattner2f7c9632001-06-06 20:29:01 +0000412// and back patchs after we are done.
413//
414
415// ResolveDefinitions - If we could not resolve some defs at parsing
416// time (forward branches, phi functions for loops, etc...) resolve the
417// defs now...
418//
Chris Lattner89da8a32003-04-22 18:42:41 +0000419static void ResolveDefinitions(std::vector<ValueList> &LateResolvers,
420 std::vector<ValueList> *FutureLateResolvers) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000421 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
422 for (unsigned ty = 0; ty < LateResolvers.size(); ty++) {
423 while (!LateResolvers[ty].empty()) {
424 Value *V = LateResolvers[ty].back();
Chris Lattner322e49a2001-10-16 19:54:17 +0000425 assert(!isa<Type>(V) && "Types should be in LateResolveTypes!");
426
Chris Lattner2f7c9632001-06-06 20:29:01 +0000427 LateResolvers[ty].pop_back();
428 ValID &DID = getValIDFromPlaceHolder(V);
429
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000430 Value *TheRealValue = getValNonImprovising(Type::getUniqueIDType(ty),DID);
Chris Lattner322e49a2001-10-16 19:54:17 +0000431 if (TheRealValue) {
432 V->replaceAllUsesWith(TheRealValue);
433 delete V;
434 } else if (FutureLateResolvers) {
Chris Lattner57698e22002-03-26 18:01:55 +0000435 // Functions have their unresolved items forwarded to the module late
Chris Lattner322e49a2001-10-16 19:54:17 +0000436 // resolver table
437 InsertValue(V, *FutureLateResolvers);
438 } else {
Chris Lattner18489fb62002-05-02 19:27:42 +0000439 if (DID.Type == ValID::NameVal)
Chris Lattneraa534bf2001-09-07 16:35:17 +0000440 ThrowException("Reference to an invalid definition: '" +DID.getName()+
441 "' of type '" + V->getType()->getDescription() + "'",
442 getLineNumFromPlaceHolder(V));
443 else
444 ThrowException("Reference to an invalid definition: #" +
445 itostr(DID.Num) + " of type '" +
446 V->getType()->getDescription() + "'",
447 getLineNumFromPlaceHolder(V));
448 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000449 }
450 }
451
452 LateResolvers.clear();
453}
454
Chris Lattnerd50fa5a2001-10-22 05:56:09 +0000455// ResolveTypeTo - A brand new type was just declared. This means that (if
456// name is not null) things referencing Name can be resolved. Otherwise, things
457// refering to the number can be resolved. Do this now.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000458//
Chris Lattnerd50fa5a2001-10-22 05:56:09 +0000459static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner89da8a32003-04-22 18:42:41 +0000460 std::vector<PATypeHolder> &Types = inFunctionScope() ?
Chris Lattner61877742003-10-16 20:12:13 +0000461 CurFun.Types : CurModule.Types;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000462
Chris Lattnerd50fa5a2001-10-22 05:56:09 +0000463 ValID D;
464 if (Name) D = ValID::create(Name);
465 else D = ValID::create((int)Types.size());
Chris Lattneraa534bf2001-09-07 16:35:17 +0000466
Chris Lattner89da8a32003-04-22 18:42:41 +0000467 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner61877742003-10-16 20:12:13 +0000468 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
Chris Lattnerd50fa5a2001-10-22 05:56:09 +0000469
Chris Lattner89da8a32003-04-22 18:42:41 +0000470 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattnerd50fa5a2001-10-22 05:56:09 +0000471 if (I != LateResolver.end()) {
Chris Lattner8cb1dfb2002-06-04 21:58:56 +0000472 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattnerd50fa5a2001-10-22 05:56:09 +0000473 LateResolver.erase(I);
474 }
475}
476
477// ResolveTypes - At this point, all types should be resolved. Any that aren't
478// are errors.
479//
Chris Lattner89da8a32003-04-22 18:42:41 +0000480static void ResolveTypes(std::map<ValID, PATypeHolder> &LateResolveTypes) {
Chris Lattnerd50fa5a2001-10-22 05:56:09 +0000481 if (!LateResolveTypes.empty()) {
Chris Lattnere6b470c2001-10-22 06:01:08 +0000482 const ValID &DID = LateResolveTypes.begin()->first;
Chris Lattnerd50fa5a2001-10-22 05:56:09 +0000483
484 if (DID.Type == ValID::NameVal)
Chris Lattnere6b470c2001-10-22 06:01:08 +0000485 ThrowException("Reference to an invalid type: '" +DID.getName() + "'");
Chris Lattnerd50fa5a2001-10-22 05:56:09 +0000486 else
Chris Lattnere6b470c2001-10-22 06:01:08 +0000487 ThrowException("Reference to an invalid type: #" + itostr(DID.Num));
Chris Lattneraa534bf2001-09-07 16:35:17 +0000488 }
489}
490
Chris Lattner571a6b02001-10-03 01:49:25 +0000491
Chris Lattner7fb14f52001-09-18 04:00:54 +0000492// setValueName - Set the specified value to the name given. The name may be
493// null potentially, in which case this is a noop. The string passed in is
494// assumed to be a malloc'd string buffer, and is freed by this function.
495//
Chris Lattnerfb3e5492001-10-03 15:39:04 +0000496// This function returns true if the value has already been defined, but is
497// allowed to be redefined in the specified context. If the name is a new name
498// for the typeplane, false is returned.
499//
500static bool setValueName(Value *V, char *NameStr) {
501 if (NameStr == 0) return false;
Chris Lattner322e49a2001-10-16 19:54:17 +0000502
Chris Lattner89da8a32003-04-22 18:42:41 +0000503 std::string Name(NameStr); // Copy string
Chris Lattner7fb14f52001-09-18 04:00:54 +0000504 free(NameStr); // Free old string
505
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000506 if (V->getType() == Type::VoidTy)
507 ThrowException("Can't assign name '" + Name +
508 "' to a null valued instruction!");
509
Chris Lattner98cf1f52002-11-20 18:36:02 +0000510 SymbolTable &ST = inFunctionScope() ?
Chris Lattner61877742003-10-16 20:12:13 +0000511 CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner98cf1f52002-11-20 18:36:02 +0000512 CurModule.CurrentModule->getSymbolTable();
Chris Lattneraa534bf2001-09-07 16:35:17 +0000513
Chris Lattner98cf1f52002-11-20 18:36:02 +0000514 Value *Existing = ST.lookup(V->getType(), Name);
Chris Lattneraa534bf2001-09-07 16:35:17 +0000515 if (Existing) { // Inserting a name that is already defined???
516 // There is only one case where this is allowed: when we are refining an
517 // opaque type. In this case, Existing will be an opaque type.
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000518 if (const Type *Ty = dyn_cast<Type>(Existing)) {
Chris Lattner8cb1dfb2002-06-04 21:58:56 +0000519 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Ty)) {
Chris Lattneraa534bf2001-09-07 16:35:17 +0000520 // We ARE replacing an opaque type!
Chris Lattner8cb1dfb2002-06-04 21:58:56 +0000521 ((OpaqueType*)OpTy)->refineAbstractTypeTo(cast<Type>(V));
Chris Lattnerfb3e5492001-10-03 15:39:04 +0000522 return true;
Chris Lattneraa534bf2001-09-07 16:35:17 +0000523 }
Chris Lattner571a6b02001-10-03 01:49:25 +0000524 }
Chris Lattneraa534bf2001-09-07 16:35:17 +0000525
Chris Lattner4b717c02001-10-01 16:18:37 +0000526 // Otherwise, we are a simple redefinition of a value, check to see if it
527 // is defined the same as the old one...
Chris Lattner8ce4e792003-03-03 23:28:55 +0000528 if (const Type *Ty = dyn_cast<Type>(Existing)) {
529 if (Ty == cast<Type>(V)) return true; // Yes, it's equal.
Chris Lattnerdb3b2022002-08-14 17:12:33 +0000530 // std::cerr << "Type: " << Ty->getDescription() << " != "
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000531 // << cast<Type>(V)->getDescription() << "!\n";
Chris Lattner8ce4e792003-03-03 23:28:55 +0000532 } else if (const Constant *C = dyn_cast<Constant>(Existing)) {
533 if (C == V) return true; // Constants are equal to themselves
Chris Lattnerfb3e5492001-10-03 15:39:04 +0000534 } else if (GlobalVariable *EGV = dyn_cast<GlobalVariable>(Existing)) {
Chris Lattner78326d22001-10-03 19:35:57 +0000535 // We are allowed to redefine a global variable in two circumstances:
536 // 1. If at least one of the globals is uninitialized or
537 // 2. If both initializers have the same value.
538 //
Chris Lattnere504b432001-10-03 19:35:04 +0000539 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
Chris Lattnerb90181e2003-02-02 16:40:20 +0000540 if (!EGV->hasInitializer() || !GV->hasInitializer() ||
541 EGV->getInitializer() == GV->getInitializer()) {
Chris Lattnerfb3e5492001-10-03 15:39:04 +0000542
Chris Lattnerb90181e2003-02-02 16:40:20 +0000543 // Make sure the existing global version gets the initializer! Make
544 // sure that it also gets marked const if the new version is.
Chris Lattnere504b432001-10-03 19:35:04 +0000545 if (GV->hasInitializer() && !EGV->hasInitializer())
546 EGV->setInitializer(GV->getInitializer());
Chris Lattnerb90181e2003-02-02 16:40:20 +0000547 if (GV->isConstant())
548 EGV->setConstant(true);
Chris Lattner379a8d22003-04-16 20:28:45 +0000549 EGV->setLinkage(GV->getLinkage());
Chris Lattnere504b432001-10-03 19:35:04 +0000550
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000551 delete GV; // Destroy the duplicate!
Chris Lattnere504b432001-10-03 19:35:04 +0000552 return true; // They are equivalent!
553 }
Chris Lattnerfb3e5492001-10-03 15:39:04 +0000554 }
Chris Lattner4b717c02001-10-01 16:18:37 +0000555 }
Chris Lattner2a18c642003-11-25 03:54:16 +0000556
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000557 ThrowException("Redefinition of value named '" + Name + "' in the '" +
Chris Lattneraa534bf2001-09-07 16:35:17 +0000558 V->getType()->getDescription() + "' type plane!");
Chris Lattner26e50dc2001-07-28 17:48:55 +0000559 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000560
Chris Lattnerebcf5a52003-11-12 04:40:30 +0000561 // Set the name
Chris Lattner98cf1f52002-11-20 18:36:02 +0000562 V->setName(Name, &ST);
Chris Lattnerebcf5a52003-11-12 04:40:30 +0000563
564 // If we're in function scope
565 if (inFunctionScope()) {
566 // Look up the symbol in the function's local symboltable
567 Existing = CurFun.LocalSymtab.lookup(V->getType(),Name);
568
569 // If it already exists
570 if (Existing) {
Chris Lattnerebcf5a52003-11-12 04:40:30 +0000571 // Bail
572 ThrowException("Redefinition of value named '" + Name + "' in the '" +
573 V->getType()->getDescription() + "' type plane!");
574
575 // otherwise, since it doesn't exist
576 } else {
577 // Insert it.
578 CurFun.LocalSymtab.insert(V);
579 }
580 }
Chris Lattnerfb3e5492001-10-03 15:39:04 +0000581 return false;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000582}
583
Chris Lattnerd9c40e32001-07-09 19:38:36 +0000584
Chris Lattneraa534bf2001-09-07 16:35:17 +0000585//===----------------------------------------------------------------------===//
586// Code for handling upreferences in type names...
Chris Lattnerd9c40e32001-07-09 19:38:36 +0000587//
Chris Lattnerd9c40e32001-07-09 19:38:36 +0000588
Chris Lattner3cb41672004-02-09 03:19:29 +0000589// TypeContains - Returns true if Ty directly contains E in it.
Chris Lattneraa534bf2001-09-07 16:35:17 +0000590//
591static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3cb41672004-02-09 03:19:29 +0000592 return find(Ty->subtype_begin(), Ty->subtype_end(), E) != Ty->subtype_end();
593}
594
595namespace {
596 struct UpRefRecord {
597 // NestingLevel - The number of nesting levels that need to be popped before
598 // this type is resolved.
599 unsigned NestingLevel;
600
601 // LastContainedTy - This is the type at the current binding level for the
602 // type. Every time we reduce the nesting level, this gets updated.
603 const Type *LastContainedTy;
604
605 // UpRefTy - This is the actual opaque type that the upreference is
606 // represented with.
607 OpaqueType *UpRefTy;
608
609 UpRefRecord(unsigned NL, OpaqueType *URTy)
610 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
611 };
Chris Lattneraa534bf2001-09-07 16:35:17 +0000612}
Chris Lattner1caf0bb2001-07-20 19:15:08 +0000613
Chris Lattnerfd2a5142003-12-31 02:18:11 +0000614// UpRefs - A list of the outstanding upreferences that need to be resolved.
Chris Lattner3cb41672004-02-09 03:19:29 +0000615static std::vector<UpRefRecord> UpRefs;
Chris Lattneraa534bf2001-09-07 16:35:17 +0000616
Chris Lattnerfd2a5142003-12-31 02:18:11 +0000617/// HandleUpRefs - Every time we finish a new layer of types, this function is
618/// called. It loops through the UpRefs vector, which is a list of the
619/// currently active types. For each type, if the up reference is contained in
620/// the newly completed type, we decrement the level count. When the level
621/// count reaches zero, the upreferenced type is the type that is passed in:
622/// thus we can complete the cycle.
623///
Chris Lattner30752bd92002-04-04 19:23:55 +0000624static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattnera263b852004-02-09 03:03:10 +0000625 if (!ty->isAbstract()) return ty;
Chris Lattner30752bd92002-04-04 19:23:55 +0000626 PATypeHolder Ty(ty);
Chris Lattnerfd2a5142003-12-31 02:18:11 +0000627 UR_OUT("Type '" << Ty->getDescription() <<
Chris Lattnerf29b2312001-11-02 07:46:26 +0000628 "' newly formed. Resolving upreferences.\n" <<
629 UpRefs.size() << " upreferences active!\n");
Chris Lattnerfd2a5142003-12-31 02:18:11 +0000630 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Chris Lattnerf29b2312001-11-02 07:46:26 +0000631 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Chris Lattneraa534bf2001-09-07 16:35:17 +0000632 << UpRefs[i].second->getDescription() << ") = "
Chris Lattnerfd2a5142003-12-31 02:18:11 +0000633 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner3cb41672004-02-09 03:19:29 +0000634 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
635 // Decrement level of upreference
636 unsigned Level = --UpRefs[i].NestingLevel;
637 UpRefs[i].LastContainedTy = Ty;
Chris Lattnerfd2a5142003-12-31 02:18:11 +0000638 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Chris Lattneraa534bf2001-09-07 16:35:17 +0000639 if (Level == 0) { // Upreference should be resolved!
Chris Lattnerf29b2312001-11-02 07:46:26 +0000640 UR_OUT(" * Resolving upreference for "
Chris Lattnerfd2a5142003-12-31 02:18:11 +0000641 << UpRefs[i].second->getDescription() << "\n";
Chris Lattner3cb41672004-02-09 03:19:29 +0000642 std::string OldName = UpRefs[i].UpRefTy->getDescription());
643 UpRefs[i].UpRefTy->refineAbstractTypeTo(Ty);
Chris Lattnerf29b2312001-11-02 07:46:26 +0000644 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
Chris Lattnerfd2a5142003-12-31 02:18:11 +0000645 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
646 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
647 --i; // Do not skip the next element...
Chris Lattneraa534bf2001-09-07 16:35:17 +0000648 }
649 }
Chris Lattnerd9c40e32001-07-09 19:38:36 +0000650 }
Chris Lattnerfd2a5142003-12-31 02:18:11 +0000651
Chris Lattnerd9c40e32001-07-09 19:38:36 +0000652 return Ty;
653}
654
Chris Lattneraa534bf2001-09-07 16:35:17 +0000655
Chris Lattner2f7c9632001-06-06 20:29:01 +0000656//===----------------------------------------------------------------------===//
657// RunVMAsmParser - Define an interface to this parser
658//===----------------------------------------------------------------------===//
659//
Chris Lattner89da8a32003-04-22 18:42:41 +0000660Module *RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000661 llvmAsmin = F;
Chris Lattnerf2d1e792001-07-22 18:36:00 +0000662 CurFilename = Filename;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000663 llvmAsmlineno = 1; // Reset the current line number...
Chris Lattner0079e7d2003-10-18 05:53:13 +0000664 ObsoleteVarArgs = false;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000665
Chris Lattnerb672b562003-04-22 18:02:52 +0000666 // Allocate a new module to read
667 CurModule.CurrentModule = new Module(Filename);
Chris Lattner6789a0b2003-11-26 07:24:58 +0000668
669 try {
670 yyparse(); // Parse the file.
671 } catch (...) {
672 // Clear the symbol table so it doesn't complain when it
673 // gets destructed
674 CurFun.LocalSymtab.clear();
675 throw;
676 }
Chris Lattner0079e7d2003-10-18 05:53:13 +0000677
Chris Lattner2f7c9632001-06-06 20:29:01 +0000678 Module *Result = ParserResult;
Chris Lattner0079e7d2003-10-18 05:53:13 +0000679
680 // Check to see if they called va_start but not va_arg..
681 if (!ObsoleteVarArgs)
682 if (Function *F = Result->getNamedFunction("llvm.va_start"))
683 if (F->asize() == 1) {
684 std::cerr << "WARNING: this file uses obsolete features. "
685 << "Assemble and disassemble to update it.\n";
686 ObsoleteVarArgs = true;
687 }
688
689
690 if (ObsoleteVarArgs) {
691 // If the user is making use of obsolete varargs intrinsics, adjust them for
692 // the user.
693 if (Function *F = Result->getNamedFunction("llvm.va_start")) {
694 assert(F->asize() == 1 && "Obsolete va_start takes 1 argument!");
695
696 const Type *RetTy = F->getFunctionType()->getParamType(0);
697 RetTy = cast<PointerType>(RetTy)->getElementType();
698 Function *NF = Result->getOrInsertFunction("llvm.va_start", RetTy, 0);
699
700 while (!F->use_empty()) {
701 CallInst *CI = cast<CallInst>(F->use_back());
702 Value *V = new CallInst(NF, "", CI);
703 new StoreInst(V, CI->getOperand(1), CI);
704 CI->getParent()->getInstList().erase(CI);
705 }
706 Result->getFunctionList().erase(F);
707 }
708
709 if (Function *F = Result->getNamedFunction("llvm.va_end")) {
710 assert(F->asize() == 1 && "Obsolete va_end takes 1 argument!");
711 const Type *ArgTy = F->getFunctionType()->getParamType(0);
712 ArgTy = cast<PointerType>(ArgTy)->getElementType();
713 Function *NF = Result->getOrInsertFunction("llvm.va_end", Type::VoidTy,
714 ArgTy, 0);
715
716 while (!F->use_empty()) {
717 CallInst *CI = cast<CallInst>(F->use_back());
718 Value *V = new LoadInst(CI->getOperand(1), "", CI);
719 new CallInst(NF, V, "", CI);
720 CI->getParent()->getInstList().erase(CI);
721 }
722 Result->getFunctionList().erase(F);
723 }
724
725 if (Function *F = Result->getNamedFunction("llvm.va_copy")) {
726 assert(F->asize() == 2 && "Obsolete va_copy takes 2 argument!");
727 const Type *ArgTy = F->getFunctionType()->getParamType(0);
728 ArgTy = cast<PointerType>(ArgTy)->getElementType();
729 Function *NF = Result->getOrInsertFunction("llvm.va_copy", ArgTy,
730 ArgTy, 0);
731
732 while (!F->use_empty()) {
733 CallInst *CI = cast<CallInst>(F->use_back());
734 Value *V = new CallInst(NF, CI->getOperand(2), "", CI);
735 new StoreInst(V, CI->getOperand(1), CI);
736 CI->getParent()->getInstList().erase(CI);
737 }
738 Result->getFunctionList().erase(F);
739 }
740 }
741
Chris Lattner2f7c9632001-06-06 20:29:01 +0000742 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
743 ParserResult = 0;
744
745 return Result;
746}
747
Brian Gaeke960707c2003-11-11 22:41:34 +0000748} // End llvm namespace
749
750using namespace llvm;
751
Chris Lattner2f7c9632001-06-06 20:29:01 +0000752%}
753
754%union {
Brian Gaeke960707c2003-11-11 22:41:34 +0000755 llvm::Module *ModuleVal;
756 llvm::Function *FunctionVal;
757 std::pair<llvm::PATypeHolder*, char*> *ArgVal;
758 llvm::BasicBlock *BasicBlockVal;
759 llvm::TerminatorInst *TermInstVal;
760 llvm::Instruction *InstVal;
761 llvm::Constant *ConstVal;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000762
Brian Gaeke960707c2003-11-11 22:41:34 +0000763 const llvm::Type *PrimType;
764 llvm::PATypeHolder *TypeVal;
765 llvm::Value *ValueVal;
Chris Lattneraa534bf2001-09-07 16:35:17 +0000766
Brian Gaeke960707c2003-11-11 22:41:34 +0000767 std::vector<std::pair<llvm::PATypeHolder*,char*> > *ArgList;
768 std::vector<llvm::Value*> *ValueList;
769 std::list<llvm::PATypeHolder> *TypeList;
770 std::list<std::pair<llvm::Value*,
771 llvm::BasicBlock*> > *PHIList; // Represent the RHS of PHI node
772 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
773 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000774
Brian Gaeke960707c2003-11-11 22:41:34 +0000775 llvm::GlobalValue::LinkageTypes Linkage;
Chris Lattneraa534bf2001-09-07 16:35:17 +0000776 int64_t SInt64Val;
777 uint64_t UInt64Val;
778 int SIntVal;
779 unsigned UIntVal;
780 double FPVal;
Chris Lattner7fb14f52001-09-18 04:00:54 +0000781 bool BoolVal;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000782
Chris Lattneraa534bf2001-09-07 16:35:17 +0000783 char *StrVal; // This memory is strdup'd!
Brian Gaeke960707c2003-11-11 22:41:34 +0000784 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000785
Brian Gaeke960707c2003-11-11 22:41:34 +0000786 llvm::Instruction::BinaryOps BinaryOpVal;
787 llvm::Instruction::TermOps TermOpVal;
788 llvm::Instruction::MemoryOps MemOpVal;
789 llvm::Instruction::OtherOps OtherOpVal;
790 llvm::Module::Endianness Endianness;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000791}
792
Chris Lattner57698e22002-03-26 18:01:55 +0000793%type <ModuleVal> Module FunctionList
794%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner2f7c9632001-06-06 20:29:01 +0000795%type <BasicBlockVal> BasicBlock InstructionList
796%type <TermInstVal> BBTerminatorInst
797%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adve7064eaf2002-07-14 22:59:28 +0000798%type <ConstVal> ConstVal ConstExpr
Chris Lattner476148f2001-11-26 16:54:11 +0000799%type <ConstVector> ConstVector
Chris Lattnerae234e12002-04-09 19:41:42 +0000800%type <ArgList> ArgList ArgListH
801%type <ArgVal> ArgVal
Chris Lattner931ef3b2001-06-11 15:04:20 +0000802%type <PHIList> PHIList
Chris Lattner62ecb4a2001-07-08 23:22:50 +0000803%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner476148f2001-11-26 16:54:11 +0000804%type <ValueList> IndexList // For GEP derived indices
Chris Lattneraa534bf2001-09-07 16:35:17 +0000805%type <TypeList> TypeListI ArgTypeListI
Chris Lattner2f7c9632001-06-06 20:29:01 +0000806%type <JumpTable> JumpTable
Chris Lattner379a8d22003-04-16 20:28:45 +0000807%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner8b1680e2003-09-08 18:20:29 +0000808%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattner379a8d22003-04-16 20:28:45 +0000809%type <Linkage> OptLinkage
Chris Lattner20126132003-04-22 19:07:06 +0000810%type <Endianness> BigOrLittle
Chris Lattner2f7c9632001-06-06 20:29:01 +0000811
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000812// ValueRef - Unresolved reference to a definition or BB
813%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattner252afba2001-07-26 16:29:15 +0000814%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner2f7c9632001-06-06 20:29:01 +0000815// Tokens and types for handling constant integer values
816//
817// ESINT64VAL - A negative number within long long range
818%token <SInt64Val> ESINT64VAL
819
820// EUINT64VAL - A positive number within uns. long long range
821%token <UInt64Val> EUINT64VAL
822%type <SInt64Val> EINT64VAL
823
824%token <SIntVal> SINTVAL // Signed 32 bit ints...
825%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
826%type <SIntVal> INTVAL
Chris Lattner212f70d2001-07-15 00:17:01 +0000827%token <FPVal> FPVAL // Float or Double constant
Chris Lattner2f7c9632001-06-06 20:29:01 +0000828
829// Built in types...
Chris Lattneraa534bf2001-09-07 16:35:17 +0000830%type <TypeVal> Types TypesV UpRTypes UpRTypesV
831%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattneraa534bf2001-09-07 16:35:17 +0000832%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
833%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner2f7c9632001-06-06 20:29:01 +0000834
Chris Lattner9eb28752003-08-22 05:42:10 +0000835%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
836%type <StrVal> Name OptName OptAssign
Chris Lattner2f7c9632001-06-06 20:29:01 +0000837
838
Chris Lattner79694012003-06-28 20:01:34 +0000839%token IMPLEMENTATION ZEROINITIALIZER TRUE FALSE BEGINTOK ENDTOK
Chris Lattner8b1680e2003-09-08 18:20:29 +0000840%token DECLARE GLOBAL CONSTANT VOLATILE
Chris Lattner17235712004-02-08 21:48:25 +0000841%token TO DOTDOTDOT NULL_TOK CONST INTERNAL LINKONCE WEAK APPENDING
Chris Lattner20126132003-04-22 19:07:06 +0000842%token OPAQUE NOT EXTERNAL TARGET ENDIAN POINTERSIZE LITTLE BIG
Chris Lattner2f7c9632001-06-06 20:29:01 +0000843
844// Basic Block Terminating Operators
Chris Lattner9c58cf62003-09-08 18:54:55 +0000845%token <TermOpVal> RET BR SWITCH INVOKE UNWIND
Chris Lattner2f7c9632001-06-06 20:29:01 +0000846
Chris Lattner2f7c9632001-06-06 20:29:01 +0000847// Binary Operators
848%type <BinaryOpVal> BinaryOps // all the binary operators
Chris Lattnerc0686fe2002-09-10 19:57:26 +0000849%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattnerc27b1d72001-10-20 09:32:59 +0000850%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattnerd8bebcd2001-07-08 21:10:27 +0000851%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner2f7c9632001-06-06 20:29:01 +0000852
853// Memory Instructions
Vikram S. Adve7064eaf2002-07-14 22:59:28 +0000854%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner2f7c9632001-06-06 20:29:01 +0000855
Chris Lattnerd8bebcd2001-07-08 21:10:27 +0000856// Other Operators
857%type <OtherOpVal> ShiftOps
Chris Lattnerb94550e2003-10-19 21:34:28 +0000858%token <OtherOpVal> PHI_TOK CALL CAST SHL SHR VAARG VANEXT
Chris Lattner0079e7d2003-10-18 05:53:13 +0000859%token VA_ARG // FIXME: OBSOLETE
Chris Lattnerd8bebcd2001-07-08 21:10:27 +0000860
Chris Lattner2f7c9632001-06-06 20:29:01 +0000861%start Module
862%%
863
864// Handle constant integer size restriction and conversion...
865//
Chris Lattner8cb1dfb2002-06-04 21:58:56 +0000866INTVAL : SINTVAL;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000867INTVAL : UINTVAL {
868 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
869 ThrowException("Value too large for type!");
870 $$ = (int32_t)$1;
Chris Lattner8cb1dfb2002-06-04 21:58:56 +0000871};
Chris Lattner2f7c9632001-06-06 20:29:01 +0000872
873
Chris Lattner8cb1dfb2002-06-04 21:58:56 +0000874EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000875EINT64VAL : EUINT64VAL {
876 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
877 ThrowException("Value too large for type!");
878 $$ = (int64_t)$1;
Chris Lattner8cb1dfb2002-06-04 21:58:56 +0000879};
Chris Lattner2f7c9632001-06-06 20:29:01 +0000880
Chris Lattner2f7c9632001-06-06 20:29:01 +0000881// Operations that are notably excluded from this list include:
882// RET, BR, & SWITCH because they end basic blocks and are treated specially.
883//
Chris Lattnerc0686fe2002-09-10 19:57:26 +0000884ArithmeticOps: ADD | SUB | MUL | DIV | REM;
885LogicalOps : AND | OR | XOR;
886SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
887BinaryOps : ArithmeticOps | LogicalOps | SetCondOps;
888
Chris Lattner8cb1dfb2002-06-04 21:58:56 +0000889ShiftOps : SHL | SHR;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000890
Chris Lattner56f73d42001-07-14 06:10:16 +0000891// These are some types that allow classification if we only want a particular
892// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner8cb1dfb2002-06-04 21:58:56 +0000893SIntType : LONG | INT | SHORT | SBYTE;
894UIntType : ULONG | UINT | USHORT | UBYTE;
895IntType : SIntType | UIntType;
896FPType : FLOAT | DOUBLE;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000897
Chris Lattner56f73d42001-07-14 06:10:16 +0000898// OptAssign - Value producing statements have an optional assignment component
Chris Lattner9eb28752003-08-22 05:42:10 +0000899OptAssign : Name '=' {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000900 $$ = $1;
901 }
902 | /*empty*/ {
903 $$ = 0;
Chris Lattner8cb1dfb2002-06-04 21:58:56 +0000904 };
Chris Lattner2f7c9632001-06-06 20:29:01 +0000905
Chris Lattner379a8d22003-04-16 20:28:45 +0000906OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
907 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
Chris Lattner068ad842003-10-16 18:29:00 +0000908 WEAK { $$ = GlobalValue::WeakLinkage; } |
Chris Lattner379a8d22003-04-16 20:28:45 +0000909 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
910 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattneraa534bf2001-09-07 16:35:17 +0000911
912//===----------------------------------------------------------------------===//
913// Types includes all predefined types... except void, because it can only be
Chris Lattner113f4f42002-06-25 16:13:24 +0000914// used in specific contexts (function returning void for example). To have
Chris Lattneraa534bf2001-09-07 16:35:17 +0000915// access to it, a user must explicitly use TypesV.
916//
917
918// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner8cb1dfb2002-06-04 21:58:56 +0000919TypesV : Types | VOID { $$ = new PATypeHolder($1); };
920UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattneraa534bf2001-09-07 16:35:17 +0000921
922Types : UpRTypes {
Chris Lattnerfd2a5142003-12-31 02:18:11 +0000923 if (!UpRefs.empty())
Chris Lattner30752bd92002-04-04 19:23:55 +0000924 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
925 $$ = $1;
Chris Lattner8cb1dfb2002-06-04 21:58:56 +0000926 };
Chris Lattneraa534bf2001-09-07 16:35:17 +0000927
928
929// Derived types are added later...
930//
Chris Lattner8cb1dfb2002-06-04 21:58:56 +0000931PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
932PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner30752bd92002-04-04 19:23:55 +0000933UpRTypes : OPAQUE {
934 $$ = new PATypeHolder(OpaqueType::get());
935 }
936 | PrimType {
937 $$ = new PATypeHolder($1);
Chris Lattner8cb1dfb2002-06-04 21:58:56 +0000938 };
Chris Lattner7f325cd2002-08-16 21:14:40 +0000939UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner30752bd92002-04-04 19:23:55 +0000940 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner8cb1dfb2002-06-04 21:58:56 +0000941};
Chris Lattneraa534bf2001-09-07 16:35:17 +0000942
Chris Lattneraa534bf2001-09-07 16:35:17 +0000943// Include derived types in the Types production.
944//
945UpRTypes : '\\' EUINT64VAL { // Type UpReference
Chris Lattnerfd2a5142003-12-31 02:18:11 +0000946 if ($2 > (uint64_t)~0U) ThrowException("Value out of range!");
Chris Lattneraa534bf2001-09-07 16:35:17 +0000947 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3cb41672004-02-09 03:19:29 +0000948 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Chris Lattner30752bd92002-04-04 19:23:55 +0000949 $$ = new PATypeHolder(OT);
Chris Lattneraa534bf2001-09-07 16:35:17 +0000950 UR_OUT("New Upreference!\n");
951 }
Chris Lattner57698e22002-03-26 18:01:55 +0000952 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner89da8a32003-04-22 18:42:41 +0000953 std::vector<const Type*> Params;
Chris Lattner7f74a562002-01-20 22:54:45 +0000954 mapto($3->begin(), $3->end(), std::back_inserter(Params),
Chris Lattner5c02edb2003-10-02 19:00:34 +0000955 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000956 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
957 if (isVarArg) Params.pop_back();
958
Chris Lattner30752bd92002-04-04 19:23:55 +0000959 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattneraa534bf2001-09-07 16:35:17 +0000960 delete $3; // Delete the argument list
Chris Lattnerfd2a5142003-12-31 02:18:11 +0000961 delete $1; // Delete the return type handle
Chris Lattneraa534bf2001-09-07 16:35:17 +0000962 }
Chris Lattnere63ddd8df2001-10-03 03:19:33 +0000963 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner30752bd92002-04-04 19:23:55 +0000964 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnere63ddd8df2001-10-03 03:19:33 +0000965 delete $4;
Chris Lattneraa534bf2001-09-07 16:35:17 +0000966 }
Chris Lattnere63ddd8df2001-10-03 03:19:33 +0000967 | '{' TypeListI '}' { // Structure type?
Chris Lattner89da8a32003-04-22 18:42:41 +0000968 std::vector<const Type*> Elements;
Chris Lattner7f74a562002-01-20 22:54:45 +0000969 mapto($2->begin(), $2->end(), std::back_inserter(Elements),
Chris Lattner5c02edb2003-10-02 19:00:34 +0000970 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattneraa534bf2001-09-07 16:35:17 +0000971
Chris Lattner30752bd92002-04-04 19:23:55 +0000972 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnere63ddd8df2001-10-03 03:19:33 +0000973 delete $2;
974 }
975 | '{' '}' { // Empty structure type?
Chris Lattner89da8a32003-04-22 18:42:41 +0000976 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Chris Lattnere63ddd8df2001-10-03 03:19:33 +0000977 }
978 | UpRTypes '*' { // Pointer type?
Chris Lattner30752bd92002-04-04 19:23:55 +0000979 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnere63ddd8df2001-10-03 03:19:33 +0000980 delete $1;
Chris Lattner8cb1dfb2002-06-04 21:58:56 +0000981 };
Chris Lattneraa534bf2001-09-07 16:35:17 +0000982
Chris Lattner113f4f42002-06-25 16:13:24 +0000983// TypeList - Used for struct declarations and as a basis for function type
Chris Lattneraa534bf2001-09-07 16:35:17 +0000984// declaration type lists
985//
986TypeListI : UpRTypes {
Chris Lattner89da8a32003-04-22 18:42:41 +0000987 $$ = new std::list<PATypeHolder>();
Chris Lattneraa534bf2001-09-07 16:35:17 +0000988 $$->push_back(*$1); delete $1;
989 }
990 | TypeListI ',' UpRTypes {
991 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner8cb1dfb2002-06-04 21:58:56 +0000992 };
Chris Lattneraa534bf2001-09-07 16:35:17 +0000993
Chris Lattner113f4f42002-06-25 16:13:24 +0000994// ArgTypeList - List of types for a function type declaration...
Chris Lattneraa534bf2001-09-07 16:35:17 +0000995ArgTypeListI : TypeListI
996 | TypeListI ',' DOTDOTDOT {
997 ($$=$1)->push_back(Type::VoidTy);
998 }
999 | DOTDOTDOT {
Chris Lattner89da8a32003-04-22 18:42:41 +00001000 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattneraa534bf2001-09-07 16:35:17 +00001001 }
1002 | /*empty*/ {
Chris Lattner89da8a32003-04-22 18:42:41 +00001003 $$ = new std::list<PATypeHolder>();
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001004 };
Chris Lattneraa534bf2001-09-07 16:35:17 +00001005
Chris Lattner56f73d42001-07-14 06:10:16 +00001006// ConstVal - The various declarations that go into the constant pool. This
Chris Lattner7f325cd2002-08-16 21:14:40 +00001007// production is used ONLY to represent constants that show up AFTER a 'const',
1008// 'constant' or 'global' token at global scope. Constants that can be inlined
1009// into other expressions (such as integers and constexprs) are handled by the
1010// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattner56f73d42001-07-14 06:10:16 +00001011//
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001012ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Chris Lattnerf26a8ee2003-07-23 15:30:06 +00001013 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001014 if (ATy == 0)
1015 ThrowException("Cannot make array constant with type: '" +
1016 (*$1)->getDescription() + "'!");
Chris Lattneraa534bf2001-09-07 16:35:17 +00001017 const Type *ETy = ATy->getElementType();
1018 int NumElements = ATy->getNumElements();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001019
Chris Lattneraa534bf2001-09-07 16:35:17 +00001020 // Verify that we have the correct size...
1021 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner2f7c9632001-06-06 20:29:01 +00001022 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattneraa534bf2001-09-07 16:35:17 +00001023 utostr($3->size()) + " arguments, but has size of " +
1024 itostr(NumElements) + "!");
Chris Lattner2f7c9632001-06-06 20:29:01 +00001025
Chris Lattneraa534bf2001-09-07 16:35:17 +00001026 // Verify all elements are correct type!
1027 for (unsigned i = 0; i < $3->size(); i++) {
1028 if (ETy != (*$3)[i]->getType())
Chris Lattner2f7c9632001-06-06 20:29:01 +00001029 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattneref99d3c2001-12-14 16:28:42 +00001030 ETy->getDescription() +"' as required!\nIt is of type '"+
1031 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner2f7c9632001-06-06 20:29:01 +00001032 }
1033
Chris Lattner3462ae32001-12-03 22:26:30 +00001034 $$ = ConstantArray::get(ATy, *$3);
Chris Lattneraa534bf2001-09-07 16:35:17 +00001035 delete $1; delete $3;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001036 }
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001037 | Types '[' ']' {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +00001038 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001039 if (ATy == 0)
1040 ThrowException("Cannot make array constant with type: '" +
1041 (*$1)->getDescription() + "'!");
1042
1043 int NumElements = ATy->getNumElements();
Chris Lattneraa534bf2001-09-07 16:35:17 +00001044 if (NumElements != -1 && NumElements != 0)
Chris Lattner2f7c9632001-06-06 20:29:01 +00001045 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattneraa534bf2001-09-07 16:35:17 +00001046 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattner89da8a32003-04-22 18:42:41 +00001047 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
Chris Lattneraa534bf2001-09-07 16:35:17 +00001048 delete $1;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001049 }
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001050 | Types 'c' STRINGCONSTANT {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +00001051 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001052 if (ATy == 0)
1053 ThrowException("Cannot make array constant with type: '" +
1054 (*$1)->getDescription() + "'!");
1055
Chris Lattneraa534bf2001-09-07 16:35:17 +00001056 int NumElements = ATy->getNumElements();
1057 const Type *ETy = ATy->getElementType();
1058 char *EndStr = UnEscapeLexed($3, true);
1059 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner26e50dc2001-07-28 17:48:55 +00001060 ThrowException("Can't build string constant of size " +
Chris Lattneraa534bf2001-09-07 16:35:17 +00001061 itostr((int)(EndStr-$3)) +
1062 " when array has size " + itostr(NumElements) + "!");
Chris Lattner89da8a32003-04-22 18:42:41 +00001063 std::vector<Constant*> Vals;
Chris Lattneraa534bf2001-09-07 16:35:17 +00001064 if (ETy == Type::SByteTy) {
1065 for (char *C = $3; C != EndStr; ++C)
Chris Lattner3462ae32001-12-03 22:26:30 +00001066 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattneraa534bf2001-09-07 16:35:17 +00001067 } else if (ETy == Type::UByteTy) {
1068 for (char *C = $3; C != EndStr; ++C)
Chris Lattner63a25242003-01-30 22:24:26 +00001069 Vals.push_back(ConstantUInt::get(ETy, (unsigned char)*C));
Chris Lattner26e50dc2001-07-28 17:48:55 +00001070 } else {
Chris Lattneraa534bf2001-09-07 16:35:17 +00001071 free($3);
Chris Lattner26e50dc2001-07-28 17:48:55 +00001072 ThrowException("Cannot build string arrays of non byte sized elements!");
1073 }
Chris Lattneraa534bf2001-09-07 16:35:17 +00001074 free($3);
Chris Lattner3462ae32001-12-03 22:26:30 +00001075 $$ = ConstantArray::get(ATy, Vals);
Chris Lattneraa534bf2001-09-07 16:35:17 +00001076 delete $1;
Chris Lattner26e50dc2001-07-28 17:48:55 +00001077 }
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001078 | Types '{' ConstVector '}' {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +00001079 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001080 if (STy == 0)
1081 ThrowException("Cannot make struct constant with type: '" +
1082 (*$1)->getDescription() + "'!");
Chris Lattner4b1d1062003-04-15 16:09:31 +00001083
Chris Lattner5bb23152003-05-21 16:06:56 +00001084 if ($3->size() != STy->getNumContainedTypes())
1085 ThrowException("Illegal number of initializers for structure type!");
1086
Chris Lattner4b1d1062003-04-15 16:09:31 +00001087 // Check to ensure that constants are compatible with the type initializer!
1088 for (unsigned i = 0, e = $3->size(); i != e; ++i)
1089 if ((*$3)[i]->getType() != STy->getElementTypes()[i])
1090 ThrowException("Expected type '" +
1091 STy->getElementTypes()[i]->getDescription() +
1092 "' for element #" + utostr(i) +
1093 " of structure initializer!");
1094
Chris Lattner3462ae32001-12-03 22:26:30 +00001095 $$ = ConstantStruct::get(STy, *$3);
Chris Lattneraa534bf2001-09-07 16:35:17 +00001096 delete $1; delete $3;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001097 }
Chris Lattner5bb23152003-05-21 16:06:56 +00001098 | Types '{' '}' {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +00001099 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattner5bb23152003-05-21 16:06:56 +00001100 if (STy == 0)
1101 ThrowException("Cannot make struct constant with type: '" +
1102 (*$1)->getDescription() + "'!");
1103
1104 if (STy->getNumContainedTypes() != 0)
1105 ThrowException("Illegal number of initializers for structure type!");
1106
1107 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1108 delete $1;
1109 }
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001110 | Types NULL_TOK {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +00001111 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnere63ddd8df2001-10-03 03:19:33 +00001112 if (PTy == 0)
1113 ThrowException("Cannot make null pointer constant with type: '" +
1114 (*$1)->getDescription() + "'!");
1115
Chris Lattner3462ae32001-12-03 22:26:30 +00001116 $$ = ConstantPointerNull::get(PTy);
Chris Lattner571a6b02001-10-03 01:49:25 +00001117 delete $1;
1118 }
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001119 | Types SymbolicValueRef {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +00001120 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattner60e0dd72001-10-03 06:12:09 +00001121 if (Ty == 0)
1122 ThrowException("Global const reference must be a pointer type!");
1123
Chris Lattner61643a02002-08-15 17:58:33 +00001124 // ConstExprs can exist in the body of a function, thus creating
1125 // ConstantPointerRefs whenever they refer to a variable. Because we are in
1126 // the context of a function, getValNonImprovising will search the functions
1127 // symbol table instead of the module symbol table for the global symbol,
1128 // which throws things all off. To get around this, we just tell
1129 // getValNonImprovising that we are at global scope here.
1130 //
Chris Lattner61877742003-10-16 20:12:13 +00001131 Function *SavedCurFn = CurFun.CurrentFunction;
1132 CurFun.CurrentFunction = 0;
Chris Lattner61643a02002-08-15 17:58:33 +00001133
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001134 Value *V = getValNonImprovising(Ty, $2);
Chris Lattner60e0dd72001-10-03 06:12:09 +00001135
Chris Lattner61877742003-10-16 20:12:13 +00001136 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner61643a02002-08-15 17:58:33 +00001137
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001138 // If this is an initializer for a constant pointer, which is referencing a
1139 // (currently) undefined variable, create a stub now that shall be replaced
1140 // in the future with the right type of variable.
1141 //
1142 if (V == 0) {
1143 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1144 const PointerType *PT = cast<PointerType>(Ty);
1145
1146 // First check to see if the forward references value is already created!
1147 PerModuleInfo::GlobalRefsType::iterator I =
Chris Lattner89da8a32003-04-22 18:42:41 +00001148 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001149
1150 if (I != CurModule.GlobalRefs.end()) {
1151 V = I->second; // Placeholder already exists, use it...
Chris Lattnere9703012003-12-23 20:05:15 +00001152 $2.destroy();
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001153 } else {
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001154 // Create a placeholder for the global variable reference...
Chris Lattner2413b162001-12-04 00:03:30 +00001155 GlobalVariable *GV = new GlobalVariable(PT->getElementType(),
Chris Lattner379a8d22003-04-16 20:28:45 +00001156 false,
1157 GlobalValue::ExternalLinkage);
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001158 // Keep track of the fact that we have a forward ref to recycle it
Chris Lattner89da8a32003-04-22 18:42:41 +00001159 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001160
1161 // Must temporarily push this value into the module table...
1162 CurModule.CurrentModule->getGlobalList().push_back(GV);
1163 V = GV;
1164 }
Chris Lattner60e0dd72001-10-03 06:12:09 +00001165 }
1166
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001167 GlobalValue *GV = cast<GlobalValue>(V);
Chris Lattner3462ae32001-12-03 22:26:30 +00001168 $$ = ConstantPointerRef::get(GV);
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001169 delete $1; // Free the type handle
Vikram S. Adve7064eaf2002-07-14 22:59:28 +00001170 }
Chris Lattner7f325cd2002-08-16 21:14:40 +00001171 | Types ConstExpr {
1172 if ($1->get() != $2->getType())
1173 ThrowException("Mismatched types for constant expression!");
1174 $$ = $2;
1175 delete $1;
Chris Lattner79694012003-06-28 20:01:34 +00001176 }
1177 | Types ZEROINITIALIZER {
1178 $$ = Constant::getNullValue($1->get());
1179 delete $1;
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001180 };
Chris Lattner60e0dd72001-10-03 06:12:09 +00001181
Chris Lattnerfa01a522002-10-09 00:25:32 +00001182ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattner7f1c98d2002-08-15 18:17:28 +00001183 if (!ConstantSInt::isValueValidForType($1, $2))
1184 ThrowException("Constant value doesn't fit in type!");
1185 $$ = ConstantSInt::get($1, $2);
Chris Lattnerfa01a522002-10-09 00:25:32 +00001186 }
1187 | UIntType EUINT64VAL { // integral constants
Chris Lattner7f1c98d2002-08-15 18:17:28 +00001188 if (!ConstantUInt::isValueValidForType($1, $2))
1189 ThrowException("Constant value doesn't fit in type!");
1190 $$ = ConstantUInt::get($1, $2);
Chris Lattnerfa01a522002-10-09 00:25:32 +00001191 }
1192 | BOOL TRUE { // Boolean constants
Chris Lattner7f1c98d2002-08-15 18:17:28 +00001193 $$ = ConstantBool::True;
1194 }
Chris Lattnerfa01a522002-10-09 00:25:32 +00001195 | BOOL FALSE { // Boolean constants
Chris Lattner7f1c98d2002-08-15 18:17:28 +00001196 $$ = ConstantBool::False;
1197 }
1198 | FPType FPVAL { // Float & Double constants
1199 $$ = ConstantFP::get($1, $2);
1200 };
1201
Chris Lattner2f7c9632001-06-06 20:29:01 +00001202
Chris Lattner7f325cd2002-08-16 21:14:40 +00001203ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattner199bb852003-11-21 20:27:35 +00001204 if (!$3->getType()->isFirstClassType())
1205 ThrowException("cast constant expression from a non-primitive type: '" +
1206 $3->getType()->getDescription() + "'!");
Chris Lattner6f8cd632003-10-10 03:56:01 +00001207 if (!$5->get()->isFirstClassType())
1208 ThrowException("cast constant expression to a non-primitive type: '" +
1209 $5->get()->getDescription() + "'!");
Chris Lattnerd8ecff72002-08-15 19:37:11 +00001210 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerd8ecff72002-08-15 19:37:11 +00001211 delete $5;
Vikram S. Adve7064eaf2002-07-14 22:59:28 +00001212 }
Chris Lattner7f325cd2002-08-16 21:14:40 +00001213 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1214 if (!isa<PointerType>($3->getType()))
Chris Lattner3cd8c562002-07-30 18:54:25 +00001215 ThrowException("GetElementPtr requires a pointer operand!");
1216
1217 const Type *IdxTy =
Chris Lattner7f325cd2002-08-16 21:14:40 +00001218 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattner3cd8c562002-07-30 18:54:25 +00001219 if (!IdxTy)
1220 ThrowException("Index list invalid for constant getelementptr!");
Chris Lattner3cd8c562002-07-30 18:54:25 +00001221
Chris Lattner89da8a32003-04-22 18:42:41 +00001222 std::vector<Constant*> IdxVec;
Chris Lattner7f325cd2002-08-16 21:14:40 +00001223 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1224 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattner980ddf52002-07-18 00:14:27 +00001225 IdxVec.push_back(C);
1226 else
Chris Lattner3cd8c562002-07-30 18:54:25 +00001227 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattner980ddf52002-07-18 00:14:27 +00001228
Chris Lattner7f325cd2002-08-16 21:14:40 +00001229 delete $4;
Chris Lattner980ddf52002-07-18 00:14:27 +00001230
Chris Lattner7f325cd2002-08-16 21:14:40 +00001231 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Vikram S. Adve7064eaf2002-07-14 22:59:28 +00001232 }
Chris Lattner7f325cd2002-08-16 21:14:40 +00001233 | BinaryOps '(' ConstVal ',' ConstVal ')' {
Chris Lattner3cd8c562002-07-30 18:54:25 +00001234 if ($3->getType() != $5->getType())
1235 ThrowException("Binary operator types must match!");
Chris Lattner7f325cd2002-08-16 21:14:40 +00001236 $$ = ConstantExpr::get($1, $3, $5);
Vikram S. Adve7064eaf2002-07-14 22:59:28 +00001237 }
Chris Lattner7f325cd2002-08-16 21:14:40 +00001238 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattner3cd8c562002-07-30 18:54:25 +00001239 if ($5->getType() != Type::UByteTy)
1240 ThrowException("Shift count for shift constant must be unsigned byte!");
Chris Lattner0aa8eaf2003-10-17 05:11:44 +00001241 if (!$3->getType()->isInteger())
1242 ThrowException("Shift constant expression requires integer operand!");
Chris Lattner1b7d4d72004-01-12 19:08:43 +00001243 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattnerdb3b2022002-08-14 17:12:33 +00001244 };
Vikram S. Adve7064eaf2002-07-14 22:59:28 +00001245
1246
Misha Brukman7fdaab42003-07-14 17:20:40 +00001247// ConstVector - A list of comma separated constants.
Chris Lattner2f7c9632001-06-06 20:29:01 +00001248ConstVector : ConstVector ',' ConstVal {
Chris Lattneraa534bf2001-09-07 16:35:17 +00001249 ($$ = $1)->push_back($3);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001250 }
1251 | ConstVal {
Chris Lattner89da8a32003-04-22 18:42:41 +00001252 $$ = new std::vector<Constant*>();
Chris Lattneraa534bf2001-09-07 16:35:17 +00001253 $$->push_back($1);
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001254 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00001255
Chris Lattner252afba2001-07-26 16:29:15 +00001256
Chris Lattner7fb14f52001-09-18 04:00:54 +00001257// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001258GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner7fb14f52001-09-18 04:00:54 +00001259
Chris Lattner2f7c9632001-06-06 20:29:01 +00001260
Chris Lattner7b804d62002-05-02 19:11:13 +00001261//===----------------------------------------------------------------------===//
1262// Rules to match Modules
1263//===----------------------------------------------------------------------===//
1264
1265// Module rule: Capture the result of parsing the whole file into a result
1266// variable...
1267//
1268Module : FunctionList {
1269 $$ = ParserResult = $1;
1270 CurModule.ModuleDone();
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001271};
Chris Lattner7b804d62002-05-02 19:11:13 +00001272
Chris Lattner113f4f42002-06-25 16:13:24 +00001273// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner7b804d62002-05-02 19:11:13 +00001274//
1275FunctionList : FunctionList Function {
1276 $$ = $1;
1277 assert($2->getParent() == 0 && "Function already in module!");
1278 $1->getFunctionList().push_back($2);
Chris Lattner61877742003-10-16 20:12:13 +00001279 CurFun.FunctionDone();
Chris Lattner7b804d62002-05-02 19:11:13 +00001280 }
1281 | FunctionList FunctionProto {
1282 $$ = $1;
1283 }
1284 | FunctionList IMPLEMENTATION {
1285 $$ = $1;
1286 }
1287 | ConstPool {
1288 $$ = CurModule.CurrentModule;
1289 // Resolve circular types before we parse the body of the module
1290 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001291 };
Chris Lattner7b804d62002-05-02 19:11:13 +00001292
Chris Lattner56f73d42001-07-14 06:10:16 +00001293// ConstPool - Constants with optional names assigned to them.
Chris Lattner571a6b02001-10-03 01:49:25 +00001294ConstPool : ConstPool OptAssign CONST ConstVal {
Chris Lattner8ce4e792003-03-03 23:28:55 +00001295 if (!setValueName($4, $2))
1296 InsertValue($4);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001297 }
Chris Lattneraa534bf2001-09-07 16:35:17 +00001298 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattnerd50fa5a2001-10-22 05:56:09 +00001299 // Eagerly resolve types. This is not an optimization, this is a
1300 // requirement that is due to the fact that we could have this:
1301 //
1302 // %list = type { %list * }
1303 // %list = type { %list * } ; repeated type decl
1304 //
1305 // If types are not resolved eagerly, then the two types will not be
1306 // determined to be the same type!
1307 //
1308 ResolveTypeTo($2, $4->get());
1309
Chris Lattner7fb14f52001-09-18 04:00:54 +00001310 // TODO: FIXME when Type are not const
Chris Lattnerfb3e5492001-10-03 15:39:04 +00001311 if (!setValueName(const_cast<Type*>($4->get()), $2)) {
1312 // If this is not a redefinition of a type...
1313 if (!$2) {
1314 InsertType($4->get(),
Chris Lattner61877742003-10-16 20:12:13 +00001315 inFunctionScope() ? CurFun.Types : CurModule.Types);
Chris Lattnerfb3e5492001-10-03 15:39:04 +00001316 }
Chris Lattneraa534bf2001-09-07 16:35:17 +00001317 }
Chris Lattner6265d792001-10-21 23:02:41 +00001318
1319 delete $4;
Chris Lattneraa534bf2001-09-07 16:35:17 +00001320 }
Chris Lattner57698e22002-03-26 18:01:55 +00001321 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner26e50dc2001-07-28 17:48:55 +00001322 }
Chris Lattner379a8d22003-04-16 20:28:45 +00001323 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Chris Lattner841d8b92001-11-26 18:54:16 +00001324 const Type *Ty = $5->getType();
Chris Lattner7fb14f52001-09-18 04:00:54 +00001325 // Global declarations appear in Constant Pool
Chris Lattner3462ae32001-12-03 22:26:30 +00001326 Constant *Initializer = $5;
Chris Lattner7fb14f52001-09-18 04:00:54 +00001327 if (Initializer == 0)
1328 ThrowException("Global value initializer is not a constant!");
Vikram S. Adve7064eaf2002-07-14 22:59:28 +00001329
Chris Lattner841d8b92001-11-26 18:54:16 +00001330 GlobalVariable *GV = new GlobalVariable(Ty, $4, $3, Initializer);
Chris Lattnerfb3e5492001-10-03 15:39:04 +00001331 if (!setValueName(GV, $2)) { // If not redefining...
1332 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001333 int Slot = InsertValue(GV, CurModule.Values);
1334
1335 if (Slot != -1) {
1336 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1337 } else {
1338 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1339 (char*)GV->getName().c_str()));
1340 }
Chris Lattnerfb3e5492001-10-03 15:39:04 +00001341 }
Chris Lattner7fb14f52001-09-18 04:00:54 +00001342 }
Chris Lattnerfcfb1762003-04-16 18:13:57 +00001343 | ConstPool OptAssign EXTERNAL GlobalType Types {
1344 const Type *Ty = *$5;
Chris Lattner7fb14f52001-09-18 04:00:54 +00001345 // Global declarations appear in Constant Pool
Chris Lattner379a8d22003-04-16 20:28:45 +00001346 GlobalVariable *GV = new GlobalVariable(Ty,$4,GlobalValue::ExternalLinkage);
Chris Lattnerfb3e5492001-10-03 15:39:04 +00001347 if (!setValueName(GV, $2)) { // If not redefining...
1348 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001349 int Slot = InsertValue(GV, CurModule.Values);
1350
1351 if (Slot != -1) {
1352 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1353 } else {
1354 assert(GV->hasName() && "Not named and not numbered!?");
1355 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1356 (char*)GV->getName().c_str()));
1357 }
Chris Lattnerfb3e5492001-10-03 15:39:04 +00001358 }
Chris Lattnerfcfb1762003-04-16 18:13:57 +00001359 delete $5;
Chris Lattner56f73d42001-07-14 06:10:16 +00001360 }
Chris Lattner20126132003-04-22 19:07:06 +00001361 | ConstPool TARGET TargetDefinition {
1362 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001363 | /* empty: end of list */ {
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001364 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00001365
1366
Chris Lattner20126132003-04-22 19:07:06 +00001367
1368BigOrLittle : BIG { $$ = Module::BigEndian; };
1369BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1370
1371TargetDefinition : ENDIAN '=' BigOrLittle {
1372 CurModule.CurrentModule->setEndianness($3);
1373 }
1374 | POINTERSIZE '=' EUINT64VAL {
1375 if ($3 == 32)
1376 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1377 else if ($3 == 64)
1378 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1379 else
1380 ThrowException("Invalid pointer size: '" + utostr($3) + "'!");
1381 };
1382
1383
Chris Lattner2f7c9632001-06-06 20:29:01 +00001384//===----------------------------------------------------------------------===//
Chris Lattner57698e22002-03-26 18:01:55 +00001385// Rules to match Function Headers
Chris Lattner2f7c9632001-06-06 20:29:01 +00001386//===----------------------------------------------------------------------===//
1387
Chris Lattner9eb28752003-08-22 05:42:10 +00001388Name : VAR_ID | STRINGCONSTANT;
1389OptName : Name | /*empty*/ { $$ = 0; };
Chris Lattner2f7c9632001-06-06 20:29:01 +00001390
Chris Lattner9eb28752003-08-22 05:42:10 +00001391ArgVal : Types OptName {
Chris Lattner149376d2002-10-13 20:57:00 +00001392 if (*$1 == Type::VoidTy)
1393 ThrowException("void typed arguments are invalid!");
Chris Lattner89da8a32003-04-22 18:42:41 +00001394 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001395};
Chris Lattner2f7c9632001-06-06 20:29:01 +00001396
Chris Lattner149376d2002-10-13 20:57:00 +00001397ArgListH : ArgListH ',' ArgVal {
1398 $$ = $1;
1399 $1->push_back(*$3);
1400 delete $3;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001401 }
1402 | ArgVal {
Chris Lattner89da8a32003-04-22 18:42:41 +00001403 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
Chris Lattner149376d2002-10-13 20:57:00 +00001404 $$->push_back(*$1);
Chris Lattner29f67092002-03-08 18:41:32 +00001405 delete $1;
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001406 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00001407
1408ArgList : ArgListH {
1409 $$ = $1;
1410 }
Chris Lattner149376d2002-10-13 20:57:00 +00001411 | ArgListH ',' DOTDOTDOT {
1412 $$ = $1;
Chris Lattner89da8a32003-04-22 18:42:41 +00001413 $$->push_back(std::pair<PATypeHolder*,
1414 char*>(new PATypeHolder(Type::VoidTy), 0));
Chris Lattner149376d2002-10-13 20:57:00 +00001415 }
1416 | DOTDOTDOT {
Chris Lattner89da8a32003-04-22 18:42:41 +00001417 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1418 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
Chris Lattner149376d2002-10-13 20:57:00 +00001419 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001420 | /* empty */ {
1421 $$ = 0;
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001422 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00001423
Chris Lattner9eb28752003-08-22 05:42:10 +00001424FunctionHeaderH : TypesV Name '(' ArgList ')' {
Chris Lattnerfcfb1762003-04-16 18:13:57 +00001425 UnEscapeLexed($2);
Chris Lattner89da8a32003-04-22 18:42:41 +00001426 std::string FunctionName($2);
Chris Lattner841d8b92001-11-26 18:54:16 +00001427
Chris Lattner3ae303c2003-11-21 22:32:23 +00001428 if (!(*$1)->isFirstClassType() && *$1 != Type::VoidTy)
1429 ThrowException("LLVM functions cannot return aggregate types!");
1430
Chris Lattner89da8a32003-04-22 18:42:41 +00001431 std::vector<const Type*> ParamTypeList;
Chris Lattnerfcfb1762003-04-16 18:13:57 +00001432 if ($4) { // If there are arguments...
Chris Lattner89da8a32003-04-22 18:42:41 +00001433 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $4->begin();
Chris Lattnerfcfb1762003-04-16 18:13:57 +00001434 I != $4->end(); ++I)
Chris Lattner149376d2002-10-13 20:57:00 +00001435 ParamTypeList.push_back(I->first->get());
Chris Lattner19613292002-10-15 21:41:14 +00001436 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001437
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001438 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1439 if (isVarArg) ParamTypeList.pop_back();
1440
Chris Lattnerfcfb1762003-04-16 18:13:57 +00001441 const FunctionType *FT = FunctionType::get(*$1, ParamTypeList, isVarArg);
Chris Lattner19613292002-10-15 21:41:14 +00001442 const PointerType *PFT = PointerType::get(FT);
Chris Lattnerfcfb1762003-04-16 18:13:57 +00001443 delete $1;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001444
Chris Lattner19613292002-10-15 21:41:14 +00001445 Function *Fn = 0;
1446 // Is the function already in symtab?
1447 if ((Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1448 // Yes it is. If this is the case, either we need to be a forward decl,
1449 // or it needs to be.
Chris Lattner61877742003-10-16 20:12:13 +00001450 if (!CurFun.isDeclare && !Fn->isExternal())
Chris Lattner19613292002-10-15 21:41:14 +00001451 ThrowException("Redefinition of function '" + FunctionName + "'!");
1452
Chris Lattner19613292002-10-15 21:41:14 +00001453 // If we found a preexisting function prototype, remove it from the
1454 // module, so that we don't get spurious conflicts with global & local
1455 // variables.
1456 //
1457 CurModule.CurrentModule->getFunctionList().remove(Fn);
Chris Lattner108308a2002-03-08 19:11:42 +00001458
Chris Lattner19613292002-10-15 21:41:14 +00001459 // Make sure to strip off any argument names so we can't get conflicts...
1460 for (Function::aiterator AI = Fn->abegin(), AE = Fn->aend(); AI != AE; ++AI)
1461 AI->setName("");
Chris Lattner8445b412002-07-15 00:10:33 +00001462
Chris Lattner19613292002-10-15 21:41:14 +00001463 } else { // Not already defined?
Chris Lattner379a8d22003-04-16 20:28:45 +00001464 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName);
Chris Lattner19613292002-10-15 21:41:14 +00001465 InsertValue(Fn, CurModule.Values);
Chris Lattnerfcfb1762003-04-16 18:13:57 +00001466 CurModule.DeclareNewGlobalValue(Fn, ValID::create($2));
Chris Lattner17f729e2001-07-15 06:35:53 +00001467 }
Chris Lattnerfcfb1762003-04-16 18:13:57 +00001468 free($2); // Free strdup'd memory!
Chris Lattner2f7c9632001-06-06 20:29:01 +00001469
Chris Lattner61877742003-10-16 20:12:13 +00001470 CurFun.FunctionStart(Fn);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001471
Chris Lattner113f4f42002-06-25 16:13:24 +00001472 // Add all of the arguments we parsed to the function...
Chris Lattnerfcfb1762003-04-16 18:13:57 +00001473 if ($4) { // Is null if empty...
Chris Lattner149376d2002-10-13 20:57:00 +00001474 if (isVarArg) { // Nuke the last entry
Chris Lattnerfcfb1762003-04-16 18:13:57 +00001475 assert($4->back().first->get() == Type::VoidTy && $4->back().second == 0&&
Chris Lattner149376d2002-10-13 20:57:00 +00001476 "Not a varargs marker!");
Chris Lattnerfcfb1762003-04-16 18:13:57 +00001477 delete $4->back().first;
1478 $4->pop_back(); // Delete the last entry
Chris Lattner149376d2002-10-13 20:57:00 +00001479 }
Chris Lattner19613292002-10-15 21:41:14 +00001480 Function::aiterator ArgIt = Fn->abegin();
Chris Lattner89da8a32003-04-22 18:42:41 +00001481 for (std::vector<std::pair<PATypeHolder*, char*> >::iterator I =$4->begin();
Chris Lattnerfcfb1762003-04-16 18:13:57 +00001482 I != $4->end(); ++I, ++ArgIt) {
Chris Lattner149376d2002-10-13 20:57:00 +00001483 delete I->first; // Delete the typeholder...
1484
1485 if (setValueName(ArgIt, I->second)) // Insert arg into symtab...
Chris Lattner29f67092002-03-08 18:41:32 +00001486 assert(0 && "No arg redef allowed!");
Chris Lattner29f67092002-03-08 18:41:32 +00001487
Chris Lattner149376d2002-10-13 20:57:00 +00001488 InsertValue(ArgIt);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001489 }
Chris Lattner149376d2002-10-13 20:57:00 +00001490
Chris Lattnerfcfb1762003-04-16 18:13:57 +00001491 delete $4; // We're now done with the argument list
Chris Lattner2f7c9632001-06-06 20:29:01 +00001492 }
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001493};
Chris Lattner2f7c9632001-06-06 20:29:01 +00001494
Chris Lattner1e1a9b42002-05-03 18:23:48 +00001495BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1496
Chris Lattner379a8d22003-04-16 20:28:45 +00001497FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
Chris Lattner61877742003-10-16 20:12:13 +00001498 $$ = CurFun.CurrentFunction;
Chris Lattneraa534bf2001-09-07 16:35:17 +00001499
Chris Lattner379a8d22003-04-16 20:28:45 +00001500 // Make sure that we keep track of the linkage type even if there was a
1501 // previous "declare".
1502 $$->setLinkage($1);
Chris Lattnerfcfb1762003-04-16 18:13:57 +00001503
Chris Lattner113f4f42002-06-25 16:13:24 +00001504 // Resolve circular types before we parse the body of the function.
Chris Lattner61877742003-10-16 20:12:13 +00001505 ResolveTypes(CurFun.LateResolveTypes);
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001506};
Chris Lattner2f7c9632001-06-06 20:29:01 +00001507
Chris Lattner1e1a9b42002-05-03 18:23:48 +00001508END : ENDTOK | '}'; // Allow end of '}' to end a function
1509
Chris Lattner57698e22002-03-26 18:01:55 +00001510Function : BasicBlockList END {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001511 $$ = $1;
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001512};
Chris Lattner2f7c9632001-06-06 20:29:01 +00001513
Chris Lattner61877742003-10-16 20:12:13 +00001514FunctionProto : DECLARE { CurFun.isDeclare = true; } FunctionHeaderH {
1515 $$ = CurFun.CurrentFunction;
Chris Lattner57698e22002-03-26 18:01:55 +00001516 assert($$->getParent() == 0 && "Function already in module!");
1517 CurModule.CurrentModule->getFunctionList().push_back($$);
Chris Lattner61877742003-10-16 20:12:13 +00001518 CurFun.FunctionDone();
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001519};
Chris Lattner2f7c9632001-06-06 20:29:01 +00001520
1521//===----------------------------------------------------------------------===//
1522// Rules to match Basic Blocks
1523//===----------------------------------------------------------------------===//
1524
1525ConstValueRef : ESINT64VAL { // A reference to a direct constant
1526 $$ = ValID::create($1);
1527 }
1528 | EUINT64VAL {
1529 $$ = ValID::create($1);
1530 }
Chris Lattner212f70d2001-07-15 00:17:01 +00001531 | FPVAL { // Perhaps it's an FP constant?
1532 $$ = ValID::create($1);
1533 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001534 | TRUE {
Chris Lattner7f325cd2002-08-16 21:14:40 +00001535 $$ = ValID::create(ConstantBool::True);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001536 }
1537 | FALSE {
Chris Lattner7f325cd2002-08-16 21:14:40 +00001538 $$ = ValID::create(ConstantBool::False);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001539 }
Chris Lattnerfbdec252001-09-30 22:46:54 +00001540 | NULL_TOK {
1541 $$ = ValID::createNull();
Vikram S. Adve7064eaf2002-07-14 22:59:28 +00001542 }
Chris Lattner7f325cd2002-08-16 21:14:40 +00001543 | ConstExpr {
1544 $$ = ValID::create($1);
1545 };
Chris Lattnerfbdec252001-09-30 22:46:54 +00001546
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001547// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1548// another value.
1549//
1550SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner2f7c9632001-06-06 20:29:01 +00001551 $$ = ValID::create($1);
1552 }
Chris Lattner9eb28752003-08-22 05:42:10 +00001553 | Name { // Is it a named reference...?
Chris Lattner2f7c9632001-06-06 20:29:01 +00001554 $$ = ValID::create($1);
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001555 };
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001556
1557// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001558ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001559
Chris Lattner2f7c9632001-06-06 20:29:01 +00001560
Chris Lattner252afba2001-07-26 16:29:15 +00001561// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1562// type immediately preceeds the value reference, and allows complex constant
1563// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattner571a6b02001-10-03 01:49:25 +00001564ResolvedVal : Types ValueRef {
Chris Lattneraa534bf2001-09-07 16:35:17 +00001565 $$ = getVal(*$1, $2); delete $1;
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001566 };
Chris Lattner42b5a8a2001-07-25 22:47:46 +00001567
Chris Lattner2f7c9632001-06-06 20:29:01 +00001568BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner113f4f42002-06-25 16:13:24 +00001569 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001570 }
Chris Lattner113f4f42002-06-25 16:13:24 +00001571 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
1572 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001573 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00001574
1575
1576// Basic blocks are terminated by branching instructions:
1577// br, br/cc, switch, ret
1578//
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001579BasicBlock : InstructionList OptAssign BBTerminatorInst {
1580 if (setValueName($3, $2)) { assert(0 && "No redefn allowed!"); }
1581 InsertValue($3);
1582
1583 $1->getInstList().push_back($3);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001584 InsertValue($1);
1585 $$ = $1;
1586 }
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001587 | LABELSTR InstructionList OptAssign BBTerminatorInst {
1588 if (setValueName($4, $3)) { assert(0 && "No redefn allowed!"); }
1589 InsertValue($4);
1590
1591 $2->getInstList().push_back($4);
Chris Lattnerfb3e5492001-10-03 15:39:04 +00001592 if (setValueName($2, $1)) { assert(0 && "No label redef allowed!"); }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001593
1594 InsertValue($2);
1595 $$ = $2;
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001596 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00001597
1598InstructionList : InstructionList Inst {
1599 $1->getInstList().push_back($2);
1600 $$ = $1;
1601 }
1602 | /* empty */ {
Chris Lattner0079e7d2003-10-18 05:53:13 +00001603 $$ = CurBB = new BasicBlock();
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001604 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00001605
Chris Lattner252afba2001-07-26 16:29:15 +00001606BBTerminatorInst : RET ResolvedVal { // Return with a result...
1607 $$ = new ReturnInst($2);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001608 }
1609 | RET VOID { // Return with no result...
1610 $$ = new ReturnInst();
1611 }
1612 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner4b717c02001-10-01 16:18:37 +00001613 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
Chris Lattner2f7c9632001-06-06 20:29:01 +00001614 } // Conditional Branch...
1615 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner4b717c02001-10-01 16:18:37 +00001616 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1617 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
Chris Lattner2f7c9632001-06-06 20:29:01 +00001618 getVal(Type::BoolTy, $3));
1619 }
1620 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1621 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner4b717c02001-10-01 16:18:37 +00001622 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
Chris Lattner2f7c9632001-06-06 20:29:01 +00001623 $$ = S;
1624
Chris Lattner89da8a32003-04-22 18:42:41 +00001625 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattnerae234e12002-04-09 19:41:42 +00001626 E = $8->end();
1627 for (; I != E; ++I)
Chris Lattner6a2b8592003-08-23 23:14:52 +00001628 S->addCase(I->first, I->second);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001629 }
Chris Lattner9f3648b2003-05-15 21:30:00 +00001630 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
1631 SwitchInst *S = new SwitchInst(getVal($2, $3),
1632 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
1633 $$ = S;
1634 }
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001635 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO ResolvedVal
Chris Lattner17235712004-02-08 21:48:25 +00001636 UNWIND ResolvedVal {
Chris Lattner19613292002-10-15 21:41:14 +00001637 const PointerType *PFTy;
Chris Lattner57698e22002-03-26 18:01:55 +00001638 const FunctionType *Ty;
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001639
Chris Lattner19613292002-10-15 21:41:14 +00001640 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1641 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001642 // Pull out the types of all of the arguments...
Chris Lattner89da8a32003-04-22 18:42:41 +00001643 std::vector<const Type*> ParamTypes;
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001644 if ($5) {
Chris Lattner89da8a32003-04-22 18:42:41 +00001645 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1646 I != E; ++I)
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001647 ParamTypes.push_back((*I)->getType());
1648 }
1649
1650 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1651 if (isVarArg) ParamTypes.pop_back();
1652
Chris Lattner57698e22002-03-26 18:01:55 +00001653 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattner19613292002-10-15 21:41:14 +00001654 PFTy = PointerType::get(Ty);
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001655 }
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001656
Chris Lattner19613292002-10-15 21:41:14 +00001657 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001658
1659 BasicBlock *Normal = dyn_cast<BasicBlock>($8);
1660 BasicBlock *Except = dyn_cast<BasicBlock>($10);
1661
1662 if (Normal == 0 || Except == 0)
1663 ThrowException("Invoke instruction without label destinations!");
1664
1665 // Create the call node...
1666 if (!$5) { // Has no arguments?
Chris Lattner89da8a32003-04-22 18:42:41 +00001667 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001668 } else { // Has arguments?
Chris Lattner57698e22002-03-26 18:01:55 +00001669 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001670 // correctly!
1671 //
Chris Lattner57698e22002-03-26 18:01:55 +00001672 FunctionType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1673 FunctionType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner89da8a32003-04-22 18:42:41 +00001674 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001675
1676 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1677 if ((*ArgI)->getType() != *I)
1678 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattneref99d3c2001-12-14 16:28:42 +00001679 (*I)->getDescription() + "'!");
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001680
1681 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1682 ThrowException("Invalid number of parameters detected!");
1683
Chris Lattner476148f2001-11-26 16:54:11 +00001684 $$ = new InvokeInst(V, Normal, Except, *$5);
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001685 }
Chris Lattner87c593b2003-12-23 22:18:36 +00001686 delete $2;
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001687 delete $5;
Chris Lattner9c58cf62003-09-08 18:54:55 +00001688 }
1689 | UNWIND {
1690 $$ = new UnwindInst();
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001691 };
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001692
1693
Chris Lattner2f7c9632001-06-06 20:29:01 +00001694
1695JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1696 $$ = $1;
Chris Lattner3462ae32001-12-03 22:26:30 +00001697 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner2f7c9632001-06-06 20:29:01 +00001698 if (V == 0)
1699 ThrowException("May only switch on a constant pool value!");
1700
Chris Lattner89da8a32003-04-22 18:42:41 +00001701 $$->push_back(std::make_pair(V, cast<BasicBlock>(getVal($5, $6))));
Chris Lattner2f7c9632001-06-06 20:29:01 +00001702 }
1703 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner89da8a32003-04-22 18:42:41 +00001704 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Chris Lattner3462ae32001-12-03 22:26:30 +00001705 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner2f7c9632001-06-06 20:29:01 +00001706
1707 if (V == 0)
1708 ThrowException("May only switch on a constant pool value!");
1709
Chris Lattner89da8a32003-04-22 18:42:41 +00001710 $$->push_back(std::make_pair(V, cast<BasicBlock>(getVal($4, $5))));
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001711 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00001712
1713Inst : OptAssign InstVal {
Chris Lattnerfb3e5492001-10-03 15:39:04 +00001714 // Is this definition named?? if so, assign the name...
1715 if (setValueName($2, $1)) { assert(0 && "No redefin allowed!"); }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001716 InsertValue($2);
1717 $$ = $2;
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001718};
Chris Lattner2f7c9632001-06-06 20:29:01 +00001719
Chris Lattner931ef3b2001-06-11 15:04:20 +00001720PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Chris Lattner89da8a32003-04-22 18:42:41 +00001721 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
1722 $$->push_back(std::make_pair(getVal(*$1, $3),
1723 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
Chris Lattneraa534bf2001-09-07 16:35:17 +00001724 delete $1;
Chris Lattner931ef3b2001-06-11 15:04:20 +00001725 }
1726 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1727 $$ = $1;
Chris Lattner89da8a32003-04-22 18:42:41 +00001728 $1->push_back(std::make_pair(getVal($1->front().first->getType(), $4),
1729 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001730 };
Chris Lattner931ef3b2001-06-11 15:04:20 +00001731
1732
Chris Lattneraa534bf2001-09-07 16:35:17 +00001733ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner89da8a32003-04-22 18:42:41 +00001734 $$ = new std::vector<Value*>();
Chris Lattner252afba2001-07-26 16:29:15 +00001735 $$->push_back($1);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001736 }
Chris Lattner252afba2001-07-26 16:29:15 +00001737 | ValueRefList ',' ResolvedVal {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001738 $$ = $1;
Chris Lattner252afba2001-07-26 16:29:15 +00001739 $1->push_back($3);
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001740 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00001741
1742// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001743ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner2f7c9632001-06-06 20:29:01 +00001744
Chris Lattnerc0686fe2002-09-10 19:57:26 +00001745InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1746 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint())
1747 ThrowException("Arithmetic operator requires integer or FP operands!");
1748 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1749 if ($$ == 0)
1750 ThrowException("binary operator returned null!");
1751 delete $2;
1752 }
1753 | LogicalOps Types ValueRef ',' ValueRef {
1754 if (!(*$2)->isIntegral())
1755 ThrowException("Logical operator requires integral operands!");
1756 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1757 if ($$ == 0)
1758 ThrowException("binary operator returned null!");
1759 delete $2;
1760 }
1761 | SetCondOps Types ValueRef ',' ValueRef {
Chris Lattnera6834c12002-09-10 22:37:46 +00001762 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner2f7c9632001-06-06 20:29:01 +00001763 if ($$ == 0)
1764 ThrowException("binary operator returned null!");
Chris Lattneraa534bf2001-09-07 16:35:17 +00001765 delete $2;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001766 }
Chris Lattnerdb3b2022002-08-14 17:12:33 +00001767 | NOT ResolvedVal {
1768 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
1769 << " Replacing with 'xor'.\n";
1770
1771 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
1772 if (Ones == 0)
1773 ThrowException("Expected integral type for not instruction!");
1774
1775 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001776 if ($$ == 0)
Chris Lattnerdb3b2022002-08-14 17:12:33 +00001777 ThrowException("Could not create a xor instruction!");
Chris Lattnera6821822001-07-08 04:57:15 +00001778 }
Chris Lattner252afba2001-07-26 16:29:15 +00001779 | ShiftOps ResolvedVal ',' ResolvedVal {
1780 if ($4->getType() != Type::UByteTy)
1781 ThrowException("Shift amount must be ubyte!");
Chris Lattner0aa8eaf2003-10-17 05:11:44 +00001782 if (!$2->getType()->isInteger())
1783 ThrowException("Shift constant expression requires integer operand!");
Chris Lattner252afba2001-07-26 16:29:15 +00001784 $$ = new ShiftInst($1, $2, $4);
Chris Lattnerd8bebcd2001-07-08 21:10:27 +00001785 }
Chris Lattner252afba2001-07-26 16:29:15 +00001786 | CAST ResolvedVal TO Types {
Chris Lattner6f8cd632003-10-10 03:56:01 +00001787 if (!$4->get()->isFirstClassType())
1788 ThrowException("cast instruction to a non-primitive type: '" +
1789 $4->get()->getDescription() + "'!");
Chris Lattneraa534bf2001-09-07 16:35:17 +00001790 $$ = new CastInst($2, *$4);
1791 delete $4;
Chris Lattnera6821822001-07-08 04:57:15 +00001792 }
Chris Lattnerf70da102003-05-08 02:44:12 +00001793 | VA_ARG ResolvedVal ',' Types {
Chris Lattner0079e7d2003-10-18 05:53:13 +00001794 // FIXME: This is emulation code for an obsolete syntax. This should be
1795 // removed at some point.
1796 if (!ObsoleteVarArgs) {
1797 std::cerr << "WARNING: this file uses obsolete features. "
1798 << "Assemble and disassemble to update it.\n";
1799 ObsoleteVarArgs = true;
1800 }
1801
1802 // First, load the valist...
1803 Instruction *CurVAList = new LoadInst($2, "");
1804 CurBB->getInstList().push_back(CurVAList);
1805
1806 // Emit the vaarg instruction.
1807 $$ = new VAArgInst(CurVAList, *$4);
1808
1809 // Now we must advance the pointer and update it in memory.
1810 Instruction *TheVANext = new VANextInst(CurVAList, *$4);
1811 CurBB->getInstList().push_back(TheVANext);
1812
1813 CurBB->getInstList().push_back(new StoreInst(TheVANext, $2));
1814 delete $4;
1815 }
1816 | VAARG ResolvedVal ',' Types {
1817 $$ = new VAArgInst($2, *$4);
1818 delete $4;
1819 }
1820 | VANEXT ResolvedVal ',' Types {
1821 $$ = new VANextInst($2, *$4);
Chris Lattnerf70da102003-05-08 02:44:12 +00001822 delete $4;
1823 }
Chris Lattnerb94550e2003-10-19 21:34:28 +00001824 | PHI_TOK PHIList {
Chris Lattner931ef3b2001-06-11 15:04:20 +00001825 const Type *Ty = $2->front().first->getType();
Chris Lattner0fc43a62003-10-30 01:38:18 +00001826 if (!Ty->isFirstClassType())
1827 ThrowException("PHI node operands must be of first class type!");
Chris Lattner931ef3b2001-06-11 15:04:20 +00001828 $$ = new PHINode(Ty);
Chris Lattner8ad18312003-10-10 16:34:58 +00001829 $$->op_reserve($2->size()*2);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001830 while ($2->begin() != $2->end()) {
Chris Lattner931ef3b2001-06-11 15:04:20 +00001831 if ($2->front().first->getType() != Ty)
1832 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerda558102001-10-02 03:41:24 +00001833 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001834 $2->pop_front();
1835 }
1836 delete $2; // Free the list...
1837 }
Chris Lattner26e50dc2001-07-28 17:48:55 +00001838 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattner19613292002-10-15 21:41:14 +00001839 const PointerType *PFTy;
Chris Lattner57698e22002-03-26 18:01:55 +00001840 const FunctionType *Ty;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001841
Chris Lattner19613292002-10-15 21:41:14 +00001842 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1843 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner42b5a8a2001-07-25 22:47:46 +00001844 // Pull out the types of all of the arguments...
Chris Lattner89da8a32003-04-22 18:42:41 +00001845 std::vector<const Type*> ParamTypes;
Chris Lattner7fac0702001-10-03 14:53:21 +00001846 if ($5) {
Chris Lattner89da8a32003-04-22 18:42:41 +00001847 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1848 I != E; ++I)
Chris Lattner7fac0702001-10-03 14:53:21 +00001849 ParamTypes.push_back((*I)->getType());
1850 }
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001851
1852 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1853 if (isVarArg) ParamTypes.pop_back();
1854
Chris Lattner57698e22002-03-26 18:01:55 +00001855 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattner19613292002-10-15 21:41:14 +00001856 PFTy = PointerType::get(Ty);
Chris Lattner42b5a8a2001-07-25 22:47:46 +00001857 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001858
Chris Lattner19613292002-10-15 21:41:14 +00001859 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner2f7c9632001-06-06 20:29:01 +00001860
Chris Lattner42b5a8a2001-07-25 22:47:46 +00001861 // Create the call node...
1862 if (!$5) { // Has no arguments?
Chris Lattner91e08322002-07-25 20:52:56 +00001863 // Make sure no arguments is a good thing!
1864 if (Ty->getNumParams() != 0)
1865 ThrowException("No arguments passed to a function that "
1866 "expects arguments!");
1867
Chris Lattner89da8a32003-04-22 18:42:41 +00001868 $$ = new CallInst(V, std::vector<Value*>());
Chris Lattner42b5a8a2001-07-25 22:47:46 +00001869 } else { // Has arguments?
Chris Lattner57698e22002-03-26 18:01:55 +00001870 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2f7c9632001-06-06 20:29:01 +00001871 // correctly!
1872 //
Chris Lattner57698e22002-03-26 18:01:55 +00001873 FunctionType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1874 FunctionType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner89da8a32003-04-22 18:42:41 +00001875 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner42b5a8a2001-07-25 22:47:46 +00001876
1877 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1878 if ((*ArgI)->getType() != *I)
1879 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattneref99d3c2001-12-14 16:28:42 +00001880 (*I)->getDescription() + "'!");
Chris Lattner2f7c9632001-06-06 20:29:01 +00001881
Chris Lattner42b5a8a2001-07-25 22:47:46 +00001882 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner2f7c9632001-06-06 20:29:01 +00001883 ThrowException("Invalid number of parameters detected!");
Chris Lattner2f7c9632001-06-06 20:29:01 +00001884
Chris Lattner476148f2001-11-26 16:54:11 +00001885 $$ = new CallInst(V, *$5);
Chris Lattner42b5a8a2001-07-25 22:47:46 +00001886 }
Chris Lattner601415e2003-12-23 20:39:17 +00001887 delete $2;
Chris Lattner42b5a8a2001-07-25 22:47:46 +00001888 delete $5;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001889 }
1890 | MemoryInst {
1891 $$ = $1;
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001892 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00001893
Chris Lattner476148f2001-11-26 16:54:11 +00001894
1895// IndexList - List of indices for GEP based instructions...
1896IndexList : ',' ValueRefList {
Chris Lattner8b1680e2003-09-08 18:20:29 +00001897 $$ = $2;
1898 } | /* empty */ {
1899 $$ = new std::vector<Value*>();
1900 };
1901
1902OptVolatile : VOLATILE {
1903 $$ = true;
1904 }
1905 | /* empty */ {
1906 $$ = false;
1907 };
1908
Chris Lattnerd8bebcd2001-07-08 21:10:27 +00001909
Chris Lattner2f7c9632001-06-06 20:29:01 +00001910MemoryInst : MALLOC Types {
Chris Lattner322bf4f2002-09-13 22:28:45 +00001911 $$ = new MallocInst(*$2);
Chris Lattneraa534bf2001-09-07 16:35:17 +00001912 delete $2;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001913 }
1914 | MALLOC Types ',' UINT ValueRef {
Chris Lattner322bf4f2002-09-13 22:28:45 +00001915 $$ = new MallocInst(*$2, getVal($4, $5));
Chris Lattneraa534bf2001-09-07 16:35:17 +00001916 delete $2;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001917 }
1918 | ALLOCA Types {
Chris Lattner322bf4f2002-09-13 22:28:45 +00001919 $$ = new AllocaInst(*$2);
Chris Lattneraa534bf2001-09-07 16:35:17 +00001920 delete $2;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001921 }
1922 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner322bf4f2002-09-13 22:28:45 +00001923 $$ = new AllocaInst(*$2, getVal($4, $5));
Chris Lattneraa534bf2001-09-07 16:35:17 +00001924 delete $2;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001925 }
Chris Lattner252afba2001-07-26 16:29:15 +00001926 | FREE ResolvedVal {
Chris Lattner181cc322002-05-06 16:15:30 +00001927 if (!isa<PointerType>($2->getType()))
Chris Lattner252afba2001-07-26 16:29:15 +00001928 ThrowException("Trying to free nonpointer type " +
Chris Lattneref99d3c2001-12-14 16:28:42 +00001929 $2->getType()->getDescription() + "!");
Chris Lattner252afba2001-07-26 16:29:15 +00001930 $$ = new FreeInst($2);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001931 }
1932
Chris Lattner8b1680e2003-09-08 18:20:29 +00001933 | OptVolatile LOAD Types ValueRef {
1934 if (!isa<PointerType>($3->get()))
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001935 ThrowException("Can't load from nonpointer type: " +
Chris Lattner8b1680e2003-09-08 18:20:29 +00001936 (*$3)->getDescription());
Chris Lattner9eea08c2003-09-08 20:29:46 +00001937 $$ = new LoadInst(getVal(*$3, $4), "", $1);
Chris Lattner8b1680e2003-09-08 18:20:29 +00001938 delete $3;
Chris Lattnerd8bebcd2001-07-08 21:10:27 +00001939 }
Chris Lattner8b1680e2003-09-08 18:20:29 +00001940 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
1941 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner667ded92003-09-01 16:31:28 +00001942 if (!PT)
Chris Lattneref99d3c2001-12-14 16:28:42 +00001943 ThrowException("Can't store to a nonpointer type: " +
Chris Lattner8b1680e2003-09-08 18:20:29 +00001944 (*$5)->getDescription());
Chris Lattner667ded92003-09-01 16:31:28 +00001945 const Type *ElTy = PT->getElementType();
Chris Lattner8b1680e2003-09-08 18:20:29 +00001946 if (ElTy != $3->getType())
1947 ThrowException("Can't store '" + $3->getType()->getDescription() +
Chris Lattneref99d3c2001-12-14 16:28:42 +00001948 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner319c47a2002-08-21 23:51:21 +00001949
Chris Lattner9eea08c2003-09-08 20:29:46 +00001950 $$ = new StoreInst($3, getVal(*$5, $6), $1);
Chris Lattner8b1680e2003-09-08 18:20:29 +00001951 delete $5;
Chris Lattner62ecb4a2001-07-08 23:22:50 +00001952 }
Chris Lattner476148f2001-11-26 16:54:11 +00001953 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001954 if (!isa<PointerType>($2->get()))
Chris Lattner62ecb4a2001-07-08 23:22:50 +00001955 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattneraa534bf2001-09-07 16:35:17 +00001956 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattneref99d3c2001-12-14 16:28:42 +00001957 ThrowException("Can't get element ptr '" + (*$2)->getDescription()+ "'!");
Chris Lattneraa534bf2001-09-07 16:35:17 +00001958 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
1959 delete $2; delete $4;
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001960 };
Chris Lattnerd8bebcd2001-07-08 21:10:27 +00001961
Brian Gaeke960707c2003-11-11 22:41:34 +00001962
Chris Lattner2f7c9632001-06-06 20:29:01 +00001963%%
Chris Lattnera6821822001-07-08 04:57:15 +00001964int yyerror(const char *ErrorMsg) {
Chris Lattner89da8a32003-04-22 18:42:41 +00001965 std::string where
1966 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adve7064eaf2002-07-14 22:59:28 +00001967 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Chris Lattner89da8a32003-04-22 18:42:41 +00001968 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
Vikram S. Adve7064eaf2002-07-14 22:59:28 +00001969 if (yychar == YYEMPTY)
1970 errMsg += "end-of-file.";
1971 else
Chris Lattner89da8a32003-04-22 18:42:41 +00001972 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Vikram S. Adve7064eaf2002-07-14 22:59:28 +00001973 ThrowException(errMsg);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001974 return 0;
1975}