blob: 2de7bd82b660c2928957bc19f80cb4ae1d821e0a [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;
35const ToolCommandLine *CurOptions = 0;
36
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
349 // Get the type type plane...
350 ConstantPool::PlaneType &P = CP.getPlane(Type::TypeTy);
351 ConstantPool::PlaneType::const_iterator PI = find_if(P.begin(), P.end(),
352 EqualsType(Ty));
353 if (PI == P.end()) {
354 vector<ValueList> &ValTab = CurMeth.CurrentMethod ?
355 CurMeth.Values : CurModule.Values;
356 ConstPoolVal *CPT = new ConstPoolType(Ty);
357 CP.insert(CPT);
358 InsertValue(CPT, ValTab);
359 }
360 return Ty;
361}
362
363
Chris Lattner00950542001-06-06 20:29:01 +0000364//===----------------------------------------------------------------------===//
365// RunVMAsmParser - Define an interface to this parser
366//===----------------------------------------------------------------------===//
367//
368Module *RunVMAsmParser(const ToolCommandLine &Opts, FILE *F) {
369 llvmAsmin = F;
370 CurOptions = &Opts;
371 llvmAsmlineno = 1; // Reset the current line number...
372
373 CurModule.CurrentModule = new Module(); // Allocate a new module to read
374 yyparse(); // Parse the file.
375 Module *Result = ParserResult;
376 CurOptions = 0;
377 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
378 ParserResult = 0;
379
380 return Result;
381}
382
383%}
384
385%union {
386 Module *ModuleVal;
387 Method *MethodVal;
388 MethodArgument *MethArgVal;
389 BasicBlock *BasicBlockVal;
390 TerminatorInst *TermInstVal;
391 Instruction *InstVal;
392 ConstPoolVal *ConstVal;
393 const Type *TypeVal;
394
395 list<MethodArgument*> *MethodArgList;
396 list<Value*> *ValueList;
397 list<const Type*> *TypeList;
Chris Lattnerc24d2082001-06-11 15:04:20 +0000398 list<pair<Value*, BasicBlock*> > *PHIList; // Represent the RHS of PHI node
Chris Lattner00950542001-06-06 20:29:01 +0000399 list<pair<ConstPoolVal*, BasicBlock*> > *JumpTable;
400 vector<ConstPoolVal*> *ConstVector;
401
402 int64_t SInt64Val;
403 uint64_t UInt64Val;
404 int SIntVal;
405 unsigned UIntVal;
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000406 double FPVal;
Chris Lattner00950542001-06-06 20:29:01 +0000407
408 char *StrVal; // This memory is allocated by strdup!
409 ValID ValIDVal; // May contain memory allocated by strdup
410
411 Instruction::UnaryOps UnaryOpVal;
412 Instruction::BinaryOps BinaryOpVal;
413 Instruction::TermOps TermOpVal;
414 Instruction::MemoryOps MemOpVal;
Chris Lattner027dcc52001-07-08 21:10:27 +0000415 Instruction::OtherOps OtherOpVal;
Chris Lattner00950542001-06-06 20:29:01 +0000416}
417
418%type <ModuleVal> Module MethodList
Chris Lattnere1815642001-07-15 06:35:53 +0000419%type <MethodVal> Method MethodProto MethodHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000420%type <BasicBlockVal> BasicBlock InstructionList
421%type <TermInstVal> BBTerminatorInst
422%type <InstVal> Inst InstVal MemoryInst
423%type <ConstVal> ConstVal
Chris Lattner027dcc52001-07-08 21:10:27 +0000424%type <ConstVector> ConstVector UByteList
Chris Lattner00950542001-06-06 20:29:01 +0000425%type <MethodArgList> ArgList ArgListH
426%type <MethArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000427%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000428%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner00950542001-06-06 20:29:01 +0000429%type <TypeList> TypeList
430%type <JumpTable> JumpTable
431
432%type <ValIDVal> ValueRef ConstValueRef // Reference to a definition or BB
433
434// Tokens and types for handling constant integer values
435//
436// ESINT64VAL - A negative number within long long range
437%token <SInt64Val> ESINT64VAL
438
439// EUINT64VAL - A positive number within uns. long long range
440%token <UInt64Val> EUINT64VAL
441%type <SInt64Val> EINT64VAL
442
443%token <SIntVal> SINTVAL // Signed 32 bit ints...
444%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
445%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000446%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000447
448// Built in types...
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000449%type <TypeVal> Types TypesV SIntType UIntType IntType FPType
Chris Lattner00950542001-06-06 20:29:01 +0000450%token <TypeVal> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
451%token <TypeVal> FLOAT DOUBLE STRING TYPE LABEL
452
453%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
454%type <StrVal> OptVAR_ID OptAssign
455
456
Chris Lattner09083092001-07-08 04:57:15 +0000457%token IMPLEMENTATION TRUE FALSE BEGINTOK END DECLARE TO
Chris Lattner00950542001-06-06 20:29:01 +0000458
459// Basic Block Terminating Operators
460%token <TermOpVal> RET BR SWITCH
461
462// Unary Operators
463%type <UnaryOpVal> UnaryOps // all the unary operators
Chris Lattner71496b32001-07-08 19:03:27 +0000464%token <UnaryOpVal> NOT
Chris Lattner00950542001-06-06 20:29:01 +0000465
466// Binary Operators
467%type <BinaryOpVal> BinaryOps // all the binary operators
468%token <BinaryOpVal> ADD SUB MUL DIV REM
Chris Lattner027dcc52001-07-08 21:10:27 +0000469%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000470
471// Memory Instructions
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000472%token <MemoryOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000473
Chris Lattner027dcc52001-07-08 21:10:27 +0000474// Other Operators
475%type <OtherOpVal> ShiftOps
476%token <OtherOpVal> PHI CALL CAST SHL SHR
477
Chris Lattner00950542001-06-06 20:29:01 +0000478%start Module
479%%
480
481// Handle constant integer size restriction and conversion...
482//
483
484INTVAL : SINTVAL
485INTVAL : UINTVAL {
486 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
487 ThrowException("Value too large for type!");
488 $$ = (int32_t)$1;
489}
490
491
492EINT64VAL : ESINT64VAL // These have same type and can't cause problems...
493EINT64VAL : EUINT64VAL {
494 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
495 ThrowException("Value too large for type!");
496 $$ = (int64_t)$1;
497}
498
499// Types includes all predefined types... except void, because you can't do
500// anything with it except for certain specific things...
501//
Chris Lattnere98dda62001-07-14 06:10:16 +0000502// User defined types are added later...
Chris Lattner00950542001-06-06 20:29:01 +0000503//
504Types : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT
505Types : LONG | ULONG | FLOAT | DOUBLE | STRING | TYPE | LABEL
506
507// TypesV includes all of 'Types', but it also includes the void type.
508TypesV : Types | VOID
509
510// Operations that are notably excluded from this list include:
511// RET, BR, & SWITCH because they end basic blocks and are treated specially.
512//
Chris Lattner09083092001-07-08 04:57:15 +0000513UnaryOps : NOT
Chris Lattner00950542001-06-06 20:29:01 +0000514BinaryOps : ADD | SUB | MUL | DIV | REM
515BinaryOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE
Chris Lattner027dcc52001-07-08 21:10:27 +0000516ShiftOps : SHL | SHR
Chris Lattner00950542001-06-06 20:29:01 +0000517
Chris Lattnere98dda62001-07-14 06:10:16 +0000518// These are some types that allow classification if we only want a particular
519// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner00950542001-06-06 20:29:01 +0000520SIntType : LONG | INT | SHORT | SBYTE
521UIntType : ULONG | UINT | USHORT | UBYTE
522IntType : SIntType | UIntType
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000523FPType : FLOAT | DOUBLE
Chris Lattner00950542001-06-06 20:29:01 +0000524
Chris Lattnere98dda62001-07-14 06:10:16 +0000525// OptAssign - Value producing statements have an optional assignment component
Chris Lattner00950542001-06-06 20:29:01 +0000526OptAssign : VAR_ID '=' {
527 $$ = $1;
528 }
529 | /*empty*/ {
530 $$ = 0;
531 }
532
Chris Lattnere98dda62001-07-14 06:10:16 +0000533// ConstVal - The various declarations that go into the constant pool. This
534// includes all forward declarations of types, constants, and functions.
535//
Chris Lattner00950542001-06-06 20:29:01 +0000536ConstVal : SIntType EINT64VAL { // integral constants
537 if (!ConstPoolSInt::isValueValidForType($1, $2))
538 ThrowException("Constant value doesn't fit in type!");
539 $$ = new ConstPoolSInt($1, $2);
540 }
541 | UIntType EUINT64VAL { // integral constants
542 if (!ConstPoolUInt::isValueValidForType($1, $2))
543 ThrowException("Constant value doesn't fit in type!");
544 $$ = new ConstPoolUInt($1, $2);
545 }
546 | BOOL TRUE { // Boolean constants
547 $$ = new ConstPoolBool(true);
548 }
549 | BOOL FALSE { // Boolean constants
550 $$ = new ConstPoolBool(false);
551 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000552 | FPType FPVAL { // Float & Double constants
553 $$ = new ConstPoolFP($1, $2);
554 }
Chris Lattner00950542001-06-06 20:29:01 +0000555 | STRING STRINGCONSTANT { // String constants
556 cerr << "FIXME: TODO: String constants [sbyte] not implemented yet!\n";
557 abort();
558 //$$ = new ConstPoolString($2);
559 free($2);
560 }
561 | TYPE Types { // Type constants
562 $$ = new ConstPoolType($2);
563 }
564 | '[' Types ']' '[' ConstVector ']' { // Nonempty array constant
565 // Verify all elements are correct type!
566 const ArrayType *AT = ArrayType::getArrayType($2);
567 for (unsigned i = 0; i < $5->size(); i++) {
568 if ($2 != (*$5)[i]->getType())
569 ThrowException("Element #" + utostr(i) + " is not of type '" +
570 $2->getName() + "' as required!\nIt is of type '" +
571 (*$5)[i]->getType()->getName() + "'.");
572 }
573
574 $$ = new ConstPoolArray(AT, *$5);
575 delete $5;
576 }
577 | '[' Types ']' '[' ']' { // Empty array constant
578 vector<ConstPoolVal*> Empty;
579 $$ = new ConstPoolArray(ArrayType::getArrayType($2), Empty);
580 }
581 | '[' EUINT64VAL 'x' Types ']' '[' ConstVector ']' {
582 // Verify all elements are correct type!
583 const ArrayType *AT = ArrayType::getArrayType($4, (int)$2);
584 if ($2 != $7->size())
585 ThrowException("Type mismatch: constant sized array initialized with " +
586 utostr($7->size()) + " arguments, but has size of " +
587 itostr((int)$2) + "!");
588
589 for (unsigned i = 0; i < $7->size(); i++) {
590 if ($4 != (*$7)[i]->getType())
591 ThrowException("Element #" + utostr(i) + " is not of type '" +
592 $4->getName() + "' as required!\nIt is of type '" +
593 (*$7)[i]->getType()->getName() + "'.");
594 }
595
596 $$ = new ConstPoolArray(AT, *$7);
597 delete $7;
598 }
599 | '[' EUINT64VAL 'x' Types ']' '[' ']' {
600 if ($2 != 0)
601 ThrowException("Type mismatch: constant sized array initialized with 0"
602 " arguments, but has size of " + itostr((int)$2) + "!");
603 vector<ConstPoolVal*> Empty;
604 $$ = new ConstPoolArray(ArrayType::getArrayType($4, 0), Empty);
605 }
606 | '{' TypeList '}' '{' ConstVector '}' {
607 StructType::ElementTypes Types($2->begin(), $2->end());
608 delete $2;
609
610 const StructType *St = StructType::getStructType(Types);
611 $$ = new ConstPoolStruct(St, *$5);
612 delete $5;
613 }
614 | '{' '}' '{' '}' {
615 const StructType *St =
616 StructType::getStructType(StructType::ElementTypes());
617 vector<ConstPoolVal*> Empty;
618 $$ = new ConstPoolStruct(St, Empty);
619 }
620/*
621 | Types '*' ConstVal {
622 assert(0);
623 $$ = 0;
624 }
625*/
626
Chris Lattnere98dda62001-07-14 06:10:16 +0000627// ConstVector - A list of comma seperated constants.
Chris Lattner00950542001-06-06 20:29:01 +0000628ConstVector : ConstVector ',' ConstVal {
629 ($$ = $1)->push_back(addConstValToConstantPool($3));
630 }
631 | ConstVal {
632 $$ = new vector<ConstPoolVal*>();
633 $$->push_back(addConstValToConstantPool($1));
634 }
635
Chris Lattnere98dda62001-07-14 06:10:16 +0000636//ExternMethodDecl : EXTERNAL TypesV '(' TypeList ')' {
637// }
638//ExternVarDecl :
Chris Lattner00950542001-06-06 20:29:01 +0000639
Chris Lattnere98dda62001-07-14 06:10:16 +0000640// ConstPool - Constants with optional names assigned to them.
Chris Lattner00950542001-06-06 20:29:01 +0000641ConstPool : ConstPool OptAssign ConstVal {
642 if ($2) {
643 $3->setName($2);
644 free($2);
645 }
646
647 addConstValToConstantPool($3);
648 }
Chris Lattnere98dda62001-07-14 06:10:16 +0000649/*
650 | ConstPool OptAssign GlobalDecl { // Global declarations appear in CP
651 if ($2) {
652 $3->setName($2);
653 free($2);
654 }
655 //CurModule.CurrentModule->
656 }
657*/
Chris Lattner00950542001-06-06 20:29:01 +0000658 | /* empty: end of list */ {
659 }
660
661
662//===----------------------------------------------------------------------===//
663// Rules to match Modules
664//===----------------------------------------------------------------------===//
665
666// Module rule: Capture the result of parsing the whole file into a result
667// variable...
668//
669Module : MethodList {
670 $$ = ParserResult = $1;
671 CurModule.ModuleDone();
672}
673
Chris Lattnere98dda62001-07-14 06:10:16 +0000674// MethodList - A list of methods, preceeded by a constant pool.
675//
Chris Lattner00950542001-06-06 20:29:01 +0000676MethodList : MethodList Method {
Chris Lattner00950542001-06-06 20:29:01 +0000677 $$ = $1;
Chris Lattnere1815642001-07-15 06:35:53 +0000678 if (!$2->getParent())
679 $1->getMethodList().push_back($2);
680 CurMeth.MethodDone();
Chris Lattner00950542001-06-06 20:29:01 +0000681 }
Chris Lattnere1815642001-07-15 06:35:53 +0000682 | MethodList MethodProto {
683 $$ = $1;
684 if (!$2->getParent())
685 $1->getMethodList().push_back($2);
686 CurMeth.MethodDone();
687 }
Chris Lattner00950542001-06-06 20:29:01 +0000688 | ConstPool IMPLEMENTATION {
689 $$ = CurModule.CurrentModule;
690 }
691
692
693//===----------------------------------------------------------------------===//
694// Rules to match Method Headers
695//===----------------------------------------------------------------------===//
696
697OptVAR_ID : VAR_ID | /*empty*/ { $$ = 0; }
698
699ArgVal : Types OptVAR_ID {
700 $$ = new MethodArgument($1);
701 if ($2) { // Was the argument named?
702 $$->setName($2);
703 free($2); // The string was strdup'd, so free it now.
704 }
705}
706
707ArgListH : ArgVal ',' ArgListH {
708 $$ = $3;
709 $3->push_front($1);
710 }
711 | ArgVal {
712 $$ = new list<MethodArgument*>();
713 $$->push_front($1);
714 }
715
716ArgList : ArgListH {
717 $$ = $1;
718 }
719 | /* empty */ {
720 $$ = 0;
721 }
722
723MethodHeaderH : TypesV STRINGCONSTANT '(' ArgList ')' {
724 MethodType::ParamTypes ParamTypeList;
725 if ($4)
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000726 for (list<MethodArgument*>::iterator I = $4->begin(); I != $4->end(); ++I)
Chris Lattner00950542001-06-06 20:29:01 +0000727 ParamTypeList.push_back((*I)->getType());
728
729 const MethodType *MT = MethodType::getMethodType($1, ParamTypeList);
730
Chris Lattnere1815642001-07-15 06:35:53 +0000731 Method *M = 0;
732 if (SymbolTable *ST = CurModule.CurrentModule->getSymbolTable()) {
733 if (Value *V = ST->lookup(MT, $2)) { // Method already in symtab?
734 M = V->castMethodAsserting();
Chris Lattner00950542001-06-06 20:29:01 +0000735
Chris Lattnere1815642001-07-15 06:35:53 +0000736 // Yes it is. If this is the case, either we need to be a forward decl,
737 // or it needs to be.
738 if (!CurMeth.isDeclare && !M->isExternal())
739 ThrowException("Redefinition of method '" + string($2) + "'!");
740 }
741 }
742
743 if (M == 0) { // Not already defined?
744 M = new Method(MT, $2);
745 InsertValue(M, CurModule.Values);
746 }
747
748 free($2); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +0000749
750 CurMeth.MethodStart(M);
751
752 // Add all of the arguments we parsed to the method...
Chris Lattnere1815642001-07-15 06:35:53 +0000753 if ($4 && !CurMeth.isDeclare) { // Is null if empty...
Chris Lattner00950542001-06-06 20:29:01 +0000754 Method::ArgumentListType &ArgList = M->getArgumentList();
755
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000756 for (list<MethodArgument*>::iterator I = $4->begin(); I != $4->end(); ++I) {
Chris Lattner00950542001-06-06 20:29:01 +0000757 InsertValue(*I);
758 ArgList.push_back(*I);
759 }
760 delete $4; // We're now done with the argument list
761 }
762}
763
764MethodHeader : MethodHeaderH ConstPool BEGINTOK {
765 $$ = CurMeth.CurrentMethod;
766}
767
768Method : BasicBlockList END {
769 $$ = $1;
770}
771
Chris Lattnere1815642001-07-15 06:35:53 +0000772MethodProto : DECLARE { CurMeth.isDeclare = true; } MethodHeaderH {
773 $$ = CurMeth.CurrentMethod;
774}
Chris Lattner00950542001-06-06 20:29:01 +0000775
776//===----------------------------------------------------------------------===//
777// Rules to match Basic Blocks
778//===----------------------------------------------------------------------===//
779
780ConstValueRef : ESINT64VAL { // A reference to a direct constant
781 $$ = ValID::create($1);
782 }
783 | EUINT64VAL {
784 $$ = ValID::create($1);
785 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000786 | FPVAL { // Perhaps it's an FP constant?
787 $$ = ValID::create($1);
788 }
Chris Lattner00950542001-06-06 20:29:01 +0000789 | TRUE {
790 $$ = ValID::create((int64_t)1);
791 }
792 | FALSE {
793 $$ = ValID::create((int64_t)0);
794 }
795 | STRINGCONSTANT { // Quoted strings work too... especially for methods
796 $$ = ValID::create_conststr($1);
797 }
798
799// ValueRef - A reference to a definition...
800ValueRef : INTVAL { // Is it an integer reference...?
801 $$ = ValID::create($1);
802 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000803 | VAR_ID { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +0000804 $$ = ValID::create($1);
805 }
806 | ConstValueRef {
807 $$ = $1;
808 }
809
810// The user may refer to a user defined type by its typeplane... check for this
811// now...
812//
813Types : ValueRef {
814 Value *D = getVal(Type::TypeTy, $1, true);
815 if (D == 0) ThrowException("Invalid user defined type: " + $1.getName());
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000816
817 // User defined type not in const pool!
818 ConstPoolType *CPT = (ConstPoolType*)D->castConstantAsserting();
Chris Lattner00950542001-06-06 20:29:01 +0000819 $$ = CPT->getValue();
820 }
821 | TypesV '(' TypeList ')' { // Method derived type?
822 MethodType::ParamTypes Params($3->begin(), $3->end());
823 delete $3;
Chris Lattner8896eda2001-07-09 19:38:36 +0000824 $$ = checkNewType(MethodType::getMethodType($1, Params));
Chris Lattner00950542001-06-06 20:29:01 +0000825 }
826 | TypesV '(' ')' { // Method derived type?
827 MethodType::ParamTypes Params; // Empty list
Chris Lattner8896eda2001-07-09 19:38:36 +0000828 $$ = checkNewType(MethodType::getMethodType($1, Params));
Chris Lattner00950542001-06-06 20:29:01 +0000829 }
830 | '[' Types ']' {
Chris Lattner8896eda2001-07-09 19:38:36 +0000831 $$ = checkNewType(ArrayType::getArrayType($2));
Chris Lattner00950542001-06-06 20:29:01 +0000832 }
833 | '[' EUINT64VAL 'x' Types ']' {
Chris Lattner8896eda2001-07-09 19:38:36 +0000834 $$ = checkNewType(ArrayType::getArrayType($4, (int)$2));
Chris Lattner00950542001-06-06 20:29:01 +0000835 }
836 | '{' TypeList '}' {
837 StructType::ElementTypes Elements($2->begin(), $2->end());
838 delete $2;
Chris Lattner8896eda2001-07-09 19:38:36 +0000839 $$ = checkNewType(StructType::getStructType(Elements));
Chris Lattner00950542001-06-06 20:29:01 +0000840 }
841 | '{' '}' {
Chris Lattner8896eda2001-07-09 19:38:36 +0000842 $$ = checkNewType(StructType::getStructType(StructType::ElementTypes()));
Chris Lattner00950542001-06-06 20:29:01 +0000843 }
844 | Types '*' {
Chris Lattner8896eda2001-07-09 19:38:36 +0000845 $$ = checkNewType(PointerType::getPointerType($1));
Chris Lattner00950542001-06-06 20:29:01 +0000846 }
847
848
849TypeList : Types {
850 $$ = new list<const Type*>();
851 $$->push_back($1);
852 }
853 | TypeList ',' Types {
854 ($$=$1)->push_back($3);
855 }
856
857
858BasicBlockList : BasicBlockList BasicBlock {
859 $1->getBasicBlocks().push_back($2);
860 $$ = $1;
861 }
862 | MethodHeader BasicBlock { // Do not allow methods with 0 basic blocks
863 $$ = $1; // in them...
864 $1->getBasicBlocks().push_back($2);
865 }
866
867
868// Basic blocks are terminated by branching instructions:
869// br, br/cc, switch, ret
870//
871BasicBlock : InstructionList BBTerminatorInst {
872 $1->getInstList().push_back($2);
873 InsertValue($1);
874 $$ = $1;
875 }
876 | LABELSTR InstructionList BBTerminatorInst {
877 $2->getInstList().push_back($3);
878 $2->setName($1);
879 free($1); // Free the strdup'd memory...
880
881 InsertValue($2);
882 $$ = $2;
883 }
884
885InstructionList : InstructionList Inst {
886 $1->getInstList().push_back($2);
887 $$ = $1;
888 }
889 | /* empty */ {
890 $$ = new BasicBlock();
891 }
892
893BBTerminatorInst : RET Types ValueRef { // Return with a result...
894 $$ = new ReturnInst(getVal($2, $3));
895 }
896 | RET VOID { // Return with no result...
897 $$ = new ReturnInst();
898 }
899 | BR LABEL ValueRef { // Unconditional Branch...
900 $$ = new BranchInst((BasicBlock*)getVal(Type::LabelTy, $3));
901 } // Conditional Branch...
902 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
903 $$ = new BranchInst((BasicBlock*)getVal(Type::LabelTy, $6),
904 (BasicBlock*)getVal(Type::LabelTy, $9),
905 getVal(Type::BoolTy, $3));
906 }
907 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
908 SwitchInst *S = new SwitchInst(getVal($2, $3),
909 (BasicBlock*)getVal(Type::LabelTy, $6));
910 $$ = S;
911
912 list<pair<ConstPoolVal*, BasicBlock*> >::iterator I = $8->begin(),
913 end = $8->end();
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000914 for (; I != end; ++I)
Chris Lattner00950542001-06-06 20:29:01 +0000915 S->dest_push_back(I->first, I->second);
916 }
917
918JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
919 $$ = $1;
920 ConstPoolVal *V = (ConstPoolVal*)getVal($2, $3, true);
921 if (V == 0)
922 ThrowException("May only switch on a constant pool value!");
923
924 $$->push_back(make_pair(V, (BasicBlock*)getVal($5, $6)));
925 }
926 | IntType ConstValueRef ',' LABEL ValueRef {
927 $$ = new list<pair<ConstPoolVal*, BasicBlock*> >();
928 ConstPoolVal *V = (ConstPoolVal*)getVal($1, $2, true);
929
930 if (V == 0)
931 ThrowException("May only switch on a constant pool value!");
932
933 $$->push_back(make_pair(V, (BasicBlock*)getVal($4, $5)));
934 }
935
936Inst : OptAssign InstVal {
937 if ($1) // Is this definition named??
938 $2->setName($1); // if so, assign the name...
939
940 InsertValue($2);
941 $$ = $2;
942}
943
Chris Lattnerc24d2082001-06-11 15:04:20 +0000944PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
945 $$ = new list<pair<Value*, BasicBlock*> >();
946 $$->push_back(make_pair(getVal($1, $3),
947 (BasicBlock*)getVal(Type::LabelTy, $5)));
948 }
949 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
950 $$ = $1;
951 $1->push_back(make_pair(getVal($1->front().first->getType(), $4),
952 (BasicBlock*)getVal(Type::LabelTy, $6)));
953 }
954
955
956ValueRefList : Types ValueRef { // Used for call statements...
Chris Lattner00950542001-06-06 20:29:01 +0000957 $$ = new list<Value*>();
958 $$->push_back(getVal($1, $2));
959 }
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000960 | ValueRefList ',' Types ValueRef {
Chris Lattner00950542001-06-06 20:29:01 +0000961 $$ = $1;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000962 $1->push_back(getVal($3, $4));
Chris Lattner00950542001-06-06 20:29:01 +0000963 }
964
965// ValueRefListE - Just like ValueRefList, except that it may also be empty!
966ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; }
967
968InstVal : BinaryOps Types ValueRef ',' ValueRef {
Chris Lattnerbebd60d2001-06-25 07:31:31 +0000969 $$ = BinaryOperator::create($1, getVal($2, $3), getVal($2, $5));
Chris Lattner00950542001-06-06 20:29:01 +0000970 if ($$ == 0)
971 ThrowException("binary operator returned null!");
972 }
973 | UnaryOps Types ValueRef {
Chris Lattnerbebd60d2001-06-25 07:31:31 +0000974 $$ = UnaryOperator::create($1, getVal($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +0000975 if ($$ == 0)
976 ThrowException("unary operator returned null!");
Chris Lattner09083092001-07-08 04:57:15 +0000977 }
Chris Lattner027dcc52001-07-08 21:10:27 +0000978 | ShiftOps Types ValueRef ',' Types ValueRef {
979 if ($5 != Type::UByteTy) ThrowException("Shift amount must be ubyte!");
980 $$ = new ShiftInst($1, getVal($2, $3), getVal($5, $6));
981 }
Chris Lattner09083092001-07-08 04:57:15 +0000982 | CAST Types ValueRef TO Types {
Chris Lattner71496b32001-07-08 19:03:27 +0000983 $$ = new CastInst(getVal($2, $3), $5);
Chris Lattner09083092001-07-08 04:57:15 +0000984 }
Chris Lattnerc24d2082001-06-11 15:04:20 +0000985 | PHI PHIList {
986 const Type *Ty = $2->front().first->getType();
987 $$ = new PHINode(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000988 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +0000989 if ($2->front().first->getType() != Ty)
990 ThrowException("All elements of a PHI node must be of the same type!");
991 ((PHINode*)$$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +0000992 $2->pop_front();
993 }
994 delete $2; // Free the list...
995 }
996 | CALL Types ValueRef '(' ValueRefListE ')' {
997 if (!$2->isMethodType())
998 ThrowException("Can only call methods: invalid type '" +
999 $2->getName() + "'!");
1000
1001 const MethodType *Ty = (const MethodType*)$2;
1002
1003 Value *V = getVal(Ty, $3);
Chris Lattner7fc9fe32001-06-27 23:41:11 +00001004 if (!V->isMethod() || V->getType() != Ty)
Chris Lattner00950542001-06-06 20:29:01 +00001005 ThrowException("Cannot call: " + $3.getName() + "!");
1006
1007 // Create or access a new type that corresponds to the function call...
1008 vector<Value *> Params;
1009
1010 if ($5) {
1011 // Pull out just the arguments...
1012 Params.insert(Params.begin(), $5->begin(), $5->end());
1013 delete $5;
1014
1015 // Loop through MethodType's arguments and ensure they are specified
1016 // correctly!
1017 //
1018 MethodType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1019 unsigned i;
1020 for (i = 0; i < Params.size() && I != Ty->getParamTypes().end(); ++i,++I){
1021 if (Params[i]->getType() != *I)
1022 ThrowException("Parameter " + utostr(i) + " is not of type '" +
1023 (*I)->getName() + "'!");
1024 }
1025
1026 if (i != Params.size() || I != Ty->getParamTypes().end())
1027 ThrowException("Invalid number of parameters detected!");
1028 }
1029
1030 // Create the call node...
1031 $$ = new CallInst((Method*)V, Params);
1032 }
1033 | MemoryInst {
1034 $$ = $1;
1035 }
1036
Chris Lattner027dcc52001-07-08 21:10:27 +00001037// UByteList - List of ubyte values for load and store instructions
1038UByteList : ',' ConstVector {
1039 $$ = $2;
1040} | /* empty */ {
1041 $$ = new vector<ConstPoolVal*>();
1042}
1043
Chris Lattner00950542001-06-06 20:29:01 +00001044MemoryInst : MALLOC Types {
Chris Lattner8896eda2001-07-09 19:38:36 +00001045 $$ = new MallocInst(checkNewType(PointerType::getPointerType($2)));
Chris Lattner00950542001-06-06 20:29:01 +00001046 }
1047 | MALLOC Types ',' UINT ValueRef {
1048 if (!$2->isArrayType() || ((const ArrayType*)$2)->isSized())
1049 ThrowException("Trying to allocate " + $2->getName() +
1050 " as unsized array!");
Chris Lattner8896eda2001-07-09 19:38:36 +00001051 const Type *Ty = checkNewType(PointerType::getPointerType($2));
1052 $$ = new MallocInst(Ty, getVal($4, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001053 }
1054 | ALLOCA Types {
Chris Lattner8896eda2001-07-09 19:38:36 +00001055 $$ = new AllocaInst(checkNewType(PointerType::getPointerType($2)));
Chris Lattner00950542001-06-06 20:29:01 +00001056 }
1057 | ALLOCA Types ',' UINT ValueRef {
1058 if (!$2->isArrayType() || ((const ArrayType*)$2)->isSized())
1059 ThrowException("Trying to allocate " + $2->getName() +
1060 " as unsized array!");
Chris Lattner8896eda2001-07-09 19:38:36 +00001061 const Type *Ty = checkNewType(PointerType::getPointerType($2));
Chris Lattner00950542001-06-06 20:29:01 +00001062 Value *ArrSize = getVal($4, $5);
Chris Lattnerf0d0e9c2001-07-07 08:36:30 +00001063 $$ = new AllocaInst(Ty, ArrSize);
Chris Lattner00950542001-06-06 20:29:01 +00001064 }
1065 | FREE Types ValueRef {
1066 if (!$2->isPointerType())
1067 ThrowException("Trying to free nonpointer type " + $2->getName() + "!");
1068 $$ = new FreeInst(getVal($2, $3));
1069 }
1070
Chris Lattner027dcc52001-07-08 21:10:27 +00001071 | LOAD Types ValueRef UByteList {
1072 if (!$2->isPointerType())
1073 ThrowException("Can't load from nonpointer type: " + $2->getName());
1074 if (LoadInst::getIndexedType($2, *$4) == 0)
1075 ThrowException("Invalid indices for load instruction!");
1076
1077 $$ = new LoadInst(getVal($2, $3), *$4);
1078 delete $4; // Free the vector...
1079 }
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001080 | STORE Types ValueRef ',' Types ValueRef UByteList {
1081 if (!$5->isPointerType())
1082 ThrowException("Can't store to a nonpointer type: " + $5->getName());
1083 const Type *ElTy = StoreInst::getIndexedType($5, *$7);
1084 if (ElTy == 0)
1085 ThrowException("Can't store into that field list!");
1086 if (ElTy != $2)
1087 ThrowException("Can't store '" + $2->getName() + "' into space of type '"+
1088 ElTy->getName() + "'!");
1089 $$ = new StoreInst(getVal($2, $3), getVal($5, $6), *$7);
1090 delete $7;
1091 }
1092 | GETELEMENTPTR Types ValueRef UByteList {
1093 if (!$2->isPointerType())
1094 ThrowException("getelementptr insn requires pointer operand!");
1095 if (!GetElementPtrInst::getIndexedType($2, *$4, true))
1096 ThrowException("Can't get element ptr '" + $2->getName() + "'!");
1097 $$ = new GetElementPtrInst(getVal($2, $3), *$4);
1098 delete $4;
Chris Lattner8896eda2001-07-09 19:38:36 +00001099 checkNewType($$->getType());
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001100 }
Chris Lattner027dcc52001-07-08 21:10:27 +00001101
Chris Lattner00950542001-06-06 20:29:01 +00001102%%
Chris Lattner09083092001-07-08 04:57:15 +00001103int yyerror(const char *ErrorMsg) {
Chris Lattner00950542001-06-06 20:29:01 +00001104 ThrowException(string("Parse error: ") + ErrorMsg);
1105 return 0;
1106}