blob: e3084ba64b53d5f57cde2c384ce78ee079b3ee92 [file] [log] [blame]
Chris Lattner00950542001-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 Lattner8896eda2001-07-09 19:38:36 +000027#include <algorithm> // Get definition of find_if
Chris Lattner00950542001-06-06 20:29:01 +000028#include <stdio.h> // This embarasment is due to our flex lexer...
29
Chris Lattner09083092001-07-08 04:57:15 +000030int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
31int yylex(); // declaration" of xxx warnings.
Chris Lattner00950542001-06-06 20:29:01 +000032int yyparse();
33
34static Module *ParserResult;
Chris Lattnera2850432001-07-22 18:36:00 +000035string CurFilename;
Chris Lattner00950542001-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 Lattnere1815642001-07-15 06:35:53 +000061 vector<ValueList> Values; // Keep track of numbered definitions
Chris Lattner00950542001-06-06 20:29:01 +000062 vector<ValueList> LateResolveValues;
Chris Lattnere1815642001-07-15 06:35:53 +000063 bool isDeclare; // Is this method a forward declararation?
Chris Lattner00950542001-06-06 20:29:01 +000064
65 inline PerMethodInfo() {
66 CurrentMethod = 0;
Chris Lattnere1815642001-07-15 06:35:53 +000067 isDeclare = false;
Chris Lattner00950542001-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 Lattnere1815642001-07-15 06:35:53 +000083 isDeclare = false;
Chris Lattner00950542001-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
92static void InsertValue(Value *D, vector<ValueList> &ValueTab = CurMeth.Values) {
93 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
102static Value *getVal(const Type *Type, ValID &D,
103 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 Lattner3d52b2f2001-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 Lattner00950542001-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 Lattner3d52b2f2001-07-15 00:17:01 +0000159 ThrowException("Symbolic constant pool value '" +
160 itostr(D.ConstPool64) + "' is invalid for type '" +
161 Type->getName() + "'!");
Chris Lattner00950542001-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 Lattner3d52b2f2001-07-15 00:17:01 +0000168 ThrowException("Integral constant pool reference is invalid!");
Chris Lattner00950542001-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();
179 //CPV = new ConstPoolString(D.Name);
180 D.destroy(); // Free the string memory
181 break;
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000182 case 5:
183 if (!ConstPoolFP::isValueValidForType(Type, D.ConstPoolFP))
184 ThrowException("FP constant invalid for type!!");
185 else
186 CPV = new ConstPoolFP(Type, D.ConstPoolFP);
187 break;
Chris Lattner00950542001-06-06 20:29:01 +0000188 }
189 assert(CPV && "How did we escape creating a constant??");
190
191 // Scan through the constant table and see if we already have loaded this
192 // constant.
193 //
194 ConstantPool &CP = CurMeth.CurrentMethod ?
195 CurMeth.CurrentMethod->getConstantPool() :
196 CurModule.CurrentModule->getConstantPool();
197 ConstPoolVal *C = CP.find(CPV); // Already have this constant?
198 if (C) {
199 delete CPV; // Didn't need this after all, oh well.
200 return C; // Yup, we already have one, recycle it!
201 }
202 CP.insert(CPV);
203
204 // Success, everything is kosher. Lets go!
205 return CPV;
206 } // End of case 2,3,4
207 } // End of switch
208
209
210 // If we reached here, we referenced either a symbol that we don't know about
211 // or an id number that hasn't been read yet. We may be referencing something
212 // forward, so just create an entry to be resolved later and get to it...
213 //
214 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
215
216 // TODO: Attempt to coallecse nodes that are the same with previous ones.
217 Value *d = 0;
218 switch (Type->getPrimitiveID()) {
219 case Type::LabelTyID: d = new BBPlaceHolder(Type, D); break;
220 case Type::MethodTyID:
221 d = new MethPlaceHolder(Type, D);
222 InsertValue(d, CurModule.LateResolveValues);
223 return d;
224//case Type::ClassTyID: d = new ClassPlaceHolder(Type, D); break;
225 default: d = new DefPlaceHolder(Type, D); break;
226 }
227
228 assert(d != 0 && "How did we not make something?");
229 InsertValue(d, CurMeth.LateResolveValues);
230 return d;
231}
232
233
234//===----------------------------------------------------------------------===//
235// Code to handle forward references in instructions
236//===----------------------------------------------------------------------===//
237//
238// This code handles the late binding needed with statements that reference
239// values not defined yet... for example, a forward branch, or the PHI node for
240// a loop body.
241//
242// This keeps a table (CurMeth.LateResolveValues) of all such forward references
243// and back patchs after we are done.
244//
245
246// ResolveDefinitions - If we could not resolve some defs at parsing
247// time (forward branches, phi functions for loops, etc...) resolve the
248// defs now...
249//
250static void ResolveDefinitions(vector<ValueList> &LateResolvers) {
251 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
252 for (unsigned ty = 0; ty < LateResolvers.size(); ty++) {
253 while (!LateResolvers[ty].empty()) {
254 Value *V = LateResolvers[ty].back();
255 LateResolvers[ty].pop_back();
256 ValID &DID = getValIDFromPlaceHolder(V);
257
258 Value *TheRealValue = getVal(Type::getUniqueIDType(ty), DID, true);
259
260 if (TheRealValue == 0 && DID.Type == 1)
261 ThrowException("Reference to an invalid definition: '" +DID.getName() +
262 "' of type '" + V->getType()->getName() + "'");
263 else if (TheRealValue == 0)
264 ThrowException("Reference to an invalid definition: #" +itostr(DID.Num)+
265 " of type '" + V->getType()->getName() + "'");
266
267 V->replaceAllUsesWith(TheRealValue);
268 assert(V->use_empty());
269 delete V;
270 }
271 }
272
273 LateResolvers.clear();
274}
275
276// addConstValToConstantPool - This code is used to insert a constant into the
277// current constant pool. This is designed to make maximal (but not more than
278// possible) reuse (merging) of constants in the constant pool. This means that
279// multiple references to %4, for example will all get merged.
280//
281static ConstPoolVal *addConstValToConstantPool(ConstPoolVal *C) {
282 vector<ValueList> &ValTab = CurMeth.CurrentMethod ?
283 CurMeth.Values : CurModule.Values;
284 ConstantPool &CP = CurMeth.CurrentMethod ?
285 CurMeth.CurrentMethod->getConstantPool() :
286 CurModule.CurrentModule->getConstantPool();
287
288 if (ConstPoolVal *CPV = CP.find(C)) {
289 // Constant already in constant pool. Try to merge the two constants
290 if (CPV->hasName() && !C->hasName()) {
291 // Merge the two values, we inherit the existing CPV's name.
292 // InsertValue requires that the value have no name to insert correctly
293 // (because we want to fill the slot this constant would have filled)
294 //
295 string Name = CPV->getName();
296 CPV->setName("");
297 InsertValue(CPV, ValTab);
298 CPV->setName(Name);
299 delete C;
300 return CPV;
301 } else if (!CPV->hasName() && C->hasName()) {
302 // If we have a name on this value and there isn't one in the const
303 // pool val already, propogate it.
304 //
305 CPV->setName(C->getName());
306 delete C; // Sorry, you're toast
307 return CPV;
308 } else if (CPV->hasName() && C->hasName()) {
309 // Both values have distinct names. We cannot merge them.
310 CP.insert(C);
311 InsertValue(C, ValTab);
312 return C;
313 } else if (!CPV->hasName() && !C->hasName()) {
314 // Neither value has a name, trivially merge them.
315 InsertValue(CPV, ValTab);
316 delete C;
317 return CPV;
318 }
319
320 assert(0 && "Not reached!");
321 return 0;
322 } else { // No duplication of value.
323 CP.insert(C);
324 InsertValue(C, ValTab);
325 return C;
326 }
327}
328
Chris Lattner8896eda2001-07-09 19:38:36 +0000329
330struct EqualsType {
331 const Type *T;
332 inline EqualsType(const Type *t) { T = t; }
333 inline bool operator()(const ConstPoolVal *CPV) const {
334 return static_cast<const ConstPoolType*>(CPV)->getValue() == T;
335 }
336};
337
338
339// checkNewType - We have to be careful to add all types referenced by the
340// program to the constant pool of the method or module. Because of this, we
341// often want to check to make sure that types used are in the constant pool,
342// and add them if they aren't. That's what this function does.
343//
344static const Type *checkNewType(const Type *Ty) {
345 ConstantPool &CP = CurMeth.CurrentMethod ?
346 CurMeth.CurrentMethod->getConstantPool() :
347 CurModule.CurrentModule->getConstantPool();
348
Chris Lattner698b56e2001-07-20 19:15:08 +0000349 // TODO: This should use ConstantPool::ensureTypeAvailable
350
Chris Lattner8896eda2001-07-09 19:38:36 +0000351 // Get the type type plane...
352 ConstantPool::PlaneType &P = CP.getPlane(Type::TypeTy);
353 ConstantPool::PlaneType::const_iterator PI = find_if(P.begin(), P.end(),
354 EqualsType(Ty));
355 if (PI == P.end()) {
356 vector<ValueList> &ValTab = CurMeth.CurrentMethod ?
357 CurMeth.Values : CurModule.Values;
358 ConstPoolVal *CPT = new ConstPoolType(Ty);
359 CP.insert(CPT);
360 InsertValue(CPT, ValTab);
361 }
362 return Ty;
363}
364
365
Chris Lattner00950542001-06-06 20:29:01 +0000366//===----------------------------------------------------------------------===//
367// RunVMAsmParser - Define an interface to this parser
368//===----------------------------------------------------------------------===//
369//
Chris Lattnera2850432001-07-22 18:36:00 +0000370Module *RunVMAsmParser(const string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000371 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000372 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000373 llvmAsmlineno = 1; // Reset the current line number...
374
375 CurModule.CurrentModule = new Module(); // Allocate a new module to read
376 yyparse(); // Parse the file.
377 Module *Result = ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +0000378 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
379 ParserResult = 0;
380
381 return Result;
382}
383
384%}
385
386%union {
387 Module *ModuleVal;
388 Method *MethodVal;
389 MethodArgument *MethArgVal;
390 BasicBlock *BasicBlockVal;
391 TerminatorInst *TermInstVal;
392 Instruction *InstVal;
393 ConstPoolVal *ConstVal;
394 const Type *TypeVal;
395
396 list<MethodArgument*> *MethodArgList;
397 list<Value*> *ValueList;
398 list<const Type*> *TypeList;
Chris Lattnerc24d2082001-06-11 15:04:20 +0000399 list<pair<Value*, BasicBlock*> > *PHIList; // Represent the RHS of PHI node
Chris Lattner00950542001-06-06 20:29:01 +0000400 list<pair<ConstPoolVal*, BasicBlock*> > *JumpTable;
401 vector<ConstPoolVal*> *ConstVector;
402
403 int64_t SInt64Val;
404 uint64_t UInt64Val;
405 int SIntVal;
406 unsigned UIntVal;
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000407 double FPVal;
Chris Lattner00950542001-06-06 20:29:01 +0000408
409 char *StrVal; // This memory is allocated by strdup!
410 ValID ValIDVal; // May contain memory allocated by strdup
411
412 Instruction::UnaryOps UnaryOpVal;
413 Instruction::BinaryOps BinaryOpVal;
414 Instruction::TermOps TermOpVal;
415 Instruction::MemoryOps MemOpVal;
Chris Lattner027dcc52001-07-08 21:10:27 +0000416 Instruction::OtherOps OtherOpVal;
Chris Lattner00950542001-06-06 20:29:01 +0000417}
418
419%type <ModuleVal> Module MethodList
Chris Lattnere1815642001-07-15 06:35:53 +0000420%type <MethodVal> Method MethodProto MethodHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000421%type <BasicBlockVal> BasicBlock InstructionList
422%type <TermInstVal> BBTerminatorInst
423%type <InstVal> Inst InstVal MemoryInst
424%type <ConstVal> ConstVal
Chris Lattner027dcc52001-07-08 21:10:27 +0000425%type <ConstVector> ConstVector UByteList
Chris Lattner00950542001-06-06 20:29:01 +0000426%type <MethodArgList> ArgList ArgListH
427%type <MethArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000428%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000429%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner8b81bf52001-07-25 22:47:46 +0000430%type <TypeList> TypeList ArgTypeList
Chris Lattner00950542001-06-06 20:29:01 +0000431%type <JumpTable> JumpTable
432
433%type <ValIDVal> ValueRef ConstValueRef // Reference to a definition or BB
434
435// Tokens and types for handling constant integer values
436//
437// ESINT64VAL - A negative number within long long range
438%token <SInt64Val> ESINT64VAL
439
440// EUINT64VAL - A positive number within uns. long long range
441%token <UInt64Val> EUINT64VAL
442%type <SInt64Val> EINT64VAL
443
444%token <SIntVal> SINTVAL // Signed 32 bit ints...
445%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
446%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000447%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000448
449// Built in types...
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000450%type <TypeVal> Types TypesV SIntType UIntType IntType FPType
Chris Lattner00950542001-06-06 20:29:01 +0000451%token <TypeVal> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
452%token <TypeVal> FLOAT DOUBLE STRING TYPE LABEL
453
454%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
455%type <StrVal> OptVAR_ID OptAssign
456
457
Chris Lattner8b81bf52001-07-25 22:47:46 +0000458%token IMPLEMENTATION TRUE FALSE BEGINTOK END DECLARE TO DOTDOTDOT
Chris Lattner00950542001-06-06 20:29:01 +0000459
460// Basic Block Terminating Operators
461%token <TermOpVal> RET BR SWITCH
462
463// Unary Operators
464%type <UnaryOpVal> UnaryOps // all the unary operators
Chris Lattner71496b32001-07-08 19:03:27 +0000465%token <UnaryOpVal> NOT
Chris Lattner00950542001-06-06 20:29:01 +0000466
467// Binary Operators
468%type <BinaryOpVal> BinaryOps // all the binary operators
469%token <BinaryOpVal> ADD SUB MUL DIV REM
Chris Lattner027dcc52001-07-08 21:10:27 +0000470%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000471
472// Memory Instructions
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000473%token <MemoryOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000474
Chris Lattner027dcc52001-07-08 21:10:27 +0000475// Other Operators
476%type <OtherOpVal> ShiftOps
477%token <OtherOpVal> PHI CALL CAST SHL SHR
478
Chris Lattner00950542001-06-06 20:29:01 +0000479%start Module
480%%
481
482// Handle constant integer size restriction and conversion...
483//
484
485INTVAL : SINTVAL
486INTVAL : UINTVAL {
487 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
488 ThrowException("Value too large for type!");
489 $$ = (int32_t)$1;
490}
491
492
493EINT64VAL : ESINT64VAL // These have same type and can't cause problems...
494EINT64VAL : EUINT64VAL {
495 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
496 ThrowException("Value too large for type!");
497 $$ = (int64_t)$1;
498}
499
500// Types includes all predefined types... except void, because you can't do
501// anything with it except for certain specific things...
502//
Chris Lattnere98dda62001-07-14 06:10:16 +0000503// User defined types are added later...
Chris Lattner00950542001-06-06 20:29:01 +0000504//
505Types : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT
506Types : LONG | ULONG | FLOAT | DOUBLE | STRING | TYPE | LABEL
507
508// TypesV includes all of 'Types', but it also includes the void type.
509TypesV : Types | VOID
510
511// Operations that are notably excluded from this list include:
512// RET, BR, & SWITCH because they end basic blocks and are treated specially.
513//
Chris Lattner09083092001-07-08 04:57:15 +0000514UnaryOps : NOT
Chris Lattner00950542001-06-06 20:29:01 +0000515BinaryOps : ADD | SUB | MUL | DIV | REM
516BinaryOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE
Chris Lattner027dcc52001-07-08 21:10:27 +0000517ShiftOps : SHL | SHR
Chris Lattner00950542001-06-06 20:29:01 +0000518
Chris Lattnere98dda62001-07-14 06:10:16 +0000519// These are some types that allow classification if we only want a particular
520// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner00950542001-06-06 20:29:01 +0000521SIntType : LONG | INT | SHORT | SBYTE
522UIntType : ULONG | UINT | USHORT | UBYTE
523IntType : SIntType | UIntType
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000524FPType : FLOAT | DOUBLE
Chris Lattner00950542001-06-06 20:29:01 +0000525
Chris Lattnere98dda62001-07-14 06:10:16 +0000526// OptAssign - Value producing statements have an optional assignment component
Chris Lattner00950542001-06-06 20:29:01 +0000527OptAssign : VAR_ID '=' {
528 $$ = $1;
529 }
530 | /*empty*/ {
531 $$ = 0;
532 }
533
Chris Lattnere98dda62001-07-14 06:10:16 +0000534// ConstVal - The various declarations that go into the constant pool. This
535// includes all forward declarations of types, constants, and functions.
536//
Chris Lattner00950542001-06-06 20:29:01 +0000537ConstVal : SIntType EINT64VAL { // integral constants
538 if (!ConstPoolSInt::isValueValidForType($1, $2))
539 ThrowException("Constant value doesn't fit in type!");
540 $$ = new ConstPoolSInt($1, $2);
541 }
542 | UIntType EUINT64VAL { // integral constants
543 if (!ConstPoolUInt::isValueValidForType($1, $2))
544 ThrowException("Constant value doesn't fit in type!");
545 $$ = new ConstPoolUInt($1, $2);
546 }
547 | BOOL TRUE { // Boolean constants
548 $$ = new ConstPoolBool(true);
549 }
550 | BOOL FALSE { // Boolean constants
551 $$ = new ConstPoolBool(false);
552 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000553 | FPType FPVAL { // Float & Double constants
554 $$ = new ConstPoolFP($1, $2);
555 }
Chris Lattner00950542001-06-06 20:29:01 +0000556 | STRING STRINGCONSTANT { // String constants
557 cerr << "FIXME: TODO: String constants [sbyte] not implemented yet!\n";
558 abort();
559 //$$ = new ConstPoolString($2);
560 free($2);
561 }
562 | TYPE Types { // Type constants
563 $$ = new ConstPoolType($2);
564 }
565 | '[' Types ']' '[' ConstVector ']' { // Nonempty array constant
566 // Verify all elements are correct type!
567 const ArrayType *AT = ArrayType::getArrayType($2);
568 for (unsigned i = 0; i < $5->size(); i++) {
569 if ($2 != (*$5)[i]->getType())
570 ThrowException("Element #" + utostr(i) + " is not of type '" +
571 $2->getName() + "' as required!\nIt is of type '" +
572 (*$5)[i]->getType()->getName() + "'.");
573 }
574
575 $$ = new ConstPoolArray(AT, *$5);
576 delete $5;
577 }
578 | '[' Types ']' '[' ']' { // Empty array constant
579 vector<ConstPoolVal*> Empty;
580 $$ = new ConstPoolArray(ArrayType::getArrayType($2), Empty);
581 }
582 | '[' EUINT64VAL 'x' Types ']' '[' ConstVector ']' {
583 // Verify all elements are correct type!
584 const ArrayType *AT = ArrayType::getArrayType($4, (int)$2);
585 if ($2 != $7->size())
586 ThrowException("Type mismatch: constant sized array initialized with " +
587 utostr($7->size()) + " arguments, but has size of " +
588 itostr((int)$2) + "!");
589
590 for (unsigned i = 0; i < $7->size(); i++) {
591 if ($4 != (*$7)[i]->getType())
592 ThrowException("Element #" + utostr(i) + " is not of type '" +
593 $4->getName() + "' as required!\nIt is of type '" +
594 (*$7)[i]->getType()->getName() + "'.");
595 }
596
597 $$ = new ConstPoolArray(AT, *$7);
598 delete $7;
599 }
600 | '[' EUINT64VAL 'x' Types ']' '[' ']' {
601 if ($2 != 0)
602 ThrowException("Type mismatch: constant sized array initialized with 0"
603 " arguments, but has size of " + itostr((int)$2) + "!");
604 vector<ConstPoolVal*> Empty;
605 $$ = new ConstPoolArray(ArrayType::getArrayType($4, 0), Empty);
606 }
607 | '{' TypeList '}' '{' ConstVector '}' {
608 StructType::ElementTypes Types($2->begin(), $2->end());
609 delete $2;
610
611 const StructType *St = StructType::getStructType(Types);
612 $$ = new ConstPoolStruct(St, *$5);
613 delete $5;
614 }
615 | '{' '}' '{' '}' {
616 const StructType *St =
617 StructType::getStructType(StructType::ElementTypes());
618 vector<ConstPoolVal*> Empty;
619 $$ = new ConstPoolStruct(St, Empty);
620 }
621/*
622 | Types '*' ConstVal {
623 assert(0);
624 $$ = 0;
625 }
626*/
627
Chris Lattnere98dda62001-07-14 06:10:16 +0000628// ConstVector - A list of comma seperated constants.
Chris Lattner00950542001-06-06 20:29:01 +0000629ConstVector : ConstVector ',' ConstVal {
630 ($$ = $1)->push_back(addConstValToConstantPool($3));
631 }
632 | ConstVal {
633 $$ = new vector<ConstPoolVal*>();
634 $$->push_back(addConstValToConstantPool($1));
635 }
636
Chris Lattnere98dda62001-07-14 06:10:16 +0000637//ExternMethodDecl : EXTERNAL TypesV '(' TypeList ')' {
638// }
639//ExternVarDecl :
Chris Lattner00950542001-06-06 20:29:01 +0000640
Chris Lattnere98dda62001-07-14 06:10:16 +0000641// ConstPool - Constants with optional names assigned to them.
Chris Lattner00950542001-06-06 20:29:01 +0000642ConstPool : ConstPool OptAssign ConstVal {
643 if ($2) {
644 $3->setName($2);
645 free($2);
646 }
647
648 addConstValToConstantPool($3);
649 }
Chris Lattnere98dda62001-07-14 06:10:16 +0000650/*
651 | ConstPool OptAssign GlobalDecl { // Global declarations appear in CP
652 if ($2) {
653 $3->setName($2);
654 free($2);
655 }
656 //CurModule.CurrentModule->
657 }
658*/
Chris Lattner00950542001-06-06 20:29:01 +0000659 | /* empty: end of list */ {
660 }
661
662
663//===----------------------------------------------------------------------===//
664// Rules to match Modules
665//===----------------------------------------------------------------------===//
666
667// Module rule: Capture the result of parsing the whole file into a result
668// variable...
669//
670Module : MethodList {
671 $$ = ParserResult = $1;
672 CurModule.ModuleDone();
673}
674
Chris Lattnere98dda62001-07-14 06:10:16 +0000675// MethodList - A list of methods, preceeded by a constant pool.
676//
Chris Lattner00950542001-06-06 20:29:01 +0000677MethodList : MethodList Method {
Chris Lattner00950542001-06-06 20:29:01 +0000678 $$ = $1;
Chris Lattnere1815642001-07-15 06:35:53 +0000679 if (!$2->getParent())
680 $1->getMethodList().push_back($2);
681 CurMeth.MethodDone();
Chris Lattner00950542001-06-06 20:29:01 +0000682 }
Chris Lattnere1815642001-07-15 06:35:53 +0000683 | MethodList MethodProto {
684 $$ = $1;
685 if (!$2->getParent())
686 $1->getMethodList().push_back($2);
687 CurMeth.MethodDone();
688 }
Chris Lattner00950542001-06-06 20:29:01 +0000689 | ConstPool IMPLEMENTATION {
690 $$ = CurModule.CurrentModule;
691 }
692
693
694//===----------------------------------------------------------------------===//
695// Rules to match Method Headers
696//===----------------------------------------------------------------------===//
697
698OptVAR_ID : VAR_ID | /*empty*/ { $$ = 0; }
699
700ArgVal : Types OptVAR_ID {
701 $$ = new MethodArgument($1);
702 if ($2) { // Was the argument named?
703 $$->setName($2);
704 free($2); // The string was strdup'd, so free it now.
705 }
706}
707
708ArgListH : ArgVal ',' ArgListH {
709 $$ = $3;
710 $3->push_front($1);
711 }
712 | ArgVal {
713 $$ = new list<MethodArgument*>();
714 $$->push_front($1);
715 }
Chris Lattner8b81bf52001-07-25 22:47:46 +0000716 | DOTDOTDOT {
717 $$ = new list<MethodArgument*>();
718 $$->push_back(new MethodArgument(Type::VoidTy));
719 }
Chris Lattner00950542001-06-06 20:29:01 +0000720
721ArgList : ArgListH {
722 $$ = $1;
723 }
724 | /* empty */ {
725 $$ = 0;
726 }
727
728MethodHeaderH : TypesV STRINGCONSTANT '(' ArgList ')' {
729 MethodType::ParamTypes ParamTypeList;
730 if ($4)
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000731 for (list<MethodArgument*>::iterator I = $4->begin(); I != $4->end(); ++I)
Chris Lattner00950542001-06-06 20:29:01 +0000732 ParamTypeList.push_back((*I)->getType());
733
734 const MethodType *MT = MethodType::getMethodType($1, ParamTypeList);
735
Chris Lattnere1815642001-07-15 06:35:53 +0000736 Method *M = 0;
737 if (SymbolTable *ST = CurModule.CurrentModule->getSymbolTable()) {
738 if (Value *V = ST->lookup(MT, $2)) { // Method already in symtab?
739 M = V->castMethodAsserting();
Chris Lattner00950542001-06-06 20:29:01 +0000740
Chris Lattnere1815642001-07-15 06:35:53 +0000741 // Yes it is. If this is the case, either we need to be a forward decl,
742 // or it needs to be.
743 if (!CurMeth.isDeclare && !M->isExternal())
744 ThrowException("Redefinition of method '" + string($2) + "'!");
745 }
746 }
747
748 if (M == 0) { // Not already defined?
749 M = new Method(MT, $2);
750 InsertValue(M, CurModule.Values);
751 }
752
753 free($2); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +0000754
755 CurMeth.MethodStart(M);
756
757 // Add all of the arguments we parsed to the method...
Chris Lattnere1815642001-07-15 06:35:53 +0000758 if ($4 && !CurMeth.isDeclare) { // Is null if empty...
Chris Lattner00950542001-06-06 20:29:01 +0000759 Method::ArgumentListType &ArgList = M->getArgumentList();
760
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000761 for (list<MethodArgument*>::iterator I = $4->begin(); I != $4->end(); ++I) {
Chris Lattner00950542001-06-06 20:29:01 +0000762 InsertValue(*I);
763 ArgList.push_back(*I);
764 }
765 delete $4; // We're now done with the argument list
766 }
767}
768
769MethodHeader : MethodHeaderH ConstPool BEGINTOK {
770 $$ = CurMeth.CurrentMethod;
771}
772
773Method : BasicBlockList END {
774 $$ = $1;
775}
776
Chris Lattnere1815642001-07-15 06:35:53 +0000777MethodProto : DECLARE { CurMeth.isDeclare = true; } MethodHeaderH {
778 $$ = CurMeth.CurrentMethod;
779}
Chris Lattner00950542001-06-06 20:29:01 +0000780
781//===----------------------------------------------------------------------===//
782// Rules to match Basic Blocks
783//===----------------------------------------------------------------------===//
784
785ConstValueRef : ESINT64VAL { // A reference to a direct constant
786 $$ = ValID::create($1);
787 }
788 | EUINT64VAL {
789 $$ = ValID::create($1);
790 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000791 | FPVAL { // Perhaps it's an FP constant?
792 $$ = ValID::create($1);
793 }
Chris Lattner00950542001-06-06 20:29:01 +0000794 | TRUE {
795 $$ = ValID::create((int64_t)1);
796 }
797 | FALSE {
798 $$ = ValID::create((int64_t)0);
799 }
800 | STRINGCONSTANT { // Quoted strings work too... especially for methods
801 $$ = ValID::create_conststr($1);
802 }
803
804// ValueRef - A reference to a definition...
805ValueRef : INTVAL { // Is it an integer reference...?
806 $$ = ValID::create($1);
807 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000808 | VAR_ID { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +0000809 $$ = ValID::create($1);
810 }
811 | ConstValueRef {
812 $$ = $1;
813 }
814
815// The user may refer to a user defined type by its typeplane... check for this
816// now...
817//
818Types : ValueRef {
819 Value *D = getVal(Type::TypeTy, $1, true);
820 if (D == 0) ThrowException("Invalid user defined type: " + $1.getName());
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000821
822 // User defined type not in const pool!
823 ConstPoolType *CPT = (ConstPoolType*)D->castConstantAsserting();
Chris Lattner00950542001-06-06 20:29:01 +0000824 $$ = CPT->getValue();
825 }
Chris Lattner8b81bf52001-07-25 22:47:46 +0000826 | TypesV '(' ArgTypeList ')' { // Method derived type?
Chris Lattner00950542001-06-06 20:29:01 +0000827 MethodType::ParamTypes Params($3->begin(), $3->end());
828 delete $3;
Chris Lattner8896eda2001-07-09 19:38:36 +0000829 $$ = checkNewType(MethodType::getMethodType($1, Params));
Chris Lattner00950542001-06-06 20:29:01 +0000830 }
831 | TypesV '(' ')' { // Method derived type?
832 MethodType::ParamTypes Params; // Empty list
Chris Lattner8896eda2001-07-09 19:38:36 +0000833 $$ = checkNewType(MethodType::getMethodType($1, Params));
Chris Lattner00950542001-06-06 20:29:01 +0000834 }
835 | '[' Types ']' {
Chris Lattner8896eda2001-07-09 19:38:36 +0000836 $$ = checkNewType(ArrayType::getArrayType($2));
Chris Lattner00950542001-06-06 20:29:01 +0000837 }
838 | '[' EUINT64VAL 'x' Types ']' {
Chris Lattner8896eda2001-07-09 19:38:36 +0000839 $$ = checkNewType(ArrayType::getArrayType($4, (int)$2));
Chris Lattner00950542001-06-06 20:29:01 +0000840 }
841 | '{' TypeList '}' {
842 StructType::ElementTypes Elements($2->begin(), $2->end());
843 delete $2;
Chris Lattner8896eda2001-07-09 19:38:36 +0000844 $$ = checkNewType(StructType::getStructType(Elements));
Chris Lattner00950542001-06-06 20:29:01 +0000845 }
846 | '{' '}' {
Chris Lattner8896eda2001-07-09 19:38:36 +0000847 $$ = checkNewType(StructType::getStructType(StructType::ElementTypes()));
Chris Lattner00950542001-06-06 20:29:01 +0000848 }
849 | Types '*' {
Chris Lattner8896eda2001-07-09 19:38:36 +0000850 $$ = checkNewType(PointerType::getPointerType($1));
Chris Lattner00950542001-06-06 20:29:01 +0000851 }
852
Chris Lattner00950542001-06-06 20:29:01 +0000853TypeList : Types {
854 $$ = new list<const Type*>();
855 $$->push_back($1);
856 }
857 | TypeList ',' Types {
858 ($$=$1)->push_back($3);
859 }
860
Chris Lattner8b81bf52001-07-25 22:47:46 +0000861ArgTypeList : TypeList
862 | TypeList ',' DOTDOTDOT {
863 ($$=$1)->push_back(Type::VoidTy);
864 }
865
Chris Lattner00950542001-06-06 20:29:01 +0000866
867BasicBlockList : BasicBlockList BasicBlock {
868 $1->getBasicBlocks().push_back($2);
869 $$ = $1;
870 }
871 | MethodHeader BasicBlock { // Do not allow methods with 0 basic blocks
872 $$ = $1; // in them...
873 $1->getBasicBlocks().push_back($2);
874 }
875
876
877// Basic blocks are terminated by branching instructions:
878// br, br/cc, switch, ret
879//
880BasicBlock : InstructionList BBTerminatorInst {
881 $1->getInstList().push_back($2);
882 InsertValue($1);
883 $$ = $1;
884 }
885 | LABELSTR InstructionList BBTerminatorInst {
886 $2->getInstList().push_back($3);
887 $2->setName($1);
888 free($1); // Free the strdup'd memory...
889
890 InsertValue($2);
891 $$ = $2;
892 }
893
894InstructionList : InstructionList Inst {
895 $1->getInstList().push_back($2);
896 $$ = $1;
897 }
898 | /* empty */ {
899 $$ = new BasicBlock();
900 }
901
902BBTerminatorInst : RET Types ValueRef { // Return with a result...
903 $$ = new ReturnInst(getVal($2, $3));
904 }
905 | RET VOID { // Return with no result...
906 $$ = new ReturnInst();
907 }
908 | BR LABEL ValueRef { // Unconditional Branch...
909 $$ = new BranchInst((BasicBlock*)getVal(Type::LabelTy, $3));
910 } // Conditional Branch...
911 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
912 $$ = new BranchInst((BasicBlock*)getVal(Type::LabelTy, $6),
913 (BasicBlock*)getVal(Type::LabelTy, $9),
914 getVal(Type::BoolTy, $3));
915 }
916 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
917 SwitchInst *S = new SwitchInst(getVal($2, $3),
918 (BasicBlock*)getVal(Type::LabelTy, $6));
919 $$ = S;
920
921 list<pair<ConstPoolVal*, BasicBlock*> >::iterator I = $8->begin(),
922 end = $8->end();
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000923 for (; I != end; ++I)
Chris Lattner00950542001-06-06 20:29:01 +0000924 S->dest_push_back(I->first, I->second);
925 }
926
927JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
928 $$ = $1;
929 ConstPoolVal *V = (ConstPoolVal*)getVal($2, $3, true);
930 if (V == 0)
931 ThrowException("May only switch on a constant pool value!");
932
933 $$->push_back(make_pair(V, (BasicBlock*)getVal($5, $6)));
934 }
935 | IntType ConstValueRef ',' LABEL ValueRef {
936 $$ = new list<pair<ConstPoolVal*, BasicBlock*> >();
937 ConstPoolVal *V = (ConstPoolVal*)getVal($1, $2, true);
938
939 if (V == 0)
940 ThrowException("May only switch on a constant pool value!");
941
942 $$->push_back(make_pair(V, (BasicBlock*)getVal($4, $5)));
943 }
944
945Inst : OptAssign InstVal {
946 if ($1) // Is this definition named??
947 $2->setName($1); // if so, assign the name...
948
949 InsertValue($2);
950 $$ = $2;
951}
952
Chris Lattnerc24d2082001-06-11 15:04:20 +0000953PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
954 $$ = new list<pair<Value*, BasicBlock*> >();
955 $$->push_back(make_pair(getVal($1, $3),
956 (BasicBlock*)getVal(Type::LabelTy, $5)));
957 }
958 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
959 $$ = $1;
960 $1->push_back(make_pair(getVal($1->front().first->getType(), $4),
961 (BasicBlock*)getVal(Type::LabelTy, $6)));
962 }
963
964
965ValueRefList : Types ValueRef { // Used for call statements...
Chris Lattner00950542001-06-06 20:29:01 +0000966 $$ = new list<Value*>();
967 $$->push_back(getVal($1, $2));
968 }
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000969 | ValueRefList ',' Types ValueRef {
Chris Lattner00950542001-06-06 20:29:01 +0000970 $$ = $1;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000971 $1->push_back(getVal($3, $4));
Chris Lattner00950542001-06-06 20:29:01 +0000972 }
973
974// ValueRefListE - Just like ValueRefList, except that it may also be empty!
975ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; }
976
977InstVal : BinaryOps Types ValueRef ',' ValueRef {
Chris Lattnerbebd60d2001-06-25 07:31:31 +0000978 $$ = BinaryOperator::create($1, getVal($2, $3), getVal($2, $5));
Chris Lattner00950542001-06-06 20:29:01 +0000979 if ($$ == 0)
980 ThrowException("binary operator returned null!");
981 }
982 | UnaryOps Types ValueRef {
Chris Lattnerbebd60d2001-06-25 07:31:31 +0000983 $$ = UnaryOperator::create($1, getVal($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +0000984 if ($$ == 0)
985 ThrowException("unary operator returned null!");
Chris Lattner09083092001-07-08 04:57:15 +0000986 }
Chris Lattner027dcc52001-07-08 21:10:27 +0000987 | ShiftOps Types ValueRef ',' Types ValueRef {
988 if ($5 != Type::UByteTy) ThrowException("Shift amount must be ubyte!");
989 $$ = new ShiftInst($1, getVal($2, $3), getVal($5, $6));
990 }
Chris Lattner09083092001-07-08 04:57:15 +0000991 | CAST Types ValueRef TO Types {
Chris Lattner71496b32001-07-08 19:03:27 +0000992 $$ = new CastInst(getVal($2, $3), $5);
Chris Lattner09083092001-07-08 04:57:15 +0000993 }
Chris Lattnerc24d2082001-06-11 15:04:20 +0000994 | PHI PHIList {
995 const Type *Ty = $2->front().first->getType();
996 $$ = new PHINode(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000997 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +0000998 if ($2->front().first->getType() != Ty)
999 ThrowException("All elements of a PHI node must be of the same type!");
1000 ((PHINode*)$$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001001 $2->pop_front();
1002 }
1003 delete $2; // Free the list...
1004 }
1005 | CALL Types ValueRef '(' ValueRefListE ')' {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001006 const MethodType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001007
Chris Lattner8b81bf52001-07-25 22:47:46 +00001008 if (!(Ty = $2->isMethodType())) {
1009 // Pull out the types of all of the arguments...
1010 vector<const Type*> ParamTypes;
1011 for (list<Value*>::iterator I = $5->begin(), E = $5->end(); I != E; ++I)
1012 ParamTypes.push_back((*I)->getType());
1013 Ty = MethodType::get($2, ParamTypes);
1014 }
Chris Lattner00950542001-06-06 20:29:01 +00001015
Chris Lattner8b81bf52001-07-25 22:47:46 +00001016 Value *V = getVal(Ty, $3); // Get the method we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001017
Chris Lattner8b81bf52001-07-25 22:47:46 +00001018 // Create the call node...
1019 if (!$5) { // Has no arguments?
1020 $$ = new CallInst(V->castMethodAsserting(), vector<Value*>());
1021 } else { // Has arguments?
Chris Lattner00950542001-06-06 20:29:01 +00001022 // Loop through MethodType's arguments and ensure they are specified
1023 // correctly!
1024 //
1025 MethodType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001026 MethodType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
1027 list<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
1028
1029 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1030 if ((*ArgI)->getType() != *I)
1031 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner00950542001-06-06 20:29:01 +00001032 (*I)->getName() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001033
Chris Lattner8b81bf52001-07-25 22:47:46 +00001034 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001035 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001036
Chris Lattner8b81bf52001-07-25 22:47:46 +00001037 $$ = new CallInst(V->castMethodAsserting(),
1038 vector<Value*>($5->begin(), $5->end()));
1039 }
1040 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001041 }
1042 | MemoryInst {
1043 $$ = $1;
1044 }
1045
Chris Lattner027dcc52001-07-08 21:10:27 +00001046// UByteList - List of ubyte values for load and store instructions
1047UByteList : ',' ConstVector {
1048 $$ = $2;
1049} | /* empty */ {
1050 $$ = new vector<ConstPoolVal*>();
1051}
1052
Chris Lattner00950542001-06-06 20:29:01 +00001053MemoryInst : MALLOC Types {
Chris Lattner8896eda2001-07-09 19:38:36 +00001054 $$ = new MallocInst(checkNewType(PointerType::getPointerType($2)));
Chris Lattner00950542001-06-06 20:29:01 +00001055 }
1056 | MALLOC Types ',' UINT ValueRef {
1057 if (!$2->isArrayType() || ((const ArrayType*)$2)->isSized())
1058 ThrowException("Trying to allocate " + $2->getName() +
1059 " as unsized array!");
Chris Lattner8896eda2001-07-09 19:38:36 +00001060 const Type *Ty = checkNewType(PointerType::getPointerType($2));
1061 $$ = new MallocInst(Ty, getVal($4, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001062 }
1063 | ALLOCA Types {
Chris Lattner8896eda2001-07-09 19:38:36 +00001064 $$ = new AllocaInst(checkNewType(PointerType::getPointerType($2)));
Chris Lattner00950542001-06-06 20:29:01 +00001065 }
1066 | ALLOCA Types ',' UINT ValueRef {
1067 if (!$2->isArrayType() || ((const ArrayType*)$2)->isSized())
1068 ThrowException("Trying to allocate " + $2->getName() +
1069 " as unsized array!");
Chris Lattner8896eda2001-07-09 19:38:36 +00001070 const Type *Ty = checkNewType(PointerType::getPointerType($2));
Chris Lattner00950542001-06-06 20:29:01 +00001071 Value *ArrSize = getVal($4, $5);
Chris Lattnerf0d0e9c2001-07-07 08:36:30 +00001072 $$ = new AllocaInst(Ty, ArrSize);
Chris Lattner00950542001-06-06 20:29:01 +00001073 }
1074 | FREE Types ValueRef {
1075 if (!$2->isPointerType())
1076 ThrowException("Trying to free nonpointer type " + $2->getName() + "!");
1077 $$ = new FreeInst(getVal($2, $3));
1078 }
1079
Chris Lattner027dcc52001-07-08 21:10:27 +00001080 | LOAD Types ValueRef UByteList {
1081 if (!$2->isPointerType())
1082 ThrowException("Can't load from nonpointer type: " + $2->getName());
1083 if (LoadInst::getIndexedType($2, *$4) == 0)
1084 ThrowException("Invalid indices for load instruction!");
1085
1086 $$ = new LoadInst(getVal($2, $3), *$4);
1087 delete $4; // Free the vector...
1088 }
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001089 | STORE Types ValueRef ',' Types ValueRef UByteList {
1090 if (!$5->isPointerType())
1091 ThrowException("Can't store to a nonpointer type: " + $5->getName());
1092 const Type *ElTy = StoreInst::getIndexedType($5, *$7);
1093 if (ElTy == 0)
1094 ThrowException("Can't store into that field list!");
1095 if (ElTy != $2)
1096 ThrowException("Can't store '" + $2->getName() + "' into space of type '"+
1097 ElTy->getName() + "'!");
1098 $$ = new StoreInst(getVal($2, $3), getVal($5, $6), *$7);
1099 delete $7;
1100 }
1101 | GETELEMENTPTR Types ValueRef UByteList {
1102 if (!$2->isPointerType())
1103 ThrowException("getelementptr insn requires pointer operand!");
1104 if (!GetElementPtrInst::getIndexedType($2, *$4, true))
1105 ThrowException("Can't get element ptr '" + $2->getName() + "'!");
1106 $$ = new GetElementPtrInst(getVal($2, $3), *$4);
1107 delete $4;
Chris Lattner8896eda2001-07-09 19:38:36 +00001108 checkNewType($$->getType());
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001109 }
Chris Lattner027dcc52001-07-08 21:10:27 +00001110
Chris Lattner00950542001-06-06 20:29:01 +00001111%%
Chris Lattner09083092001-07-08 04:57:15 +00001112int yyerror(const char *ErrorMsg) {
Chris Lattner00950542001-06-06 20:29:01 +00001113 ThrowException(string("Parse error: ") + ErrorMsg);
1114 return 0;
1115}