blob: 181ca19e14e53663695a15ca572c6012d290a8b5 [file] [log] [blame]
Chris Lattner2f7c9632001-06-06 20:29:01 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files ---------*- C++ -*--=//
2//
3// This file implements the bison parser for LLVM assembly languages files.
4//
5//===------------------------------------------------------------------------=//
6
7//
8// TODO: Parse comments and add them to an internal node... so that they may
9// be saved in the bytecode format as well as everything else. Very important
10// for a general IR format.
11//
12
13%{
14#include "ParserInternals.h"
15#include "llvm/BasicBlock.h"
16#include "llvm/Method.h"
17#include "llvm/SymbolTable.h"
18#include "llvm/Module.h"
19#include "llvm/Type.h"
20#include "llvm/DerivedTypes.h"
21#include "llvm/Assembly/Parser.h"
22#include "llvm/ConstantPool.h"
23#include "llvm/iTerminators.h"
24#include "llvm/iMemory.h"
25#include <list>
26#include <utility> // Get definition of pair class
Chris Lattnerd9c40e32001-07-09 19:38:36 +000027#include <algorithm> // Get definition of find_if
Chris Lattner2f7c9632001-06-06 20:29:01 +000028#include <stdio.h> // This embarasment is due to our flex lexer...
29
Chris Lattnera6821822001-07-08 04:57:15 +000030int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
31int yylex(); // declaration" of xxx warnings.
Chris Lattner2f7c9632001-06-06 20:29:01 +000032int yyparse();
33
34static Module *ParserResult;
Chris Lattnerf2d1e792001-07-22 18:36:00 +000035string CurFilename;
Chris Lattner2f7c9632001-06-06 20:29:01 +000036
37// This contains info used when building the body of a method. It is destroyed
38// when the method is completed.
39//
40typedef vector<Value *> ValueList; // Numbered defs
41static void ResolveDefinitions(vector<ValueList> &LateResolvers);
42
43static struct PerModuleInfo {
44 Module *CurrentModule;
45 vector<ValueList> Values; // Module level numbered definitions
46 vector<ValueList> LateResolveValues;
47
48 void ModuleDone() {
49 // If we could not resolve some blocks at parsing time (forward branches)
50 // resolve the branches now...
51 ResolveDefinitions(LateResolveValues);
52
53 Values.clear(); // Clear out method local definitions
54 CurrentModule = 0;
55 }
56} CurModule;
57
58static struct PerMethodInfo {
59 Method *CurrentMethod; // Pointer to current method being created
60
Chris Lattner17f729e2001-07-15 06:35:53 +000061 vector<ValueList> Values; // Keep track of numbered definitions
Chris Lattner2f7c9632001-06-06 20:29:01 +000062 vector<ValueList> LateResolveValues;
Chris Lattner17f729e2001-07-15 06:35:53 +000063 bool isDeclare; // Is this method a forward declararation?
Chris Lattner2f7c9632001-06-06 20:29:01 +000064
65 inline PerMethodInfo() {
66 CurrentMethod = 0;
Chris Lattner17f729e2001-07-15 06:35:53 +000067 isDeclare = false;
Chris Lattner2f7c9632001-06-06 20:29:01 +000068 }
69
70 inline ~PerMethodInfo() {}
71
72 inline void MethodStart(Method *M) {
73 CurrentMethod = M;
74 }
75
76 void MethodDone() {
77 // If we could not resolve some blocks at parsing time (forward branches)
78 // resolve the branches now...
79 ResolveDefinitions(LateResolveValues);
80
81 Values.clear(); // Clear out method local definitions
82 CurrentMethod = 0;
Chris Lattner17f729e2001-07-15 06:35:53 +000083 isDeclare = false;
Chris Lattner2f7c9632001-06-06 20:29:01 +000084 }
85} CurMeth; // Info for the current method...
86
87
88//===----------------------------------------------------------------------===//
89// Code to handle definitions of all the types
90//===----------------------------------------------------------------------===//
91
Chris Lattner26e50dc2001-07-28 17:48:55 +000092static void InsertValue(Value *D, vector<ValueList> &ValueTab = CurMeth.Values){
Chris Lattner2f7c9632001-06-06 20:29:01 +000093 if (!D->hasName()) { // Is this a numbered definition?
94 unsigned type = D->getType()->getUniqueID();
95 if (ValueTab.size() <= type)
96 ValueTab.resize(type+1, ValueList());
97 //printf("Values[%d][%d] = %d\n", type, ValueTab[type].size(), D);
98 ValueTab[type].push_back(D);
99 }
100}
101
Chris Lattner252afba2001-07-26 16:29:15 +0000102static Value *getVal(const Type *Type, const ValID &D,
Chris Lattner2f7c9632001-06-06 20:29:01 +0000103 bool DoNotImprovise = false) {
104 switch (D.Type) {
105 case 0: { // Is it a numbered definition?
106 unsigned type = Type->getUniqueID();
107 unsigned Num = (unsigned)D.Num;
108
109 // Module constants occupy the lowest numbered slots...
110 if (type < CurModule.Values.size()) {
111 if (Num < CurModule.Values[type].size())
112 return CurModule.Values[type][Num];
113
114 Num -= CurModule.Values[type].size();
115 }
116
117 // Make sure that our type is within bounds
118 if (CurMeth.Values.size() <= type)
119 break;
120
121 // Check that the number is within bounds...
122 if (CurMeth.Values[type].size() <= Num)
123 break;
124
125 return CurMeth.Values[type][Num];
126 }
127 case 1: { // Is it a named definition?
128 string Name(D.Name);
129 SymbolTable *SymTab = 0;
130 if (CurMeth.CurrentMethod)
131 SymTab = CurMeth.CurrentMethod->getSymbolTable();
132 Value *N = SymTab ? SymTab->lookup(Type, Name) : 0;
133
134 if (N == 0) {
135 SymTab = CurModule.CurrentModule->getSymbolTable();
136 if (SymTab)
137 N = SymTab->lookup(Type, Name);
138 if (N == 0) break;
139 }
140
141 D.destroy(); // Free old strdup'd memory...
142 return N;
143 }
144
145 case 2: // Is it a constant pool reference??
146 case 3: // Is it an unsigned const pool reference?
Chris Lattner212f70d2001-07-15 00:17:01 +0000147 case 4: // Is it a string const pool reference?
148 case 5:{ // Is it a floating point const pool reference?
Chris Lattner2f7c9632001-06-06 20:29:01 +0000149 ConstPoolVal *CPV = 0;
150
151 // Check to make sure that "Type" is an integral type, and that our
152 // value will fit into the specified type...
153 switch (D.Type) {
154 case 2:
155 if (Type == Type::BoolTy) { // Special handling for boolean data
156 CPV = new ConstPoolBool(D.ConstPool64 != 0);
157 } else {
158 if (!ConstPoolSInt::isValueValidForType(Type, D.ConstPool64))
Chris Lattner212f70d2001-07-15 00:17:01 +0000159 ThrowException("Symbolic constant pool value '" +
160 itostr(D.ConstPool64) + "' is invalid for type '" +
161 Type->getName() + "'!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000162 CPV = new ConstPoolSInt(Type, D.ConstPool64);
163 }
164 break;
165 case 3:
166 if (!ConstPoolUInt::isValueValidForType(Type, D.UConstPool64)) {
167 if (!ConstPoolSInt::isValueValidForType(Type, D.ConstPool64)) {
Chris Lattner212f70d2001-07-15 00:17:01 +0000168 ThrowException("Integral constant pool reference is invalid!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000169 } else { // This is really a signed reference. Transmogrify.
170 CPV = new ConstPoolSInt(Type, D.ConstPool64);
171 }
172 } else {
173 CPV = new ConstPoolUInt(Type, D.UConstPool64);
174 }
175 break;
176 case 4:
177 cerr << "FIXME: TODO: String constants [sbyte] not implemented yet!\n";
178 abort();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000179 break;
Chris Lattner212f70d2001-07-15 00:17:01 +0000180 case 5:
181 if (!ConstPoolFP::isValueValidForType(Type, D.ConstPoolFP))
182 ThrowException("FP constant invalid for type!!");
183 else
184 CPV = new ConstPoolFP(Type, D.ConstPoolFP);
185 break;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000186 }
187 assert(CPV && "How did we escape creating a constant??");
188
189 // Scan through the constant table and see if we already have loaded this
190 // constant.
191 //
192 ConstantPool &CP = CurMeth.CurrentMethod ?
193 CurMeth.CurrentMethod->getConstantPool() :
194 CurModule.CurrentModule->getConstantPool();
195 ConstPoolVal *C = CP.find(CPV); // Already have this constant?
196 if (C) {
197 delete CPV; // Didn't need this after all, oh well.
198 return C; // Yup, we already have one, recycle it!
199 }
200 CP.insert(CPV);
201
202 // Success, everything is kosher. Lets go!
203 return CPV;
204 } // End of case 2,3,4
205 } // End of switch
206
207
208 // If we reached here, we referenced either a symbol that we don't know about
209 // or an id number that hasn't been read yet. We may be referencing something
210 // forward, so just create an entry to be resolved later and get to it...
211 //
212 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
213
214 // TODO: Attempt to coallecse nodes that are the same with previous ones.
215 Value *d = 0;
Chris Lattner26e50dc2001-07-28 17:48:55 +0000216 vector<ValueList> *LateResolver = &CurMeth.LateResolveValues;
217
Chris Lattner2f7c9632001-06-06 20:29:01 +0000218 switch (Type->getPrimitiveID()) {
Chris Lattner26e50dc2001-07-28 17:48:55 +0000219 case Type::LabelTyID: d = new BBPlaceHolder(Type, D); break;
220 case Type::MethodTyID: d = new MethPlaceHolder(Type, D);
221 LateResolver = &CurModule.LateResolveValues; break;
222 default: d = new ValuePlaceHolder(Type, D); break;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000223 }
224
225 assert(d != 0 && "How did we not make something?");
Chris Lattner26e50dc2001-07-28 17:48:55 +0000226 InsertValue(d, *LateResolver);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000227 return d;
228}
229
230
231//===----------------------------------------------------------------------===//
232// Code to handle forward references in instructions
233//===----------------------------------------------------------------------===//
234//
235// This code handles the late binding needed with statements that reference
236// values not defined yet... for example, a forward branch, or the PHI node for
237// a loop body.
238//
239// This keeps a table (CurMeth.LateResolveValues) of all such forward references
240// and back patchs after we are done.
241//
242
243// ResolveDefinitions - If we could not resolve some defs at parsing
244// time (forward branches, phi functions for loops, etc...) resolve the
245// defs now...
246//
247static void ResolveDefinitions(vector<ValueList> &LateResolvers) {
248 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
249 for (unsigned ty = 0; ty < LateResolvers.size(); ty++) {
250 while (!LateResolvers[ty].empty()) {
251 Value *V = LateResolvers[ty].back();
252 LateResolvers[ty].pop_back();
253 ValID &DID = getValIDFromPlaceHolder(V);
254
255 Value *TheRealValue = getVal(Type::getUniqueIDType(ty), DID, true);
256
257 if (TheRealValue == 0 && DID.Type == 1)
258 ThrowException("Reference to an invalid definition: '" +DID.getName() +
Chris Lattner26e50dc2001-07-28 17:48:55 +0000259 "' of type '" + V->getType()->getName() + "'",
260 getLineNumFromPlaceHolder(V));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000261 else if (TheRealValue == 0)
262 ThrowException("Reference to an invalid definition: #" +itostr(DID.Num)+
Chris Lattner26e50dc2001-07-28 17:48:55 +0000263 " of type '" + V->getType()->getName() + "'",
264 getLineNumFromPlaceHolder(V));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000265
266 V->replaceAllUsesWith(TheRealValue);
267 assert(V->use_empty());
268 delete V;
269 }
270 }
271
272 LateResolvers.clear();
273}
274
275// addConstValToConstantPool - This code is used to insert a constant into the
276// current constant pool. This is designed to make maximal (but not more than
277// possible) reuse (merging) of constants in the constant pool. This means that
278// multiple references to %4, for example will all get merged.
279//
280static ConstPoolVal *addConstValToConstantPool(ConstPoolVal *C) {
281 vector<ValueList> &ValTab = CurMeth.CurrentMethod ?
282 CurMeth.Values : CurModule.Values;
283 ConstantPool &CP = CurMeth.CurrentMethod ?
284 CurMeth.CurrentMethod->getConstantPool() :
285 CurModule.CurrentModule->getConstantPool();
286
287 if (ConstPoolVal *CPV = CP.find(C)) {
288 // Constant already in constant pool. Try to merge the two constants
289 if (CPV->hasName() && !C->hasName()) {
290 // Merge the two values, we inherit the existing CPV's name.
291 // InsertValue requires that the value have no name to insert correctly
292 // (because we want to fill the slot this constant would have filled)
293 //
294 string Name = CPV->getName();
295 CPV->setName("");
296 InsertValue(CPV, ValTab);
297 CPV->setName(Name);
298 delete C;
299 return CPV;
300 } else if (!CPV->hasName() && C->hasName()) {
301 // If we have a name on this value and there isn't one in the const
302 // pool val already, propogate it.
303 //
304 CPV->setName(C->getName());
305 delete C; // Sorry, you're toast
306 return CPV;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000307 } else if (!CPV->hasName() && !C->hasName()) {
308 // Neither value has a name, trivially merge them.
309 InsertValue(CPV, ValTab);
310 delete C;
311 return CPV;
Chris Lattner26e50dc2001-07-28 17:48:55 +0000312 } else if (CPV->hasName() && C->hasName()) {
313 // Both values have distinct names. We cannot merge them.
314 // fall through
Chris Lattner2f7c9632001-06-06 20:29:01 +0000315 }
Chris Lattner26e50dc2001-07-28 17:48:55 +0000316 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000317
Chris Lattner26e50dc2001-07-28 17:48:55 +0000318 // No duplication of value, check to see if our current symbol table already
319 // has a variable of this type and name...
320 //
321 if (C->hasName()) {
322 SymbolTable *SymTab = CurMeth.CurrentMethod ?
323 CurMeth.CurrentMethod->getSymbolTable() :
324 CurModule.CurrentModule->getSymbolTable();
325 if (SymTab && SymTab->lookup(C->getType(), C->getName()))
326 ThrowException("<" + C->getType()->getName() + ">:" + C->getName() +
327 " already defined in translation unit!");
328 }
329
330 // Everything is happy: Insert into constant pool now!
331 CP.insert(C);
332 InsertValue(C, ValTab);
333 return C;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000334}
335
Chris Lattnerd9c40e32001-07-09 19:38:36 +0000336
337struct EqualsType {
338 const Type *T;
339 inline EqualsType(const Type *t) { T = t; }
340 inline bool operator()(const ConstPoolVal *CPV) const {
341 return static_cast<const ConstPoolType*>(CPV)->getValue() == T;
342 }
343};
344
345
346// checkNewType - We have to be careful to add all types referenced by the
347// program to the constant pool of the method or module. Because of this, we
348// often want to check to make sure that types used are in the constant pool,
349// and add them if they aren't. That's what this function does.
350//
351static const Type *checkNewType(const Type *Ty) {
352 ConstantPool &CP = CurMeth.CurrentMethod ?
353 CurMeth.CurrentMethod->getConstantPool() :
354 CurModule.CurrentModule->getConstantPool();
355
Chris Lattner1caf0bb2001-07-20 19:15:08 +0000356 // TODO: This should use ConstantPool::ensureTypeAvailable
357
Chris Lattnerd9c40e32001-07-09 19:38:36 +0000358 // Get the type type plane...
359 ConstantPool::PlaneType &P = CP.getPlane(Type::TypeTy);
360 ConstantPool::PlaneType::const_iterator PI = find_if(P.begin(), P.end(),
361 EqualsType(Ty));
362 if (PI == P.end()) {
363 vector<ValueList> &ValTab = CurMeth.CurrentMethod ?
364 CurMeth.Values : CurModule.Values;
365 ConstPoolVal *CPT = new ConstPoolType(Ty);
366 CP.insert(CPT);
367 InsertValue(CPT, ValTab);
368 }
369 return Ty;
370}
371
372
Chris Lattner2f7c9632001-06-06 20:29:01 +0000373//===----------------------------------------------------------------------===//
374// RunVMAsmParser - Define an interface to this parser
375//===----------------------------------------------------------------------===//
376//
Chris Lattnerf2d1e792001-07-22 18:36:00 +0000377Module *RunVMAsmParser(const string &Filename, FILE *F) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000378 llvmAsmin = F;
Chris Lattnerf2d1e792001-07-22 18:36:00 +0000379 CurFilename = Filename;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000380 llvmAsmlineno = 1; // Reset the current line number...
381
382 CurModule.CurrentModule = new Module(); // Allocate a new module to read
383 yyparse(); // Parse the file.
384 Module *Result = ParserResult;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000385 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
386 ParserResult = 0;
387
388 return Result;
389}
390
391%}
392
393%union {
394 Module *ModuleVal;
395 Method *MethodVal;
396 MethodArgument *MethArgVal;
397 BasicBlock *BasicBlockVal;
398 TerminatorInst *TermInstVal;
399 Instruction *InstVal;
400 ConstPoolVal *ConstVal;
401 const Type *TypeVal;
Chris Lattner252afba2001-07-26 16:29:15 +0000402 Value *ValueVal;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000403
404 list<MethodArgument*> *MethodArgList;
405 list<Value*> *ValueList;
406 list<const Type*> *TypeList;
Chris Lattner931ef3b2001-06-11 15:04:20 +0000407 list<pair<Value*, BasicBlock*> > *PHIList; // Represent the RHS of PHI node
Chris Lattner2f7c9632001-06-06 20:29:01 +0000408 list<pair<ConstPoolVal*, BasicBlock*> > *JumpTable;
409 vector<ConstPoolVal*> *ConstVector;
410
411 int64_t SInt64Val;
412 uint64_t UInt64Val;
413 int SIntVal;
414 unsigned UIntVal;
Chris Lattner212f70d2001-07-15 00:17:01 +0000415 double FPVal;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000416
417 char *StrVal; // This memory is allocated by strdup!
418 ValID ValIDVal; // May contain memory allocated by strdup
419
420 Instruction::UnaryOps UnaryOpVal;
421 Instruction::BinaryOps BinaryOpVal;
422 Instruction::TermOps TermOpVal;
423 Instruction::MemoryOps MemOpVal;
Chris Lattnerd8bebcd2001-07-08 21:10:27 +0000424 Instruction::OtherOps OtherOpVal;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000425}
426
427%type <ModuleVal> Module MethodList
Chris Lattner17f729e2001-07-15 06:35:53 +0000428%type <MethodVal> Method MethodProto MethodHeader BasicBlockList
Chris Lattner2f7c9632001-06-06 20:29:01 +0000429%type <BasicBlockVal> BasicBlock InstructionList
430%type <TermInstVal> BBTerminatorInst
431%type <InstVal> Inst InstVal MemoryInst
Chris Lattner252afba2001-07-26 16:29:15 +0000432%type <ConstVal> ConstVal ExtendedConstVal
Chris Lattnerd8bebcd2001-07-08 21:10:27 +0000433%type <ConstVector> ConstVector UByteList
Chris Lattner2f7c9632001-06-06 20:29:01 +0000434%type <MethodArgList> ArgList ArgListH
435%type <MethArgVal> ArgVal
Chris Lattner931ef3b2001-06-11 15:04:20 +0000436%type <PHIList> PHIList
Chris Lattner62ecb4a2001-07-08 23:22:50 +0000437%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner42b5a8a2001-07-25 22:47:46 +0000438%type <TypeList> TypeList ArgTypeList
Chris Lattner2f7c9632001-06-06 20:29:01 +0000439%type <JumpTable> JumpTable
440
441%type <ValIDVal> ValueRef ConstValueRef // Reference to a definition or BB
Chris Lattner252afba2001-07-26 16:29:15 +0000442%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner2f7c9632001-06-06 20:29:01 +0000443// Tokens and types for handling constant integer values
444//
445// ESINT64VAL - A negative number within long long range
446%token <SInt64Val> ESINT64VAL
447
448// EUINT64VAL - A positive number within uns. long long range
449%token <UInt64Val> EUINT64VAL
450%type <SInt64Val> EINT64VAL
451
452%token <SIntVal> SINTVAL // Signed 32 bit ints...
453%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
454%type <SIntVal> INTVAL
Chris Lattner212f70d2001-07-15 00:17:01 +0000455%token <FPVal> FPVAL // Float or Double constant
Chris Lattner2f7c9632001-06-06 20:29:01 +0000456
457// Built in types...
Chris Lattner212f70d2001-07-15 00:17:01 +0000458%type <TypeVal> Types TypesV SIntType UIntType IntType FPType
Chris Lattner2f7c9632001-06-06 20:29:01 +0000459%token <TypeVal> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
Chris Lattner26e50dc2001-07-28 17:48:55 +0000460%token <TypeVal> FLOAT DOUBLE TYPE LABEL
Chris Lattner2f7c9632001-06-06 20:29:01 +0000461
462%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
463%type <StrVal> OptVAR_ID OptAssign
464
465
Chris Lattner26e50dc2001-07-28 17:48:55 +0000466%token IMPLEMENTATION TRUE FALSE BEGINTOK END DECLARE TO DOTDOTDOT STRING
Chris Lattner2f7c9632001-06-06 20:29:01 +0000467
468// Basic Block Terminating Operators
469%token <TermOpVal> RET BR SWITCH
470
471// Unary Operators
472%type <UnaryOpVal> UnaryOps // all the unary operators
Chris Lattner49c64322001-07-08 19:03:27 +0000473%token <UnaryOpVal> NOT
Chris Lattner2f7c9632001-06-06 20:29:01 +0000474
475// Binary Operators
476%type <BinaryOpVal> BinaryOps // all the binary operators
477%token <BinaryOpVal> ADD SUB MUL DIV REM
Chris Lattnerd8bebcd2001-07-08 21:10:27 +0000478%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner2f7c9632001-06-06 20:29:01 +0000479
480// Memory Instructions
Chris Lattner62ecb4a2001-07-08 23:22:50 +0000481%token <MemoryOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner2f7c9632001-06-06 20:29:01 +0000482
Chris Lattnerd8bebcd2001-07-08 21:10:27 +0000483// Other Operators
484%type <OtherOpVal> ShiftOps
485%token <OtherOpVal> PHI CALL CAST SHL SHR
486
Chris Lattner2f7c9632001-06-06 20:29:01 +0000487%start Module
488%%
489
490// Handle constant integer size restriction and conversion...
491//
492
493INTVAL : SINTVAL
494INTVAL : UINTVAL {
495 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
496 ThrowException("Value too large for type!");
497 $$ = (int32_t)$1;
498}
499
500
501EINT64VAL : ESINT64VAL // These have same type and can't cause problems...
502EINT64VAL : EUINT64VAL {
503 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
504 ThrowException("Value too large for type!");
505 $$ = (int64_t)$1;
506}
507
508// Types includes all predefined types... except void, because you can't do
509// anything with it except for certain specific things...
510//
Chris Lattner56f73d42001-07-14 06:10:16 +0000511// User defined types are added later...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000512//
513Types : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT
Chris Lattner26e50dc2001-07-28 17:48:55 +0000514Types : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL
Chris Lattner2f7c9632001-06-06 20:29:01 +0000515
516// TypesV includes all of 'Types', but it also includes the void type.
517TypesV : Types | VOID
518
519// Operations that are notably excluded from this list include:
520// RET, BR, & SWITCH because they end basic blocks and are treated specially.
521//
Chris Lattnera6821822001-07-08 04:57:15 +0000522UnaryOps : NOT
Chris Lattner2f7c9632001-06-06 20:29:01 +0000523BinaryOps : ADD | SUB | MUL | DIV | REM
524BinaryOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE
Chris Lattnerd8bebcd2001-07-08 21:10:27 +0000525ShiftOps : SHL | SHR
Chris Lattner2f7c9632001-06-06 20:29:01 +0000526
Chris Lattner56f73d42001-07-14 06:10:16 +0000527// These are some types that allow classification if we only want a particular
528// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000529SIntType : LONG | INT | SHORT | SBYTE
530UIntType : ULONG | UINT | USHORT | UBYTE
531IntType : SIntType | UIntType
Chris Lattner212f70d2001-07-15 00:17:01 +0000532FPType : FLOAT | DOUBLE
Chris Lattner2f7c9632001-06-06 20:29:01 +0000533
Chris Lattner56f73d42001-07-14 06:10:16 +0000534// OptAssign - Value producing statements have an optional assignment component
Chris Lattner2f7c9632001-06-06 20:29:01 +0000535OptAssign : VAR_ID '=' {
536 $$ = $1;
537 }
538 | /*empty*/ {
539 $$ = 0;
540 }
541
Chris Lattner56f73d42001-07-14 06:10:16 +0000542// ConstVal - The various declarations that go into the constant pool. This
543// includes all forward declarations of types, constants, and functions.
544//
Chris Lattner252afba2001-07-26 16:29:15 +0000545// This is broken into two sections: ExtendedConstVal and ConstVal
546//
547ExtendedConstVal: '[' Types ']' '[' ConstVector ']' { // Nonempty unsized array
Chris Lattner2f7c9632001-06-06 20:29:01 +0000548 // Verify all elements are correct type!
549 const ArrayType *AT = ArrayType::getArrayType($2);
550 for (unsigned i = 0; i < $5->size(); i++) {
551 if ($2 != (*$5)[i]->getType())
552 ThrowException("Element #" + utostr(i) + " is not of type '" +
553 $2->getName() + "' as required!\nIt is of type '" +
554 (*$5)[i]->getType()->getName() + "'.");
555 }
556
557 $$ = new ConstPoolArray(AT, *$5);
558 delete $5;
559 }
Chris Lattner252afba2001-07-26 16:29:15 +0000560 | '[' Types ']' '[' ']' { // Empty unsized array constant
Chris Lattner2f7c9632001-06-06 20:29:01 +0000561 vector<ConstPoolVal*> Empty;
562 $$ = new ConstPoolArray(ArrayType::getArrayType($2), Empty);
563 }
Chris Lattner26e50dc2001-07-28 17:48:55 +0000564 | '[' Types ']' 'c' STRINGCONSTANT {
565 char *EndStr = UnEscapeLexed($5, true);
566 vector<ConstPoolVal*> Vals;
567 if ($2 == Type::SByteTy) {
568 for (char *C = $5; C != EndStr; ++C)
569 Vals.push_back(addConstValToConstantPool(new ConstPoolSInt($2, *C)));
570 } else if ($2 == Type::UByteTy) {
571 for (char *C = $5; C != EndStr; ++C)
572 Vals.push_back(addConstValToConstantPool(new ConstPoolUInt($2, *C)));
573 } else {
574 ThrowException("Cannot build string arrays of non byte sized elements!");
575 }
576 free($5);
577
578 $$ = new ConstPoolArray(ArrayType::getArrayType($2), Vals);
579 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000580 | '[' EUINT64VAL 'x' Types ']' '[' ConstVector ']' {
581 // Verify all elements are correct type!
582 const ArrayType *AT = ArrayType::getArrayType($4, (int)$2);
583 if ($2 != $7->size())
584 ThrowException("Type mismatch: constant sized array initialized with " +
585 utostr($7->size()) + " arguments, but has size of " +
586 itostr((int)$2) + "!");
587
588 for (unsigned i = 0; i < $7->size(); i++) {
589 if ($4 != (*$7)[i]->getType())
590 ThrowException("Element #" + utostr(i) + " is not of type '" +
591 $4->getName() + "' as required!\nIt is of type '" +
592 (*$7)[i]->getType()->getName() + "'.");
593 }
594
595 $$ = new ConstPoolArray(AT, *$7);
596 delete $7;
597 }
598 | '[' EUINT64VAL 'x' Types ']' '[' ']' {
599 if ($2 != 0)
600 ThrowException("Type mismatch: constant sized array initialized with 0"
601 " arguments, but has size of " + itostr((int)$2) + "!");
602 vector<ConstPoolVal*> Empty;
603 $$ = new ConstPoolArray(ArrayType::getArrayType($4, 0), Empty);
604 }
Chris Lattner26e50dc2001-07-28 17:48:55 +0000605 | '[' EUINT64VAL 'x' Types ']' 'c' STRINGCONSTANT {
606 char *EndStr = UnEscapeLexed($7, true);
607 if ($2 != (unsigned)(EndStr-$7))
608 ThrowException("Can't build string constant of size " +
609 itostr((int)(EndStr-$7)) +
610 " when array has size " + itostr((int)$2) + "!");
611 vector<ConstPoolVal*> Vals;
612 if ($4 == Type::SByteTy) {
613 for (char *C = $7; C != EndStr; ++C)
614 Vals.push_back(addConstValToConstantPool(new ConstPoolSInt($4, *C)));
615 } else if ($4 == Type::UByteTy) {
616 for (char *C = $7; C != EndStr; ++C)
617 Vals.push_back(addConstValToConstantPool(new ConstPoolUInt($4, *C)));
618 } else {
619 ThrowException("Cannot build string arrays of non byte sized elements!");
620 }
621 free($7);
622
623 $$ = new ConstPoolArray(ArrayType::getArrayType($4, (int)$2), Vals);
624 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000625 | '{' TypeList '}' '{' ConstVector '}' {
626 StructType::ElementTypes Types($2->begin(), $2->end());
627 delete $2;
628
629 const StructType *St = StructType::getStructType(Types);
630 $$ = new ConstPoolStruct(St, *$5);
631 delete $5;
632 }
633 | '{' '}' '{' '}' {
634 const StructType *St =
635 StructType::getStructType(StructType::ElementTypes());
636 vector<ConstPoolVal*> Empty;
637 $$ = new ConstPoolStruct(St, Empty);
638 }
639/*
640 | Types '*' ConstVal {
641 assert(0);
642 $$ = 0;
643 }
644*/
645
Chris Lattner26e50dc2001-07-28 17:48:55 +0000646ConstVal : ExtendedConstVal {
647 $$ = $1;
648 }
649 | TYPE TypesV { // Type constants
Chris Lattner252afba2001-07-26 16:29:15 +0000650 $$ = new ConstPoolType($2);
651 }
652 | SIntType EINT64VAL { // integral constants
653 if (!ConstPoolSInt::isValueValidForType($1, $2))
654 ThrowException("Constant value doesn't fit in type!");
655 $$ = new ConstPoolSInt($1, $2);
656 }
657 | UIntType EUINT64VAL { // integral constants
658 if (!ConstPoolUInt::isValueValidForType($1, $2))
659 ThrowException("Constant value doesn't fit in type!");
660 $$ = new ConstPoolUInt($1, $2);
661 }
662 | BOOL TRUE { // Boolean constants
663 $$ = new ConstPoolBool(true);
664 }
665 | BOOL FALSE { // Boolean constants
666 $$ = new ConstPoolBool(false);
667 }
668 | FPType FPVAL { // Float & Double constants
669 $$ = new ConstPoolFP($1, $2);
670 }
Chris Lattner252afba2001-07-26 16:29:15 +0000671
Chris Lattner56f73d42001-07-14 06:10:16 +0000672// ConstVector - A list of comma seperated constants.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000673ConstVector : ConstVector ',' ConstVal {
674 ($$ = $1)->push_back(addConstValToConstantPool($3));
675 }
676 | ConstVal {
677 $$ = new vector<ConstPoolVal*>();
678 $$->push_back(addConstValToConstantPool($1));
679 }
680
Chris Lattner252afba2001-07-26 16:29:15 +0000681
Chris Lattner56f73d42001-07-14 06:10:16 +0000682//ExternMethodDecl : EXTERNAL TypesV '(' TypeList ')' {
683// }
684//ExternVarDecl :
Chris Lattner2f7c9632001-06-06 20:29:01 +0000685
Chris Lattner56f73d42001-07-14 06:10:16 +0000686// ConstPool - Constants with optional names assigned to them.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000687ConstPool : ConstPool OptAssign ConstVal {
688 if ($2) {
689 $3->setName($2);
690 free($2);
691 }
692
693 addConstValToConstantPool($3);
694 }
Chris Lattner26e50dc2001-07-28 17:48:55 +0000695| ConstPool MethodProto { // Method prototypes can be in const pool
696 }
Chris Lattner56f73d42001-07-14 06:10:16 +0000697/*
698 | ConstPool OptAssign GlobalDecl { // Global declarations appear in CP
699 if ($2) {
700 $3->setName($2);
701 free($2);
702 }
703 //CurModule.CurrentModule->
704 }
705*/
Chris Lattner2f7c9632001-06-06 20:29:01 +0000706 | /* empty: end of list */ {
707 }
708
709
710//===----------------------------------------------------------------------===//
711// Rules to match Modules
712//===----------------------------------------------------------------------===//
713
714// Module rule: Capture the result of parsing the whole file into a result
715// variable...
716//
717Module : MethodList {
718 $$ = ParserResult = $1;
719 CurModule.ModuleDone();
720}
721
Chris Lattner56f73d42001-07-14 06:10:16 +0000722// MethodList - A list of methods, preceeded by a constant pool.
723//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000724MethodList : MethodList Method {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000725 $$ = $1;
Chris Lattner17f729e2001-07-15 06:35:53 +0000726 if (!$2->getParent())
727 $1->getMethodList().push_back($2);
728 CurMeth.MethodDone();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000729 }
Chris Lattner17f729e2001-07-15 06:35:53 +0000730 | MethodList MethodProto {
731 $$ = $1;
Chris Lattner17f729e2001-07-15 06:35:53 +0000732 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000733 | ConstPool IMPLEMENTATION {
734 $$ = CurModule.CurrentModule;
735 }
736
737
738//===----------------------------------------------------------------------===//
739// Rules to match Method Headers
740//===----------------------------------------------------------------------===//
741
742OptVAR_ID : VAR_ID | /*empty*/ { $$ = 0; }
743
744ArgVal : Types OptVAR_ID {
745 $$ = new MethodArgument($1);
746 if ($2) { // Was the argument named?
747 $$->setName($2);
748 free($2); // The string was strdup'd, so free it now.
749 }
750}
751
752ArgListH : ArgVal ',' ArgListH {
753 $$ = $3;
754 $3->push_front($1);
755 }
756 | ArgVal {
757 $$ = new list<MethodArgument*>();
758 $$->push_front($1);
759 }
Chris Lattner42b5a8a2001-07-25 22:47:46 +0000760 | DOTDOTDOT {
761 $$ = new list<MethodArgument*>();
762 $$->push_back(new MethodArgument(Type::VoidTy));
763 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000764
765ArgList : ArgListH {
766 $$ = $1;
767 }
768 | /* empty */ {
769 $$ = 0;
770 }
771
772MethodHeaderH : TypesV STRINGCONSTANT '(' ArgList ')' {
Chris Lattner26e50dc2001-07-28 17:48:55 +0000773 UnEscapeLexed($2);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000774 MethodType::ParamTypes ParamTypeList;
775 if ($4)
Chris Lattner4cee8d82001-06-27 23:41:11 +0000776 for (list<MethodArgument*>::iterator I = $4->begin(); I != $4->end(); ++I)
Chris Lattner2f7c9632001-06-06 20:29:01 +0000777 ParamTypeList.push_back((*I)->getType());
778
779 const MethodType *MT = MethodType::getMethodType($1, ParamTypeList);
780
Chris Lattner17f729e2001-07-15 06:35:53 +0000781 Method *M = 0;
782 if (SymbolTable *ST = CurModule.CurrentModule->getSymbolTable()) {
783 if (Value *V = ST->lookup(MT, $2)) { // Method already in symtab?
784 M = V->castMethodAsserting();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000785
Chris Lattner17f729e2001-07-15 06:35:53 +0000786 // Yes it is. If this is the case, either we need to be a forward decl,
787 // or it needs to be.
788 if (!CurMeth.isDeclare && !M->isExternal())
789 ThrowException("Redefinition of method '" + string($2) + "'!");
790 }
791 }
792
793 if (M == 0) { // Not already defined?
794 M = new Method(MT, $2);
795 InsertValue(M, CurModule.Values);
796 }
797
798 free($2); // Free strdup'd memory!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000799
800 CurMeth.MethodStart(M);
801
802 // Add all of the arguments we parsed to the method...
Chris Lattner17f729e2001-07-15 06:35:53 +0000803 if ($4 && !CurMeth.isDeclare) { // Is null if empty...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000804 Method::ArgumentListType &ArgList = M->getArgumentList();
805
Chris Lattner4cee8d82001-06-27 23:41:11 +0000806 for (list<MethodArgument*>::iterator I = $4->begin(); I != $4->end(); ++I) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000807 InsertValue(*I);
808 ArgList.push_back(*I);
809 }
810 delete $4; // We're now done with the argument list
811 }
812}
813
814MethodHeader : MethodHeaderH ConstPool BEGINTOK {
815 $$ = CurMeth.CurrentMethod;
816}
817
818Method : BasicBlockList END {
819 $$ = $1;
820}
821
Chris Lattner17f729e2001-07-15 06:35:53 +0000822MethodProto : DECLARE { CurMeth.isDeclare = true; } MethodHeaderH {
823 $$ = CurMeth.CurrentMethod;
Chris Lattner26e50dc2001-07-28 17:48:55 +0000824 if (!$$->getParent())
825 CurModule.CurrentModule->getMethodList().push_back($$);
826 CurMeth.MethodDone();
Chris Lattner17f729e2001-07-15 06:35:53 +0000827}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000828
829//===----------------------------------------------------------------------===//
830// Rules to match Basic Blocks
831//===----------------------------------------------------------------------===//
832
833ConstValueRef : ESINT64VAL { // A reference to a direct constant
834 $$ = ValID::create($1);
835 }
836 | EUINT64VAL {
837 $$ = ValID::create($1);
838 }
Chris Lattner212f70d2001-07-15 00:17:01 +0000839 | FPVAL { // Perhaps it's an FP constant?
840 $$ = ValID::create($1);
841 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000842 | TRUE {
843 $$ = ValID::create((int64_t)1);
844 }
845 | FALSE {
846 $$ = ValID::create((int64_t)0);
847 }
Chris Lattner26e50dc2001-07-28 17:48:55 +0000848/*
Chris Lattner2f7c9632001-06-06 20:29:01 +0000849 | STRINGCONSTANT { // Quoted strings work too... especially for methods
850 $$ = ValID::create_conststr($1);
851 }
Chris Lattner26e50dc2001-07-28 17:48:55 +0000852*/
Chris Lattner2f7c9632001-06-06 20:29:01 +0000853
854// ValueRef - A reference to a definition...
855ValueRef : INTVAL { // Is it an integer reference...?
856 $$ = ValID::create($1);
857 }
Chris Lattner212f70d2001-07-15 00:17:01 +0000858 | VAR_ID { // Is it a named reference...?
Chris Lattner2f7c9632001-06-06 20:29:01 +0000859 $$ = ValID::create($1);
860 }
861 | ConstValueRef {
862 $$ = $1;
863 }
864
Chris Lattner252afba2001-07-26 16:29:15 +0000865// ResolvedVal - a <type> <value> pair. This is used only in cases where the
866// type immediately preceeds the value reference, and allows complex constant
867// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
868ResolvedVal : ExtendedConstVal {
869 $$ = addConstValToConstantPool($1);
870 }
871 | Types ValueRef {
872 $$ = getVal($1, $2);
873 }
874
875
Chris Lattner2f7c9632001-06-06 20:29:01 +0000876// The user may refer to a user defined type by its typeplane... check for this
877// now...
878//
879Types : ValueRef {
880 Value *D = getVal(Type::TypeTy, $1, true);
881 if (D == 0) ThrowException("Invalid user defined type: " + $1.getName());
Chris Lattner4cee8d82001-06-27 23:41:11 +0000882
883 // User defined type not in const pool!
884 ConstPoolType *CPT = (ConstPoolType*)D->castConstantAsserting();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000885 $$ = CPT->getValue();
886 }
Chris Lattner42b5a8a2001-07-25 22:47:46 +0000887 | TypesV '(' ArgTypeList ')' { // Method derived type?
Chris Lattner2f7c9632001-06-06 20:29:01 +0000888 MethodType::ParamTypes Params($3->begin(), $3->end());
889 delete $3;
Chris Lattnerd9c40e32001-07-09 19:38:36 +0000890 $$ = checkNewType(MethodType::getMethodType($1, Params));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000891 }
892 | TypesV '(' ')' { // Method derived type?
893 MethodType::ParamTypes Params; // Empty list
Chris Lattnerd9c40e32001-07-09 19:38:36 +0000894 $$ = checkNewType(MethodType::getMethodType($1, Params));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000895 }
896 | '[' Types ']' {
Chris Lattnerd9c40e32001-07-09 19:38:36 +0000897 $$ = checkNewType(ArrayType::getArrayType($2));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000898 }
899 | '[' EUINT64VAL 'x' Types ']' {
Chris Lattnerd9c40e32001-07-09 19:38:36 +0000900 $$ = checkNewType(ArrayType::getArrayType($4, (int)$2));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000901 }
902 | '{' TypeList '}' {
903 StructType::ElementTypes Elements($2->begin(), $2->end());
904 delete $2;
Chris Lattnerd9c40e32001-07-09 19:38:36 +0000905 $$ = checkNewType(StructType::getStructType(Elements));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000906 }
907 | '{' '}' {
Chris Lattnerd9c40e32001-07-09 19:38:36 +0000908 $$ = checkNewType(StructType::getStructType(StructType::ElementTypes()));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000909 }
910 | Types '*' {
Chris Lattnerd9c40e32001-07-09 19:38:36 +0000911 $$ = checkNewType(PointerType::getPointerType($1));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000912 }
913
Chris Lattner26e50dc2001-07-28 17:48:55 +0000914// TypeList - Used for struct declarations and as a basis for method type
915// declaration type lists
916//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000917TypeList : Types {
918 $$ = new list<const Type*>();
919 $$->push_back($1);
920 }
921 | TypeList ',' Types {
922 ($$=$1)->push_back($3);
923 }
924
Chris Lattner26e50dc2001-07-28 17:48:55 +0000925// ArgTypeList - List of types for a method type declaration...
Chris Lattner42b5a8a2001-07-25 22:47:46 +0000926ArgTypeList : TypeList
927 | TypeList ',' DOTDOTDOT {
928 ($$=$1)->push_back(Type::VoidTy);
929 }
Chris Lattner26e50dc2001-07-28 17:48:55 +0000930 | DOTDOTDOT {
931 ($$ = new list<const Type*>())->push_back(Type::VoidTy);
932 }
Chris Lattner42b5a8a2001-07-25 22:47:46 +0000933
Chris Lattner2f7c9632001-06-06 20:29:01 +0000934
935BasicBlockList : BasicBlockList BasicBlock {
936 $1->getBasicBlocks().push_back($2);
937 $$ = $1;
938 }
939 | MethodHeader BasicBlock { // Do not allow methods with 0 basic blocks
940 $$ = $1; // in them...
941 $1->getBasicBlocks().push_back($2);
942 }
943
944
945// Basic blocks are terminated by branching instructions:
946// br, br/cc, switch, ret
947//
948BasicBlock : InstructionList BBTerminatorInst {
949 $1->getInstList().push_back($2);
950 InsertValue($1);
951 $$ = $1;
952 }
953 | LABELSTR InstructionList BBTerminatorInst {
954 $2->getInstList().push_back($3);
955 $2->setName($1);
956 free($1); // Free the strdup'd memory...
957
958 InsertValue($2);
959 $$ = $2;
960 }
961
962InstructionList : InstructionList Inst {
963 $1->getInstList().push_back($2);
964 $$ = $1;
965 }
966 | /* empty */ {
967 $$ = new BasicBlock();
968 }
969
Chris Lattner252afba2001-07-26 16:29:15 +0000970BBTerminatorInst : RET ResolvedVal { // Return with a result...
971 $$ = new ReturnInst($2);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000972 }
973 | RET VOID { // Return with no result...
974 $$ = new ReturnInst();
975 }
976 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner252afba2001-07-26 16:29:15 +0000977 $$ = new BranchInst(getVal(Type::LabelTy, $3)->castBasicBlockAsserting());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000978 } // Conditional Branch...
979 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner252afba2001-07-26 16:29:15 +0000980 $$ = new BranchInst(getVal(Type::LabelTy, $6)->castBasicBlockAsserting(),
981 getVal(Type::LabelTy, $9)->castBasicBlockAsserting(),
Chris Lattner2f7c9632001-06-06 20:29:01 +0000982 getVal(Type::BoolTy, $3));
983 }
984 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
985 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner252afba2001-07-26 16:29:15 +0000986 getVal(Type::LabelTy, $6)->castBasicBlockAsserting());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000987 $$ = S;
988
989 list<pair<ConstPoolVal*, BasicBlock*> >::iterator I = $8->begin(),
990 end = $8->end();
Chris Lattner4cee8d82001-06-27 23:41:11 +0000991 for (; I != end; ++I)
Chris Lattner2f7c9632001-06-06 20:29:01 +0000992 S->dest_push_back(I->first, I->second);
993 }
994
995JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
996 $$ = $1;
Chris Lattner252afba2001-07-26 16:29:15 +0000997 ConstPoolVal *V = getVal($2, $3, true)->castConstantAsserting();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000998 if (V == 0)
999 ThrowException("May only switch on a constant pool value!");
1000
Chris Lattner252afba2001-07-26 16:29:15 +00001001 $$->push_back(make_pair(V, getVal($5, $6)->castBasicBlockAsserting()));
Chris Lattner2f7c9632001-06-06 20:29:01 +00001002 }
1003 | IntType ConstValueRef ',' LABEL ValueRef {
1004 $$ = new list<pair<ConstPoolVal*, BasicBlock*> >();
Chris Lattner252afba2001-07-26 16:29:15 +00001005 ConstPoolVal *V = getVal($1, $2, true)->castConstantAsserting();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001006
1007 if (V == 0)
1008 ThrowException("May only switch on a constant pool value!");
1009
Chris Lattner252afba2001-07-26 16:29:15 +00001010 $$->push_back(make_pair(V, getVal($4, $5)->castBasicBlockAsserting()));
Chris Lattner2f7c9632001-06-06 20:29:01 +00001011 }
1012
1013Inst : OptAssign InstVal {
1014 if ($1) // Is this definition named??
1015 $2->setName($1); // if so, assign the name...
1016
1017 InsertValue($2);
1018 $$ = $2;
1019}
1020
Chris Lattner931ef3b2001-06-11 15:04:20 +00001021PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
1022 $$ = new list<pair<Value*, BasicBlock*> >();
1023 $$->push_back(make_pair(getVal($1, $3),
Chris Lattner252afba2001-07-26 16:29:15 +00001024 getVal(Type::LabelTy, $5)->castBasicBlockAsserting()));
Chris Lattner931ef3b2001-06-11 15:04:20 +00001025 }
1026 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1027 $$ = $1;
1028 $1->push_back(make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner252afba2001-07-26 16:29:15 +00001029 getVal(Type::LabelTy, $6)->castBasicBlockAsserting()));
Chris Lattner931ef3b2001-06-11 15:04:20 +00001030 }
1031
1032
Chris Lattner252afba2001-07-26 16:29:15 +00001033ValueRefList : ResolvedVal { // Used for call statements...
Chris Lattner2f7c9632001-06-06 20:29:01 +00001034 $$ = new list<Value*>();
Chris Lattner252afba2001-07-26 16:29:15 +00001035 $$->push_back($1);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001036 }
Chris Lattner252afba2001-07-26 16:29:15 +00001037 | ValueRefList ',' ResolvedVal {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001038 $$ = $1;
Chris Lattner252afba2001-07-26 16:29:15 +00001039 $1->push_back($3);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001040 }
1041
1042// ValueRefListE - Just like ValueRefList, except that it may also be empty!
1043ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; }
1044
1045InstVal : BinaryOps Types ValueRef ',' ValueRef {
Chris Lattner31e23cd2001-06-25 07:31:31 +00001046 $$ = BinaryOperator::create($1, getVal($2, $3), getVal($2, $5));
Chris Lattner2f7c9632001-06-06 20:29:01 +00001047 if ($$ == 0)
1048 ThrowException("binary operator returned null!");
1049 }
Chris Lattner252afba2001-07-26 16:29:15 +00001050 | UnaryOps ResolvedVal {
1051 $$ = UnaryOperator::create($1, $2);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001052 if ($$ == 0)
1053 ThrowException("unary operator returned null!");
Chris Lattnera6821822001-07-08 04:57:15 +00001054 }
Chris Lattner252afba2001-07-26 16:29:15 +00001055 | ShiftOps ResolvedVal ',' ResolvedVal {
1056 if ($4->getType() != Type::UByteTy)
1057 ThrowException("Shift amount must be ubyte!");
1058 $$ = new ShiftInst($1, $2, $4);
Chris Lattnerd8bebcd2001-07-08 21:10:27 +00001059 }
Chris Lattner252afba2001-07-26 16:29:15 +00001060 | CAST ResolvedVal TO Types {
1061 $$ = new CastInst($2, $4);
Chris Lattnera6821822001-07-08 04:57:15 +00001062 }
Chris Lattner931ef3b2001-06-11 15:04:20 +00001063 | PHI PHIList {
1064 const Type *Ty = $2->front().first->getType();
1065 $$ = new PHINode(Ty);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001066 while ($2->begin() != $2->end()) {
Chris Lattner931ef3b2001-06-11 15:04:20 +00001067 if ($2->front().first->getType() != Ty)
1068 ThrowException("All elements of a PHI node must be of the same type!");
1069 ((PHINode*)$$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001070 $2->pop_front();
1071 }
1072 delete $2; // Free the list...
1073 }
Chris Lattner26e50dc2001-07-28 17:48:55 +00001074 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattner42b5a8a2001-07-25 22:47:46 +00001075 const MethodType *Ty;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001076
Chris Lattner42b5a8a2001-07-25 22:47:46 +00001077 if (!(Ty = $2->isMethodType())) {
1078 // Pull out the types of all of the arguments...
1079 vector<const Type*> ParamTypes;
1080 for (list<Value*>::iterator I = $5->begin(), E = $5->end(); I != E; ++I)
1081 ParamTypes.push_back((*I)->getType());
1082 Ty = MethodType::get($2, ParamTypes);
1083 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001084
Chris Lattner42b5a8a2001-07-25 22:47:46 +00001085 Value *V = getVal(Ty, $3); // Get the method we're calling...
Chris Lattner2f7c9632001-06-06 20:29:01 +00001086
Chris Lattner42b5a8a2001-07-25 22:47:46 +00001087 // Create the call node...
1088 if (!$5) { // Has no arguments?
1089 $$ = new CallInst(V->castMethodAsserting(), vector<Value*>());
1090 } else { // Has arguments?
Chris Lattner2f7c9632001-06-06 20:29:01 +00001091 // Loop through MethodType's arguments and ensure they are specified
1092 // correctly!
1093 //
1094 MethodType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
Chris Lattner42b5a8a2001-07-25 22:47:46 +00001095 MethodType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
1096 list<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
1097
1098 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1099 if ((*ArgI)->getType() != *I)
1100 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner2f7c9632001-06-06 20:29:01 +00001101 (*I)->getName() + "'!");
Chris Lattner2f7c9632001-06-06 20:29:01 +00001102
Chris Lattner42b5a8a2001-07-25 22:47:46 +00001103 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner2f7c9632001-06-06 20:29:01 +00001104 ThrowException("Invalid number of parameters detected!");
Chris Lattner2f7c9632001-06-06 20:29:01 +00001105
Chris Lattner42b5a8a2001-07-25 22:47:46 +00001106 $$ = new CallInst(V->castMethodAsserting(),
1107 vector<Value*>($5->begin(), $5->end()));
1108 }
1109 delete $5;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001110 }
1111 | MemoryInst {
1112 $$ = $1;
1113 }
1114
Chris Lattnerd8bebcd2001-07-08 21:10:27 +00001115// UByteList - List of ubyte values for load and store instructions
1116UByteList : ',' ConstVector {
1117 $$ = $2;
1118} | /* empty */ {
1119 $$ = new vector<ConstPoolVal*>();
1120}
1121
Chris Lattner2f7c9632001-06-06 20:29:01 +00001122MemoryInst : MALLOC Types {
Chris Lattnerd9c40e32001-07-09 19:38:36 +00001123 $$ = new MallocInst(checkNewType(PointerType::getPointerType($2)));
Chris Lattner2f7c9632001-06-06 20:29:01 +00001124 }
1125 | MALLOC Types ',' UINT ValueRef {
1126 if (!$2->isArrayType() || ((const ArrayType*)$2)->isSized())
1127 ThrowException("Trying to allocate " + $2->getName() +
1128 " as unsized array!");
Chris Lattnerd9c40e32001-07-09 19:38:36 +00001129 const Type *Ty = checkNewType(PointerType::getPointerType($2));
1130 $$ = new MallocInst(Ty, getVal($4, $5));
Chris Lattner2f7c9632001-06-06 20:29:01 +00001131 }
1132 | ALLOCA Types {
Chris Lattnerd9c40e32001-07-09 19:38:36 +00001133 $$ = new AllocaInst(checkNewType(PointerType::getPointerType($2)));
Chris Lattner2f7c9632001-06-06 20:29:01 +00001134 }
1135 | ALLOCA Types ',' UINT ValueRef {
1136 if (!$2->isArrayType() || ((const ArrayType*)$2)->isSized())
1137 ThrowException("Trying to allocate " + $2->getName() +
1138 " as unsized array!");
Chris Lattnerd9c40e32001-07-09 19:38:36 +00001139 const Type *Ty = checkNewType(PointerType::getPointerType($2));
Chris Lattner2f7c9632001-06-06 20:29:01 +00001140 Value *ArrSize = getVal($4, $5);
Chris Lattner37099992001-07-07 08:36:30 +00001141 $$ = new AllocaInst(Ty, ArrSize);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001142 }
Chris Lattner252afba2001-07-26 16:29:15 +00001143 | FREE ResolvedVal {
1144 if (!$2->getType()->isPointerType())
1145 ThrowException("Trying to free nonpointer type " +
1146 $2->getType()->getName() + "!");
1147 $$ = new FreeInst($2);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001148 }
1149
Chris Lattnerd8bebcd2001-07-08 21:10:27 +00001150 | LOAD Types ValueRef UByteList {
1151 if (!$2->isPointerType())
1152 ThrowException("Can't load from nonpointer type: " + $2->getName());
1153 if (LoadInst::getIndexedType($2, *$4) == 0)
1154 ThrowException("Invalid indices for load instruction!");
1155
1156 $$ = new LoadInst(getVal($2, $3), *$4);
1157 delete $4; // Free the vector...
1158 }
Chris Lattner252afba2001-07-26 16:29:15 +00001159 | STORE ResolvedVal ',' Types ValueRef UByteList {
1160 if (!$4->isPointerType())
1161 ThrowException("Can't store to a nonpointer type: " + $4->getName());
1162 const Type *ElTy = StoreInst::getIndexedType($4, *$6);
Chris Lattner62ecb4a2001-07-08 23:22:50 +00001163 if (ElTy == 0)
1164 ThrowException("Can't store into that field list!");
Chris Lattner252afba2001-07-26 16:29:15 +00001165 if (ElTy != $2->getType())
1166 ThrowException("Can't store '" + $2->getType()->getName() +
1167 "' into space of type '" + ElTy->getName() + "'!");
1168 $$ = new StoreInst($2, getVal($4, $5), *$6);
1169 delete $6;
Chris Lattner62ecb4a2001-07-08 23:22:50 +00001170 }
1171 | GETELEMENTPTR Types ValueRef UByteList {
1172 if (!$2->isPointerType())
1173 ThrowException("getelementptr insn requires pointer operand!");
1174 if (!GetElementPtrInst::getIndexedType($2, *$4, true))
1175 ThrowException("Can't get element ptr '" + $2->getName() + "'!");
1176 $$ = new GetElementPtrInst(getVal($2, $3), *$4);
1177 delete $4;
Chris Lattnerd9c40e32001-07-09 19:38:36 +00001178 checkNewType($$->getType());
Chris Lattner62ecb4a2001-07-08 23:22:50 +00001179 }
Chris Lattnerd8bebcd2001-07-08 21:10:27 +00001180
Chris Lattner2f7c9632001-06-06 20:29:01 +00001181%%
Chris Lattnera6821822001-07-08 04:57:15 +00001182int yyerror(const char *ErrorMsg) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001183 ThrowException(string("Parse error: ") + ErrorMsg);
1184 return 0;
1185}