blob: e5534cda76b5ab2ce16f92b4d4a1be5de06ff14f [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
61 vector<ValueList> Values; // Keep track of numbered definitions
62 vector<ValueList> LateResolveValues;
63
64 inline PerMethodInfo() {
65 CurrentMethod = 0;
66 }
67
68 inline ~PerMethodInfo() {}
69
70 inline void MethodStart(Method *M) {
71 CurrentMethod = M;
72 }
73
74 void MethodDone() {
75 // If we could not resolve some blocks at parsing time (forward branches)
76 // resolve the branches now...
77 ResolveDefinitions(LateResolveValues);
78
79 Values.clear(); // Clear out method local definitions
80 CurrentMethod = 0;
81 }
82} CurMeth; // Info for the current method...
83
84
85//===----------------------------------------------------------------------===//
86// Code to handle definitions of all the types
87//===----------------------------------------------------------------------===//
88
89static void InsertValue(Value *D, vector<ValueList> &ValueTab = CurMeth.Values) {
90 if (!D->hasName()) { // Is this a numbered definition?
91 unsigned type = D->getType()->getUniqueID();
92 if (ValueTab.size() <= type)
93 ValueTab.resize(type+1, ValueList());
94 //printf("Values[%d][%d] = %d\n", type, ValueTab[type].size(), D);
95 ValueTab[type].push_back(D);
96 }
97}
98
99static Value *getVal(const Type *Type, ValID &D,
100 bool DoNotImprovise = false) {
101 switch (D.Type) {
102 case 0: { // Is it a numbered definition?
103 unsigned type = Type->getUniqueID();
104 unsigned Num = (unsigned)D.Num;
105
106 // Module constants occupy the lowest numbered slots...
107 if (type < CurModule.Values.size()) {
108 if (Num < CurModule.Values[type].size())
109 return CurModule.Values[type][Num];
110
111 Num -= CurModule.Values[type].size();
112 }
113
114 // Make sure that our type is within bounds
115 if (CurMeth.Values.size() <= type)
116 break;
117
118 // Check that the number is within bounds...
119 if (CurMeth.Values[type].size() <= Num)
120 break;
121
122 return CurMeth.Values[type][Num];
123 }
124 case 1: { // Is it a named definition?
125 string Name(D.Name);
126 SymbolTable *SymTab = 0;
127 if (CurMeth.CurrentMethod)
128 SymTab = CurMeth.CurrentMethod->getSymbolTable();
129 Value *N = SymTab ? SymTab->lookup(Type, Name) : 0;
130
131 if (N == 0) {
132 SymTab = CurModule.CurrentModule->getSymbolTable();
133 if (SymTab)
134 N = SymTab->lookup(Type, Name);
135 if (N == 0) break;
136 }
137
138 D.destroy(); // Free old strdup'd memory...
139 return N;
140 }
141
142 case 2: // Is it a constant pool reference??
143 case 3: // Is it an unsigned const pool reference?
144 case 4:{ // Is it a string const pool reference?
145 ConstPoolVal *CPV = 0;
146
147 // Check to make sure that "Type" is an integral type, and that our
148 // value will fit into the specified type...
149 switch (D.Type) {
150 case 2:
151 if (Type == Type::BoolTy) { // Special handling for boolean data
152 CPV = new ConstPoolBool(D.ConstPool64 != 0);
153 } else {
154 if (!ConstPoolSInt::isValueValidForType(Type, D.ConstPool64))
155 ThrowException("Symbolic constant pool reference is invalid!");
156 CPV = new ConstPoolSInt(Type, D.ConstPool64);
157 }
158 break;
159 case 3:
160 if (!ConstPoolUInt::isValueValidForType(Type, D.UConstPool64)) {
161 if (!ConstPoolSInt::isValueValidForType(Type, D.ConstPool64)) {
162 ThrowException("Symbolic constant pool reference is invalid!");
163 } else { // This is really a signed reference. Transmogrify.
164 CPV = new ConstPoolSInt(Type, D.ConstPool64);
165 }
166 } else {
167 CPV = new ConstPoolUInt(Type, D.UConstPool64);
168 }
169 break;
170 case 4:
171 cerr << "FIXME: TODO: String constants [sbyte] not implemented yet!\n";
172 abort();
173 //CPV = new ConstPoolString(D.Name);
174 D.destroy(); // Free the string memory
175 break;
176 }
177 assert(CPV && "How did we escape creating a constant??");
178
179 // Scan through the constant table and see if we already have loaded this
180 // constant.
181 //
182 ConstantPool &CP = CurMeth.CurrentMethod ?
183 CurMeth.CurrentMethod->getConstantPool() :
184 CurModule.CurrentModule->getConstantPool();
185 ConstPoolVal *C = CP.find(CPV); // Already have this constant?
186 if (C) {
187 delete CPV; // Didn't need this after all, oh well.
188 return C; // Yup, we already have one, recycle it!
189 }
190 CP.insert(CPV);
191
192 // Success, everything is kosher. Lets go!
193 return CPV;
194 } // End of case 2,3,4
195 } // End of switch
196
197
198 // If we reached here, we referenced either a symbol that we don't know about
199 // or an id number that hasn't been read yet. We may be referencing something
200 // forward, so just create an entry to be resolved later and get to it...
201 //
202 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
203
204 // TODO: Attempt to coallecse nodes that are the same with previous ones.
205 Value *d = 0;
206 switch (Type->getPrimitiveID()) {
207 case Type::LabelTyID: d = new BBPlaceHolder(Type, D); break;
208 case Type::MethodTyID:
209 d = new MethPlaceHolder(Type, D);
210 InsertValue(d, CurModule.LateResolveValues);
211 return d;
212//case Type::ClassTyID: d = new ClassPlaceHolder(Type, D); break;
213 default: d = new DefPlaceHolder(Type, D); break;
214 }
215
216 assert(d != 0 && "How did we not make something?");
217 InsertValue(d, CurMeth.LateResolveValues);
218 return d;
219}
220
221
222//===----------------------------------------------------------------------===//
223// Code to handle forward references in instructions
224//===----------------------------------------------------------------------===//
225//
226// This code handles the late binding needed with statements that reference
227// values not defined yet... for example, a forward branch, or the PHI node for
228// a loop body.
229//
230// This keeps a table (CurMeth.LateResolveValues) of all such forward references
231// and back patchs after we are done.
232//
233
234// ResolveDefinitions - If we could not resolve some defs at parsing
235// time (forward branches, phi functions for loops, etc...) resolve the
236// defs now...
237//
238static void ResolveDefinitions(vector<ValueList> &LateResolvers) {
239 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
240 for (unsigned ty = 0; ty < LateResolvers.size(); ty++) {
241 while (!LateResolvers[ty].empty()) {
242 Value *V = LateResolvers[ty].back();
243 LateResolvers[ty].pop_back();
244 ValID &DID = getValIDFromPlaceHolder(V);
245
246 Value *TheRealValue = getVal(Type::getUniqueIDType(ty), DID, true);
247
248 if (TheRealValue == 0 && DID.Type == 1)
249 ThrowException("Reference to an invalid definition: '" +DID.getName() +
250 "' of type '" + V->getType()->getName() + "'");
251 else if (TheRealValue == 0)
252 ThrowException("Reference to an invalid definition: #" +itostr(DID.Num)+
253 " of type '" + V->getType()->getName() + "'");
254
255 V->replaceAllUsesWith(TheRealValue);
256 assert(V->use_empty());
257 delete V;
258 }
259 }
260
261 LateResolvers.clear();
262}
263
264// addConstValToConstantPool - This code is used to insert a constant into the
265// current constant pool. This is designed to make maximal (but not more than
266// possible) reuse (merging) of constants in the constant pool. This means that
267// multiple references to %4, for example will all get merged.
268//
269static ConstPoolVal *addConstValToConstantPool(ConstPoolVal *C) {
270 vector<ValueList> &ValTab = CurMeth.CurrentMethod ?
271 CurMeth.Values : CurModule.Values;
272 ConstantPool &CP = CurMeth.CurrentMethod ?
273 CurMeth.CurrentMethod->getConstantPool() :
274 CurModule.CurrentModule->getConstantPool();
275
276 if (ConstPoolVal *CPV = CP.find(C)) {
277 // Constant already in constant pool. Try to merge the two constants
278 if (CPV->hasName() && !C->hasName()) {
279 // Merge the two values, we inherit the existing CPV's name.
280 // InsertValue requires that the value have no name to insert correctly
281 // (because we want to fill the slot this constant would have filled)
282 //
283 string Name = CPV->getName();
284 CPV->setName("");
285 InsertValue(CPV, ValTab);
286 CPV->setName(Name);
287 delete C;
288 return CPV;
289 } else if (!CPV->hasName() && C->hasName()) {
290 // If we have a name on this value and there isn't one in the const
291 // pool val already, propogate it.
292 //
293 CPV->setName(C->getName());
294 delete C; // Sorry, you're toast
295 return CPV;
296 } else if (CPV->hasName() && C->hasName()) {
297 // Both values have distinct names. We cannot merge them.
298 CP.insert(C);
299 InsertValue(C, ValTab);
300 return C;
301 } else if (!CPV->hasName() && !C->hasName()) {
302 // Neither value has a name, trivially merge them.
303 InsertValue(CPV, ValTab);
304 delete C;
305 return CPV;
306 }
307
308 assert(0 && "Not reached!");
309 return 0;
310 } else { // No duplication of value.
311 CP.insert(C);
312 InsertValue(C, ValTab);
313 return C;
314 }
315}
316
Chris Lattner8896eda2001-07-09 19:38:36 +0000317
318struct EqualsType {
319 const Type *T;
320 inline EqualsType(const Type *t) { T = t; }
321 inline bool operator()(const ConstPoolVal *CPV) const {
322 return static_cast<const ConstPoolType*>(CPV)->getValue() == T;
323 }
324};
325
326
327// checkNewType - We have to be careful to add all types referenced by the
328// program to the constant pool of the method or module. Because of this, we
329// often want to check to make sure that types used are in the constant pool,
330// and add them if they aren't. That's what this function does.
331//
332static const Type *checkNewType(const Type *Ty) {
333 ConstantPool &CP = CurMeth.CurrentMethod ?
334 CurMeth.CurrentMethod->getConstantPool() :
335 CurModule.CurrentModule->getConstantPool();
336
337 // Get the type type plane...
338 ConstantPool::PlaneType &P = CP.getPlane(Type::TypeTy);
339 ConstantPool::PlaneType::const_iterator PI = find_if(P.begin(), P.end(),
340 EqualsType(Ty));
341 if (PI == P.end()) {
342 vector<ValueList> &ValTab = CurMeth.CurrentMethod ?
343 CurMeth.Values : CurModule.Values;
344 ConstPoolVal *CPT = new ConstPoolType(Ty);
345 CP.insert(CPT);
346 InsertValue(CPT, ValTab);
347 }
348 return Ty;
349}
350
351
Chris Lattner00950542001-06-06 20:29:01 +0000352//===----------------------------------------------------------------------===//
353// RunVMAsmParser - Define an interface to this parser
354//===----------------------------------------------------------------------===//
355//
356Module *RunVMAsmParser(const ToolCommandLine &Opts, FILE *F) {
357 llvmAsmin = F;
358 CurOptions = &Opts;
359 llvmAsmlineno = 1; // Reset the current line number...
360
361 CurModule.CurrentModule = new Module(); // Allocate a new module to read
362 yyparse(); // Parse the file.
363 Module *Result = ParserResult;
364 CurOptions = 0;
365 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
366 ParserResult = 0;
367
368 return Result;
369}
370
371%}
372
373%union {
374 Module *ModuleVal;
375 Method *MethodVal;
376 MethodArgument *MethArgVal;
377 BasicBlock *BasicBlockVal;
378 TerminatorInst *TermInstVal;
379 Instruction *InstVal;
380 ConstPoolVal *ConstVal;
381 const Type *TypeVal;
382
383 list<MethodArgument*> *MethodArgList;
384 list<Value*> *ValueList;
385 list<const Type*> *TypeList;
Chris Lattnerc24d2082001-06-11 15:04:20 +0000386 list<pair<Value*, BasicBlock*> > *PHIList; // Represent the RHS of PHI node
Chris Lattner00950542001-06-06 20:29:01 +0000387 list<pair<ConstPoolVal*, BasicBlock*> > *JumpTable;
388 vector<ConstPoolVal*> *ConstVector;
389
390 int64_t SInt64Val;
391 uint64_t UInt64Val;
392 int SIntVal;
393 unsigned UIntVal;
394
395 char *StrVal; // This memory is allocated by strdup!
396 ValID ValIDVal; // May contain memory allocated by strdup
397
398 Instruction::UnaryOps UnaryOpVal;
399 Instruction::BinaryOps BinaryOpVal;
400 Instruction::TermOps TermOpVal;
401 Instruction::MemoryOps MemOpVal;
Chris Lattner027dcc52001-07-08 21:10:27 +0000402 Instruction::OtherOps OtherOpVal;
Chris Lattner00950542001-06-06 20:29:01 +0000403}
404
405%type <ModuleVal> Module MethodList
406%type <MethodVal> Method MethodHeader BasicBlockList
407%type <BasicBlockVal> BasicBlock InstructionList
408%type <TermInstVal> BBTerminatorInst
409%type <InstVal> Inst InstVal MemoryInst
410%type <ConstVal> ConstVal
Chris Lattner027dcc52001-07-08 21:10:27 +0000411%type <ConstVector> ConstVector UByteList
Chris Lattner00950542001-06-06 20:29:01 +0000412%type <MethodArgList> ArgList ArgListH
413%type <MethArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000414%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000415%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner00950542001-06-06 20:29:01 +0000416%type <TypeList> TypeList
417%type <JumpTable> JumpTable
418
419%type <ValIDVal> ValueRef ConstValueRef // Reference to a definition or BB
420
421// Tokens and types for handling constant integer values
422//
423// ESINT64VAL - A negative number within long long range
424%token <SInt64Val> ESINT64VAL
425
426// EUINT64VAL - A positive number within uns. long long range
427%token <UInt64Val> EUINT64VAL
428%type <SInt64Val> EINT64VAL
429
430%token <SIntVal> SINTVAL // Signed 32 bit ints...
431%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
432%type <SIntVal> INTVAL
433
434// Built in types...
435%type <TypeVal> Types TypesV SIntType UIntType IntType
436%token <TypeVal> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
437%token <TypeVal> FLOAT DOUBLE STRING TYPE LABEL
438
439%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
440%type <StrVal> OptVAR_ID OptAssign
441
442
Chris Lattner09083092001-07-08 04:57:15 +0000443%token IMPLEMENTATION TRUE FALSE BEGINTOK END DECLARE TO
Chris Lattner00950542001-06-06 20:29:01 +0000444
445// Basic Block Terminating Operators
446%token <TermOpVal> RET BR SWITCH
447
448// Unary Operators
449%type <UnaryOpVal> UnaryOps // all the unary operators
Chris Lattner71496b32001-07-08 19:03:27 +0000450%token <UnaryOpVal> NOT
Chris Lattner00950542001-06-06 20:29:01 +0000451
452// Binary Operators
453%type <BinaryOpVal> BinaryOps // all the binary operators
454%token <BinaryOpVal> ADD SUB MUL DIV REM
Chris Lattner027dcc52001-07-08 21:10:27 +0000455%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000456
457// Memory Instructions
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000458%token <MemoryOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000459
Chris Lattner027dcc52001-07-08 21:10:27 +0000460// Other Operators
461%type <OtherOpVal> ShiftOps
462%token <OtherOpVal> PHI CALL CAST SHL SHR
463
Chris Lattner00950542001-06-06 20:29:01 +0000464%start Module
465%%
466
467// Handle constant integer size restriction and conversion...
468//
469
470INTVAL : SINTVAL
471INTVAL : UINTVAL {
472 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
473 ThrowException("Value too large for type!");
474 $$ = (int32_t)$1;
475}
476
477
478EINT64VAL : ESINT64VAL // These have same type and can't cause problems...
479EINT64VAL : EUINT64VAL {
480 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
481 ThrowException("Value too large for type!");
482 $$ = (int64_t)$1;
483}
484
485// Types includes all predefined types... except void, because you can't do
486// anything with it except for certain specific things...
487//
Chris Lattnere98dda62001-07-14 06:10:16 +0000488// User defined types are added later...
Chris Lattner00950542001-06-06 20:29:01 +0000489//
490Types : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT
491Types : LONG | ULONG | FLOAT | DOUBLE | STRING | TYPE | LABEL
492
493// TypesV includes all of 'Types', but it also includes the void type.
494TypesV : Types | VOID
495
496// Operations that are notably excluded from this list include:
497// RET, BR, & SWITCH because they end basic blocks and are treated specially.
498//
Chris Lattner09083092001-07-08 04:57:15 +0000499UnaryOps : NOT
Chris Lattner00950542001-06-06 20:29:01 +0000500BinaryOps : ADD | SUB | MUL | DIV | REM
501BinaryOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE
Chris Lattner027dcc52001-07-08 21:10:27 +0000502ShiftOps : SHL | SHR
Chris Lattner00950542001-06-06 20:29:01 +0000503
Chris Lattnere98dda62001-07-14 06:10:16 +0000504// These are some types that allow classification if we only want a particular
505// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner00950542001-06-06 20:29:01 +0000506SIntType : LONG | INT | SHORT | SBYTE
507UIntType : ULONG | UINT | USHORT | UBYTE
508IntType : SIntType | UIntType
509
Chris Lattnere98dda62001-07-14 06:10:16 +0000510// OptAssign - Value producing statements have an optional assignment component
Chris Lattner00950542001-06-06 20:29:01 +0000511OptAssign : VAR_ID '=' {
512 $$ = $1;
513 }
514 | /*empty*/ {
515 $$ = 0;
516 }
517
Chris Lattnere98dda62001-07-14 06:10:16 +0000518// ConstVal - The various declarations that go into the constant pool. This
519// includes all forward declarations of types, constants, and functions.
520//
Chris Lattner00950542001-06-06 20:29:01 +0000521ConstVal : SIntType EINT64VAL { // integral constants
522 if (!ConstPoolSInt::isValueValidForType($1, $2))
523 ThrowException("Constant value doesn't fit in type!");
524 $$ = new ConstPoolSInt($1, $2);
525 }
526 | UIntType EUINT64VAL { // integral constants
527 if (!ConstPoolUInt::isValueValidForType($1, $2))
528 ThrowException("Constant value doesn't fit in type!");
529 $$ = new ConstPoolUInt($1, $2);
530 }
531 | BOOL TRUE { // Boolean constants
532 $$ = new ConstPoolBool(true);
533 }
534 | BOOL FALSE { // Boolean constants
535 $$ = new ConstPoolBool(false);
536 }
537 | STRING STRINGCONSTANT { // String constants
538 cerr << "FIXME: TODO: String constants [sbyte] not implemented yet!\n";
539 abort();
540 //$$ = new ConstPoolString($2);
541 free($2);
542 }
543 | TYPE Types { // Type constants
544 $$ = new ConstPoolType($2);
545 }
546 | '[' Types ']' '[' ConstVector ']' { // Nonempty array constant
547 // Verify all elements are correct type!
548 const ArrayType *AT = ArrayType::getArrayType($2);
549 for (unsigned i = 0; i < $5->size(); i++) {
550 if ($2 != (*$5)[i]->getType())
551 ThrowException("Element #" + utostr(i) + " is not of type '" +
552 $2->getName() + "' as required!\nIt is of type '" +
553 (*$5)[i]->getType()->getName() + "'.");
554 }
555
556 $$ = new ConstPoolArray(AT, *$5);
557 delete $5;
558 }
559 | '[' Types ']' '[' ']' { // Empty array constant
560 vector<ConstPoolVal*> Empty;
561 $$ = new ConstPoolArray(ArrayType::getArrayType($2), Empty);
562 }
563 | '[' EUINT64VAL 'x' Types ']' '[' ConstVector ']' {
564 // Verify all elements are correct type!
565 const ArrayType *AT = ArrayType::getArrayType($4, (int)$2);
566 if ($2 != $7->size())
567 ThrowException("Type mismatch: constant sized array initialized with " +
568 utostr($7->size()) + " arguments, but has size of " +
569 itostr((int)$2) + "!");
570
571 for (unsigned i = 0; i < $7->size(); i++) {
572 if ($4 != (*$7)[i]->getType())
573 ThrowException("Element #" + utostr(i) + " is not of type '" +
574 $4->getName() + "' as required!\nIt is of type '" +
575 (*$7)[i]->getType()->getName() + "'.");
576 }
577
578 $$ = new ConstPoolArray(AT, *$7);
579 delete $7;
580 }
581 | '[' EUINT64VAL 'x' Types ']' '[' ']' {
582 if ($2 != 0)
583 ThrowException("Type mismatch: constant sized array initialized with 0"
584 " arguments, but has size of " + itostr((int)$2) + "!");
585 vector<ConstPoolVal*> Empty;
586 $$ = new ConstPoolArray(ArrayType::getArrayType($4, 0), Empty);
587 }
588 | '{' TypeList '}' '{' ConstVector '}' {
589 StructType::ElementTypes Types($2->begin(), $2->end());
590 delete $2;
591
592 const StructType *St = StructType::getStructType(Types);
593 $$ = new ConstPoolStruct(St, *$5);
594 delete $5;
595 }
596 | '{' '}' '{' '}' {
597 const StructType *St =
598 StructType::getStructType(StructType::ElementTypes());
599 vector<ConstPoolVal*> Empty;
600 $$ = new ConstPoolStruct(St, Empty);
601 }
602/*
603 | Types '*' ConstVal {
604 assert(0);
605 $$ = 0;
606 }
607*/
608
Chris Lattnere98dda62001-07-14 06:10:16 +0000609// ConstVector - A list of comma seperated constants.
Chris Lattner00950542001-06-06 20:29:01 +0000610ConstVector : ConstVector ',' ConstVal {
611 ($$ = $1)->push_back(addConstValToConstantPool($3));
612 }
613 | ConstVal {
614 $$ = new vector<ConstPoolVal*>();
615 $$->push_back(addConstValToConstantPool($1));
616 }
617
Chris Lattnere98dda62001-07-14 06:10:16 +0000618//ExternMethodDecl : EXTERNAL TypesV '(' TypeList ')' {
619// }
620//ExternVarDecl :
Chris Lattner00950542001-06-06 20:29:01 +0000621
Chris Lattnere98dda62001-07-14 06:10:16 +0000622// ConstPool - Constants with optional names assigned to them.
Chris Lattner00950542001-06-06 20:29:01 +0000623ConstPool : ConstPool OptAssign ConstVal {
624 if ($2) {
625 $3->setName($2);
626 free($2);
627 }
628
629 addConstValToConstantPool($3);
630 }
Chris Lattnere98dda62001-07-14 06:10:16 +0000631/*
632 | ConstPool OptAssign GlobalDecl { // Global declarations appear in CP
633 if ($2) {
634 $3->setName($2);
635 free($2);
636 }
637 //CurModule.CurrentModule->
638 }
639*/
Chris Lattner00950542001-06-06 20:29:01 +0000640 | /* empty: end of list */ {
641 }
642
643
644//===----------------------------------------------------------------------===//
645// Rules to match Modules
646//===----------------------------------------------------------------------===//
647
648// Module rule: Capture the result of parsing the whole file into a result
649// variable...
650//
651Module : MethodList {
652 $$ = ParserResult = $1;
653 CurModule.ModuleDone();
654}
655
Chris Lattnere98dda62001-07-14 06:10:16 +0000656// MethodList - A list of methods, preceeded by a constant pool.
657//
Chris Lattner00950542001-06-06 20:29:01 +0000658MethodList : MethodList Method {
659 $1->getMethodList().push_back($2);
660 CurMeth.MethodDone();
661 $$ = $1;
662 }
663 | ConstPool IMPLEMENTATION {
664 $$ = CurModule.CurrentModule;
665 }
666
667
668//===----------------------------------------------------------------------===//
669// Rules to match Method Headers
670//===----------------------------------------------------------------------===//
671
672OptVAR_ID : VAR_ID | /*empty*/ { $$ = 0; }
673
674ArgVal : Types OptVAR_ID {
675 $$ = new MethodArgument($1);
676 if ($2) { // Was the argument named?
677 $$->setName($2);
678 free($2); // The string was strdup'd, so free it now.
679 }
680}
681
682ArgListH : ArgVal ',' ArgListH {
683 $$ = $3;
684 $3->push_front($1);
685 }
686 | ArgVal {
687 $$ = new list<MethodArgument*>();
688 $$->push_front($1);
689 }
690
691ArgList : ArgListH {
692 $$ = $1;
693 }
694 | /* empty */ {
695 $$ = 0;
696 }
697
698MethodHeaderH : TypesV STRINGCONSTANT '(' ArgList ')' {
699 MethodType::ParamTypes ParamTypeList;
700 if ($4)
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000701 for (list<MethodArgument*>::iterator I = $4->begin(); I != $4->end(); ++I)
Chris Lattner00950542001-06-06 20:29:01 +0000702 ParamTypeList.push_back((*I)->getType());
703
704 const MethodType *MT = MethodType::getMethodType($1, ParamTypeList);
705
706 Method *M = new Method(MT, $2);
707 free($2); // Free strdup'd memory!
708
709 InsertValue(M, CurModule.Values);
710
711 CurMeth.MethodStart(M);
712
713 // Add all of the arguments we parsed to the method...
714 if ($4) { // Is null if empty...
715 Method::ArgumentListType &ArgList = M->getArgumentList();
716
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000717 for (list<MethodArgument*>::iterator I = $4->begin(); I != $4->end(); ++I) {
Chris Lattner00950542001-06-06 20:29:01 +0000718 InsertValue(*I);
719 ArgList.push_back(*I);
720 }
721 delete $4; // We're now done with the argument list
722 }
723}
724
725MethodHeader : MethodHeaderH ConstPool BEGINTOK {
726 $$ = CurMeth.CurrentMethod;
727}
728
729Method : BasicBlockList END {
730 $$ = $1;
731}
732
733
734//===----------------------------------------------------------------------===//
735// Rules to match Basic Blocks
736//===----------------------------------------------------------------------===//
737
738ConstValueRef : ESINT64VAL { // A reference to a direct constant
739 $$ = ValID::create($1);
740 }
741 | EUINT64VAL {
742 $$ = ValID::create($1);
743 }
744 | TRUE {
745 $$ = ValID::create((int64_t)1);
746 }
747 | FALSE {
748 $$ = ValID::create((int64_t)0);
749 }
750 | STRINGCONSTANT { // Quoted strings work too... especially for methods
751 $$ = ValID::create_conststr($1);
752 }
753
754// ValueRef - A reference to a definition...
755ValueRef : INTVAL { // Is it an integer reference...?
756 $$ = ValID::create($1);
757 }
758 | VAR_ID { // It must be a named reference then...
759 $$ = ValID::create($1);
760 }
761 | ConstValueRef {
762 $$ = $1;
763 }
764
765// The user may refer to a user defined type by its typeplane... check for this
766// now...
767//
768Types : ValueRef {
769 Value *D = getVal(Type::TypeTy, $1, true);
770 if (D == 0) ThrowException("Invalid user defined type: " + $1.getName());
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000771
772 // User defined type not in const pool!
773 ConstPoolType *CPT = (ConstPoolType*)D->castConstantAsserting();
Chris Lattner00950542001-06-06 20:29:01 +0000774 $$ = CPT->getValue();
775 }
776 | TypesV '(' TypeList ')' { // Method derived type?
777 MethodType::ParamTypes Params($3->begin(), $3->end());
778 delete $3;
Chris Lattner8896eda2001-07-09 19:38:36 +0000779 $$ = checkNewType(MethodType::getMethodType($1, Params));
Chris Lattner00950542001-06-06 20:29:01 +0000780 }
781 | TypesV '(' ')' { // Method derived type?
782 MethodType::ParamTypes Params; // Empty list
Chris Lattner8896eda2001-07-09 19:38:36 +0000783 $$ = checkNewType(MethodType::getMethodType($1, Params));
Chris Lattner00950542001-06-06 20:29:01 +0000784 }
785 | '[' Types ']' {
Chris Lattner8896eda2001-07-09 19:38:36 +0000786 $$ = checkNewType(ArrayType::getArrayType($2));
Chris Lattner00950542001-06-06 20:29:01 +0000787 }
788 | '[' EUINT64VAL 'x' Types ']' {
Chris Lattner8896eda2001-07-09 19:38:36 +0000789 $$ = checkNewType(ArrayType::getArrayType($4, (int)$2));
Chris Lattner00950542001-06-06 20:29:01 +0000790 }
791 | '{' TypeList '}' {
792 StructType::ElementTypes Elements($2->begin(), $2->end());
793 delete $2;
Chris Lattner8896eda2001-07-09 19:38:36 +0000794 $$ = checkNewType(StructType::getStructType(Elements));
Chris Lattner00950542001-06-06 20:29:01 +0000795 }
796 | '{' '}' {
Chris Lattner8896eda2001-07-09 19:38:36 +0000797 $$ = checkNewType(StructType::getStructType(StructType::ElementTypes()));
Chris Lattner00950542001-06-06 20:29:01 +0000798 }
799 | Types '*' {
Chris Lattner8896eda2001-07-09 19:38:36 +0000800 $$ = checkNewType(PointerType::getPointerType($1));
Chris Lattner00950542001-06-06 20:29:01 +0000801 }
802
803
804TypeList : Types {
805 $$ = new list<const Type*>();
806 $$->push_back($1);
807 }
808 | TypeList ',' Types {
809 ($$=$1)->push_back($3);
810 }
811
812
813BasicBlockList : BasicBlockList BasicBlock {
814 $1->getBasicBlocks().push_back($2);
815 $$ = $1;
816 }
817 | MethodHeader BasicBlock { // Do not allow methods with 0 basic blocks
818 $$ = $1; // in them...
819 $1->getBasicBlocks().push_back($2);
820 }
821
822
823// Basic blocks are terminated by branching instructions:
824// br, br/cc, switch, ret
825//
826BasicBlock : InstructionList BBTerminatorInst {
827 $1->getInstList().push_back($2);
828 InsertValue($1);
829 $$ = $1;
830 }
831 | LABELSTR InstructionList BBTerminatorInst {
832 $2->getInstList().push_back($3);
833 $2->setName($1);
834 free($1); // Free the strdup'd memory...
835
836 InsertValue($2);
837 $$ = $2;
838 }
839
840InstructionList : InstructionList Inst {
841 $1->getInstList().push_back($2);
842 $$ = $1;
843 }
844 | /* empty */ {
845 $$ = new BasicBlock();
846 }
847
848BBTerminatorInst : RET Types ValueRef { // Return with a result...
849 $$ = new ReturnInst(getVal($2, $3));
850 }
851 | RET VOID { // Return with no result...
852 $$ = new ReturnInst();
853 }
854 | BR LABEL ValueRef { // Unconditional Branch...
855 $$ = new BranchInst((BasicBlock*)getVal(Type::LabelTy, $3));
856 } // Conditional Branch...
857 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
858 $$ = new BranchInst((BasicBlock*)getVal(Type::LabelTy, $6),
859 (BasicBlock*)getVal(Type::LabelTy, $9),
860 getVal(Type::BoolTy, $3));
861 }
862 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
863 SwitchInst *S = new SwitchInst(getVal($2, $3),
864 (BasicBlock*)getVal(Type::LabelTy, $6));
865 $$ = S;
866
867 list<pair<ConstPoolVal*, BasicBlock*> >::iterator I = $8->begin(),
868 end = $8->end();
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000869 for (; I != end; ++I)
Chris Lattner00950542001-06-06 20:29:01 +0000870 S->dest_push_back(I->first, I->second);
871 }
872
873JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
874 $$ = $1;
875 ConstPoolVal *V = (ConstPoolVal*)getVal($2, $3, true);
876 if (V == 0)
877 ThrowException("May only switch on a constant pool value!");
878
879 $$->push_back(make_pair(V, (BasicBlock*)getVal($5, $6)));
880 }
881 | IntType ConstValueRef ',' LABEL ValueRef {
882 $$ = new list<pair<ConstPoolVal*, BasicBlock*> >();
883 ConstPoolVal *V = (ConstPoolVal*)getVal($1, $2, true);
884
885 if (V == 0)
886 ThrowException("May only switch on a constant pool value!");
887
888 $$->push_back(make_pair(V, (BasicBlock*)getVal($4, $5)));
889 }
890
891Inst : OptAssign InstVal {
892 if ($1) // Is this definition named??
893 $2->setName($1); // if so, assign the name...
894
895 InsertValue($2);
896 $$ = $2;
897}
898
Chris Lattnerc24d2082001-06-11 15:04:20 +0000899PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
900 $$ = new list<pair<Value*, BasicBlock*> >();
901 $$->push_back(make_pair(getVal($1, $3),
902 (BasicBlock*)getVal(Type::LabelTy, $5)));
903 }
904 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
905 $$ = $1;
906 $1->push_back(make_pair(getVal($1->front().first->getType(), $4),
907 (BasicBlock*)getVal(Type::LabelTy, $6)));
908 }
909
910
911ValueRefList : Types ValueRef { // Used for call statements...
Chris Lattner00950542001-06-06 20:29:01 +0000912 $$ = new list<Value*>();
913 $$->push_back(getVal($1, $2));
914 }
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000915 | ValueRefList ',' Types ValueRef {
Chris Lattner00950542001-06-06 20:29:01 +0000916 $$ = $1;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000917 $1->push_back(getVal($3, $4));
Chris Lattner00950542001-06-06 20:29:01 +0000918 }
919
920// ValueRefListE - Just like ValueRefList, except that it may also be empty!
921ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; }
922
923InstVal : BinaryOps Types ValueRef ',' ValueRef {
Chris Lattnerbebd60d2001-06-25 07:31:31 +0000924 $$ = BinaryOperator::create($1, getVal($2, $3), getVal($2, $5));
Chris Lattner00950542001-06-06 20:29:01 +0000925 if ($$ == 0)
926 ThrowException("binary operator returned null!");
927 }
928 | UnaryOps Types ValueRef {
Chris Lattnerbebd60d2001-06-25 07:31:31 +0000929 $$ = UnaryOperator::create($1, getVal($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +0000930 if ($$ == 0)
931 ThrowException("unary operator returned null!");
Chris Lattner09083092001-07-08 04:57:15 +0000932 }
Chris Lattner027dcc52001-07-08 21:10:27 +0000933 | ShiftOps Types ValueRef ',' Types ValueRef {
934 if ($5 != Type::UByteTy) ThrowException("Shift amount must be ubyte!");
935 $$ = new ShiftInst($1, getVal($2, $3), getVal($5, $6));
936 }
Chris Lattner09083092001-07-08 04:57:15 +0000937 | CAST Types ValueRef TO Types {
Chris Lattner71496b32001-07-08 19:03:27 +0000938 $$ = new CastInst(getVal($2, $3), $5);
Chris Lattner09083092001-07-08 04:57:15 +0000939 }
Chris Lattnerc24d2082001-06-11 15:04:20 +0000940 | PHI PHIList {
941 const Type *Ty = $2->front().first->getType();
942 $$ = new PHINode(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000943 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +0000944 if ($2->front().first->getType() != Ty)
945 ThrowException("All elements of a PHI node must be of the same type!");
946 ((PHINode*)$$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +0000947 $2->pop_front();
948 }
949 delete $2; // Free the list...
950 }
951 | CALL Types ValueRef '(' ValueRefListE ')' {
952 if (!$2->isMethodType())
953 ThrowException("Can only call methods: invalid type '" +
954 $2->getName() + "'!");
955
956 const MethodType *Ty = (const MethodType*)$2;
957
958 Value *V = getVal(Ty, $3);
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000959 if (!V->isMethod() || V->getType() != Ty)
Chris Lattner00950542001-06-06 20:29:01 +0000960 ThrowException("Cannot call: " + $3.getName() + "!");
961
962 // Create or access a new type that corresponds to the function call...
963 vector<Value *> Params;
964
965 if ($5) {
966 // Pull out just the arguments...
967 Params.insert(Params.begin(), $5->begin(), $5->end());
968 delete $5;
969
970 // Loop through MethodType's arguments and ensure they are specified
971 // correctly!
972 //
973 MethodType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
974 unsigned i;
975 for (i = 0; i < Params.size() && I != Ty->getParamTypes().end(); ++i,++I){
976 if (Params[i]->getType() != *I)
977 ThrowException("Parameter " + utostr(i) + " is not of type '" +
978 (*I)->getName() + "'!");
979 }
980
981 if (i != Params.size() || I != Ty->getParamTypes().end())
982 ThrowException("Invalid number of parameters detected!");
983 }
984
985 // Create the call node...
986 $$ = new CallInst((Method*)V, Params);
987 }
988 | MemoryInst {
989 $$ = $1;
990 }
991
Chris Lattner027dcc52001-07-08 21:10:27 +0000992// UByteList - List of ubyte values for load and store instructions
993UByteList : ',' ConstVector {
994 $$ = $2;
995} | /* empty */ {
996 $$ = new vector<ConstPoolVal*>();
997}
998
Chris Lattner00950542001-06-06 20:29:01 +0000999MemoryInst : MALLOC Types {
Chris Lattner8896eda2001-07-09 19:38:36 +00001000 $$ = new MallocInst(checkNewType(PointerType::getPointerType($2)));
Chris Lattner00950542001-06-06 20:29:01 +00001001 }
1002 | MALLOC Types ',' UINT ValueRef {
1003 if (!$2->isArrayType() || ((const ArrayType*)$2)->isSized())
1004 ThrowException("Trying to allocate " + $2->getName() +
1005 " as unsized array!");
Chris Lattner8896eda2001-07-09 19:38:36 +00001006 const Type *Ty = checkNewType(PointerType::getPointerType($2));
1007 $$ = new MallocInst(Ty, getVal($4, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001008 }
1009 | ALLOCA Types {
Chris Lattner8896eda2001-07-09 19:38:36 +00001010 $$ = new AllocaInst(checkNewType(PointerType::getPointerType($2)));
Chris Lattner00950542001-06-06 20:29:01 +00001011 }
1012 | ALLOCA Types ',' UINT ValueRef {
1013 if (!$2->isArrayType() || ((const ArrayType*)$2)->isSized())
1014 ThrowException("Trying to allocate " + $2->getName() +
1015 " as unsized array!");
Chris Lattner8896eda2001-07-09 19:38:36 +00001016 const Type *Ty = checkNewType(PointerType::getPointerType($2));
Chris Lattner00950542001-06-06 20:29:01 +00001017 Value *ArrSize = getVal($4, $5);
Chris Lattnerf0d0e9c2001-07-07 08:36:30 +00001018 $$ = new AllocaInst(Ty, ArrSize);
Chris Lattner00950542001-06-06 20:29:01 +00001019 }
1020 | FREE Types ValueRef {
1021 if (!$2->isPointerType())
1022 ThrowException("Trying to free nonpointer type " + $2->getName() + "!");
1023 $$ = new FreeInst(getVal($2, $3));
1024 }
1025
Chris Lattner027dcc52001-07-08 21:10:27 +00001026 | LOAD Types ValueRef UByteList {
1027 if (!$2->isPointerType())
1028 ThrowException("Can't load from nonpointer type: " + $2->getName());
1029 if (LoadInst::getIndexedType($2, *$4) == 0)
1030 ThrowException("Invalid indices for load instruction!");
1031
1032 $$ = new LoadInst(getVal($2, $3), *$4);
1033 delete $4; // Free the vector...
1034 }
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001035 | STORE Types ValueRef ',' Types ValueRef UByteList {
1036 if (!$5->isPointerType())
1037 ThrowException("Can't store to a nonpointer type: " + $5->getName());
1038 const Type *ElTy = StoreInst::getIndexedType($5, *$7);
1039 if (ElTy == 0)
1040 ThrowException("Can't store into that field list!");
1041 if (ElTy != $2)
1042 ThrowException("Can't store '" + $2->getName() + "' into space of type '"+
1043 ElTy->getName() + "'!");
1044 $$ = new StoreInst(getVal($2, $3), getVal($5, $6), *$7);
1045 delete $7;
1046 }
1047 | GETELEMENTPTR Types ValueRef UByteList {
1048 if (!$2->isPointerType())
1049 ThrowException("getelementptr insn requires pointer operand!");
1050 if (!GetElementPtrInst::getIndexedType($2, *$4, true))
1051 ThrowException("Can't get element ptr '" + $2->getName() + "'!");
1052 $$ = new GetElementPtrInst(getVal($2, $3), *$4);
1053 delete $4;
Chris Lattner8896eda2001-07-09 19:38:36 +00001054 checkNewType($$->getType());
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001055 }
Chris Lattner027dcc52001-07-08 21:10:27 +00001056
Chris Lattner00950542001-06-06 20:29:01 +00001057%%
Chris Lattner09083092001-07-08 04:57:15 +00001058int yyerror(const char *ErrorMsg) {
Chris Lattner00950542001-06-06 20:29:01 +00001059 ThrowException(string("Parse error: ") + ErrorMsg);
1060 return 0;
1061}