1 //===-- llvmAsmParser.y - Parser for llvm assembly files ---------*- C++ -*--=//
3 // This file implements the bison parser for LLVM assembly languages files.
5 //===------------------------------------------------------------------------=//
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.
14 #include "ParserInternals.h"
15 #include "llvm/Assembly/Parser.h"
16 #include "llvm/SymbolTable.h"
17 #include "llvm/Module.h"
18 #include "llvm/GlobalVariable.h"
19 #include "llvm/Method.h"
20 #include "llvm/BasicBlock.h"
21 #include "llvm/DerivedTypes.h"
22 #include "llvm/iTerminators.h"
23 #include "llvm/iMemory.h"
24 #include "llvm/Support/STLExtras.h"
25 #include "llvm/Support/DepthFirstIterator.h"
27 #include <utility> // Get definition of pair class
29 #include <stdio.h> // This embarasment is due to our flex lexer...
31 int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
32 int yylex(); // declaration" of xxx warnings.
35 static Module *ParserResult;
38 // DEBUG_UPREFS - Define this symbol if you want to enable debugging output
39 // relating to upreferences in the input stream.
41 //#define DEBUG_UPREFS 1
43 #define UR_OUT(X) cerr << X
48 // This contains info used when building the body of a method. It is destroyed
49 // when the method is completed.
51 typedef vector<Value *> ValueList; // Numbered defs
52 static void ResolveDefinitions(vector<ValueList> &LateResolvers);
53 static void ResolveTypes (vector<PATypeHolder<Type> > &LateResolveTypes);
55 static struct PerModuleInfo {
56 Module *CurrentModule;
57 vector<ValueList> Values; // Module level numbered definitions
58 vector<ValueList> LateResolveValues;
59 vector<PATypeHolder<Type> > Types, LateResolveTypes;
62 // If we could not resolve some methods at method compilation time (calls to
63 // methods before they are defined), resolve them now... Types are resolved
64 // when the constant pool has been completely parsed.
66 ResolveDefinitions(LateResolveValues);
68 Values.clear(); // Clear out method local definitions
74 static struct PerMethodInfo {
75 Method *CurrentMethod; // Pointer to current method being created
77 vector<ValueList> Values; // Keep track of numbered definitions
78 vector<ValueList> LateResolveValues;
79 vector<PATypeHolder<Type> > Types, LateResolveTypes;
80 bool isDeclare; // Is this method a forward declararation?
82 inline PerMethodInfo() {
87 inline ~PerMethodInfo() {}
89 inline void MethodStart(Method *M) {
94 // If we could not resolve some blocks at parsing time (forward branches)
95 // resolve the branches now...
96 ResolveDefinitions(LateResolveValues);
98 Values.clear(); // Clear out method local definitions
103 } CurMeth; // Info for the current method...
106 //===----------------------------------------------------------------------===//
107 // Code to handle definitions of all the types
108 //===----------------------------------------------------------------------===//
110 static void InsertValue(Value *D, vector<ValueList> &ValueTab = CurMeth.Values){
111 if (!D->hasName()) { // Is this a numbered definition?
112 unsigned type = D->getType()->getUniqueID();
113 if (ValueTab.size() <= type)
114 ValueTab.resize(type+1, ValueList());
115 //printf("Values[%d][%d] = %d\n", type, ValueTab[type].size(), D);
116 ValueTab[type].push_back(D);
120 // TODO: FIXME when Type are not const
121 static void InsertType(const Type *Ty, vector<PATypeHolder<Type> > &Types) {
125 static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
127 case 0: { // Is it a numbered definition?
128 unsigned Num = (unsigned)D.Num;
130 // Module constants occupy the lowest numbered slots...
131 if (Num < CurModule.Types.size())
132 return CurModule.Types[Num];
134 Num -= CurModule.Types.size();
136 // Check that the number is within bounds...
137 if (Num <= CurMeth.Types.size())
138 return CurMeth.Types[Num];
140 case 1: { // Is it a named definition?
142 SymbolTable *SymTab = 0;
143 if (CurMeth.CurrentMethod)
144 SymTab = CurMeth.CurrentMethod->getSymbolTable();
145 Value *N = SymTab ? SymTab->lookup(Type::TypeTy, Name) : 0;
148 // Symbol table doesn't automatically chain yet... because the method
149 // hasn't been added to the module...
151 SymTab = CurModule.CurrentModule->getSymbolTable();
153 N = SymTab->lookup(Type::TypeTy, Name);
157 D.destroy(); // Free old strdup'd memory...
158 return cast<const Type>(N);
161 ThrowException("Invalid symbol type reference!");
164 // If we reached here, we referenced either a symbol that we don't know about
165 // or an id number that hasn't been read yet. We may be referencing something
166 // forward, so just create an entry to be resolved later and get to it...
168 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
170 vector<PATypeHolder<Type> > *LateResolver = CurMeth.CurrentMethod ?
171 &CurMeth.LateResolveTypes : &CurModule.LateResolveTypes;
173 Type *Typ = new TypePlaceHolder(Type::TypeTy, D);
174 InsertType(Typ, *LateResolver);
178 static Value *lookupInSymbolTable(const Type *Ty, const string &Name) {
179 SymbolTable *SymTab =
180 CurMeth.CurrentMethod ? CurMeth.CurrentMethod->getSymbolTable() : 0;
181 Value *N = SymTab ? SymTab->lookup(Ty, Name) : 0;
184 // Symbol table doesn't automatically chain yet... because the method
185 // hasn't been added to the module...
187 SymTab = CurModule.CurrentModule->getSymbolTable();
189 N = SymTab->lookup(Ty, Name);
195 static Value *getVal(const Type *Ty, const ValID &D,
196 bool DoNotImprovise = false) {
197 assert(Ty != Type::TypeTy && "Should use getTypeVal for types!");
200 case ValID::NumberVal: { // Is it a numbered definition?
201 unsigned type = Ty->getUniqueID();
202 unsigned Num = (unsigned)D.Num;
204 // Module constants occupy the lowest numbered slots...
205 if (type < CurModule.Values.size()) {
206 if (Num < CurModule.Values[type].size())
207 return CurModule.Values[type][Num];
209 Num -= CurModule.Values[type].size();
212 // Make sure that our type is within bounds
213 if (CurMeth.Values.size() <= type)
216 // Check that the number is within bounds...
217 if (CurMeth.Values[type].size() <= Num)
220 return CurMeth.Values[type][Num];
222 case ValID::NameVal: { // Is it a named definition?
224 Value *N = lookupInSymbolTable(Ty, Name);
227 D.destroy(); // Free old strdup'd memory...
231 case ValID::ConstSIntVal: // Is it a constant pool reference??
232 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
233 case ValID::ConstStringVal: // Is it a string const pool reference?
234 case ValID::ConstFPVal: // Is it a floating point const pool reference?
235 case ValID::ConstNullVal: { // Is it a null value?
236 ConstPoolVal *CPV = 0;
238 // Check to make sure that "Ty" is an integral type, and that our
239 // value will fit into the specified type...
241 case ValID::ConstSIntVal:
242 if (Ty == Type::BoolTy) { // Special handling for boolean data
243 CPV = ConstPoolBool::get(D.ConstPool64 != 0);
245 if (!ConstPoolSInt::isValueValidForType(Ty, D.ConstPool64))
246 ThrowException("Symbolic constant pool value '" +
247 itostr(D.ConstPool64) + "' is invalid for type '" +
248 Ty->getName() + "'!");
249 CPV = ConstPoolSInt::get(Ty, D.ConstPool64);
252 case ValID::ConstUIntVal:
253 if (!ConstPoolUInt::isValueValidForType(Ty, D.UConstPool64)) {
254 if (!ConstPoolSInt::isValueValidForType(Ty, D.ConstPool64)) {
255 ThrowException("Integral constant pool reference is invalid!");
256 } else { // This is really a signed reference. Transmogrify.
257 CPV = ConstPoolSInt::get(Ty, D.ConstPool64);
260 CPV = ConstPoolUInt::get(Ty, D.UConstPool64);
263 case ValID::ConstStringVal:
264 cerr << "FIXME: TODO: String constants [sbyte] not implemented yet!\n";
267 case ValID::ConstFPVal:
268 if (!ConstPoolFP::isValueValidForType(Ty, D.ConstPoolFP))
269 ThrowException("FP constant invalid for type!!");
270 CPV = ConstPoolFP::get(Ty, D.ConstPoolFP);
272 case ValID::ConstNullVal:
273 if (!Ty->isPointerType())
274 ThrowException("Cannot create a a non pointer null!");
275 CPV = ConstPoolPointer::getNullPointer(cast<PointerType>(Ty));
278 assert(0 && "Unhandled case!");
280 assert(CPV && "How did we escape creating a constant??");
282 } // End of case 2,3,4
284 assert(0 && "Unhandled case!");
288 // If we reached here, we referenced either a symbol that we don't know about
289 // or an id number that hasn't been read yet. We may be referencing something
290 // forward, so just create an entry to be resolved later and get to it...
292 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
295 vector<ValueList> *LateResolver = (CurMeth.CurrentMethod) ?
296 &CurMeth.LateResolveValues : &CurModule.LateResolveValues;
298 if (isa<MethodType>(Ty))
299 ThrowException("Methods are not values and must be referenced as pointers");
301 if (const PointerType *PTy = dyn_cast<PointerType>(Ty))
302 if (const MethodType *MTy = dyn_cast<MethodType>(PTy->getValueType()))
303 Ty = MTy; // Convert pointer to method to method type
305 switch (Ty->getPrimitiveID()) {
306 case Type::LabelTyID: d = new BBPlaceHolder(Ty, D); break;
307 case Type::MethodTyID: d = new MethPlaceHolder(Ty, D);
308 LateResolver = &CurModule.LateResolveValues; break;
309 default: d = new ValuePlaceHolder(Ty, D); break;
312 assert(d != 0 && "How did we not make something?");
313 InsertValue(d, *LateResolver);
318 //===----------------------------------------------------------------------===//
319 // Code to handle forward references in instructions
320 //===----------------------------------------------------------------------===//
322 // This code handles the late binding needed with statements that reference
323 // values not defined yet... for example, a forward branch, or the PHI node for
326 // This keeps a table (CurMeth.LateResolveValues) of all such forward references
327 // and back patchs after we are done.
330 // ResolveDefinitions - If we could not resolve some defs at parsing
331 // time (forward branches, phi functions for loops, etc...) resolve the
334 static void ResolveDefinitions(vector<ValueList> &LateResolvers) {
335 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
336 for (unsigned ty = 0; ty < LateResolvers.size(); ty++) {
337 while (!LateResolvers[ty].empty()) {
338 Value *V = LateResolvers[ty].back();
339 LateResolvers[ty].pop_back();
340 ValID &DID = getValIDFromPlaceHolder(V);
342 Value *TheRealValue = getVal(Type::getUniqueIDType(ty), DID, true);
344 if (TheRealValue == 0) {
346 ThrowException("Reference to an invalid definition: '" +DID.getName()+
347 "' of type '" + V->getType()->getDescription() + "'",
348 getLineNumFromPlaceHolder(V));
350 ThrowException("Reference to an invalid definition: #" +
351 itostr(DID.Num) + " of type '" +
352 V->getType()->getDescription() + "'",
353 getLineNumFromPlaceHolder(V));
356 assert(!isa<Type>(V) && "Types should be in LateResolveTypes!");
358 V->replaceAllUsesWith(TheRealValue);
363 LateResolvers.clear();
366 // ResolveType - Take a specified unresolved type and resolve it. If there is
367 // nothing to resolve it to yet, return true. Otherwise resolve it and return
370 static bool ResolveType(PATypeHolder<Type> &T) {
372 ValID &DID = getValIDFromPlaceHolder(Ty);
374 const Type *TheRealType = getTypeVal(DID, true);
375 if (TheRealType == 0) return true;
377 // Refine the opaque type we had to the new type we are getting.
378 cast<DerivedType>(Ty)->refineAbstractTypeTo(TheRealType);
383 // ResolveTypes - This goes through the forward referenced type table and makes
384 // sure that all type references are complete. This code is executed after the
385 // constant pool of a method or module is completely parsed.
387 static void ResolveTypes(vector<PATypeHolder<Type> > &LateResolveTypes) {
388 while (!LateResolveTypes.empty()) {
389 if (ResolveType(LateResolveTypes.back())) {
390 const Type *Ty = LateResolveTypes.back();
391 ValID &DID = getValIDFromPlaceHolder(Ty);
393 if (DID.Type == ValID::NameVal)
394 ThrowException("Reference to an invalid type: '" +DID.getName(),
395 getLineNumFromPlaceHolder(Ty));
397 ThrowException("Reference to an invalid type: #" + itostr(DID.Num),
398 getLineNumFromPlaceHolder(Ty));
401 // No need to delete type, refine does that for us.
402 LateResolveTypes.pop_back();
407 // ResolveSomeTypes - This goes through the forward referenced type table and
408 // completes references that are now done. This is so that types are
409 // immediately resolved to be as concrete as possible. This does not cause
410 // thrown exceptions if not everything is resolved.
412 static void ResolveSomeTypes(vector<PATypeHolder<Type> > &LateResolveTypes) {
413 for (unsigned i = 0; i < LateResolveTypes.size(); ) {
414 if (ResolveType(LateResolveTypes[i]))
415 ++i; // Type didn't resolve
417 LateResolveTypes.erase(LateResolveTypes.begin()+i); // Type resolved!
422 // setValueName - Set the specified value to the name given. The name may be
423 // null potentially, in which case this is a noop. The string passed in is
424 // assumed to be a malloc'd string buffer, and is freed by this function.
426 static void setValueName(Value *V, char *NameStr) {
427 if (NameStr == 0) return;
428 string Name(NameStr); // Copy string
429 free(NameStr); // Free old string
431 SymbolTable *ST = CurMeth.CurrentMethod ?
432 CurMeth.CurrentMethod->getSymbolTableSure() :
433 CurModule.CurrentModule->getSymbolTableSure();
435 Value *Existing = ST->lookup(V->getType(), Name);
436 if (Existing) { // Inserting a name that is already defined???
437 // There is only one case where this is allowed: when we are refining an
438 // opaque type. In this case, Existing will be an opaque type.
439 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
440 if (OpaqueType *OpTy = dyn_cast<OpaqueType>(Ty)) {
441 // We ARE replacing an opaque type!
442 OpTy->refineAbstractTypeTo(cast<Type>(V));
447 // Otherwise, we are a simple redefinition of a value, check to see if it
448 // is defined the same as the old one...
449 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
450 if (Ty == cast<const Type>(V)) return; // Yes, it's equal.
451 cerr << "Type: " << Ty->getDescription() << " != "
452 << cast<const Type>(V)->getDescription() << "!\n";
456 ThrowException("Redefinition of value name '" + Name + "' in the '" +
457 V->getType()->getDescription() + "' type plane!");
460 V->setName(Name, ST);
464 //===----------------------------------------------------------------------===//
465 // Code for handling upreferences in type names...
468 // TypeContains - Returns true if Ty contains E in it.
470 static bool TypeContains(const Type *Ty, const Type *E) {
471 return find(df_begin(Ty), df_end(Ty), E) != df_end(Ty);
475 static vector<pair<unsigned, OpaqueType *> > UpRefs;
477 static PATypeHolder<Type> HandleUpRefs(const Type *ty) {
478 PATypeHolder<Type> Ty(ty);
479 UR_OUT(UpRefs.size() << " upreferences active!\n");
480 for (unsigned i = 0; i < UpRefs.size(); ) {
481 UR_OUT("TypeContains(" << Ty->getDescription() << ", "
482 << UpRefs[i].second->getDescription() << ") = "
483 << TypeContains(Ty, UpRefs[i].second) << endl);
484 if (TypeContains(Ty, UpRefs[i].second)) {
485 unsigned Level = --UpRefs[i].first; // Decrement level of upreference
486 UR_OUT("Uplevel Ref Level = " << Level << endl);
487 if (Level == 0) { // Upreference should be resolved!
488 UR_OUT("About to resolve upreference!\n";
489 string OldName = UpRefs[i].second->getDescription());
490 UpRefs[i].second->refineAbstractTypeTo(Ty);
491 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
492 UR_OUT("Type '" << OldName << "' refined upreference to: "
493 << (const void*)Ty << ", " << Ty->getDescription() << endl);
498 ++i; // Otherwise, no resolve, move on...
500 // FIXME: TODO: this should return the updated type
504 template <class TypeTy>
505 inline static void TypeDone(PATypeHolder<TypeTy> *Ty) {
507 ThrowException("Invalid upreference in type: " + (*Ty)->getDescription());
510 // newTH - Allocate a new type holder for the specified type
511 template <class TypeTy>
512 inline static PATypeHolder<TypeTy> *newTH(const TypeTy *Ty) {
513 return new PATypeHolder<TypeTy>(Ty);
515 template <class TypeTy>
516 inline static PATypeHolder<TypeTy> *newTH(const PATypeHolder<TypeTy> &TH) {
517 return new PATypeHolder<TypeTy>(TH);
521 //===----------------------------------------------------------------------===//
522 // RunVMAsmParser - Define an interface to this parser
523 //===----------------------------------------------------------------------===//
525 Module *RunVMAsmParser(const string &Filename, FILE *F) {
527 CurFilename = Filename;
528 llvmAsmlineno = 1; // Reset the current line number...
530 CurModule.CurrentModule = new Module(); // Allocate a new module to read
531 yyparse(); // Parse the file.
532 Module *Result = ParserResult;
533 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
544 MethodArgument *MethArgVal;
545 BasicBlock *BasicBlockVal;
546 TerminatorInst *TermInstVal;
547 Instruction *InstVal;
548 ConstPoolVal *ConstVal;
550 const Type *PrimType;
551 PATypeHolder<Type> *TypeVal;
554 list<MethodArgument*> *MethodArgList;
555 list<Value*> *ValueList;
556 list<PATypeHolder<Type> > *TypeList;
557 list<pair<Value*, BasicBlock*> > *PHIList; // Represent the RHS of PHI node
558 list<pair<ConstPoolVal*, BasicBlock*> > *JumpTable;
559 vector<ConstPoolVal*> *ConstVector;
568 char *StrVal; // This memory is strdup'd!
569 ValID ValIDVal; // strdup'd memory maybe!
571 Instruction::UnaryOps UnaryOpVal;
572 Instruction::BinaryOps BinaryOpVal;
573 Instruction::TermOps TermOpVal;
574 Instruction::MemoryOps MemOpVal;
575 Instruction::OtherOps OtherOpVal;
578 %type <ModuleVal> Module MethodList
579 %type <MethodVal> Method MethodProto MethodHeader BasicBlockList
580 %type <BasicBlockVal> BasicBlock InstructionList
581 %type <TermInstVal> BBTerminatorInst
582 %type <InstVal> Inst InstVal MemoryInst
583 %type <ConstVal> ConstVal
584 %type <ConstVector> ConstVector UByteList
585 %type <MethodArgList> ArgList ArgListH
586 %type <MethArgVal> ArgVal
587 %type <PHIList> PHIList
588 %type <ValueList> ValueRefList ValueRefListE // For call param lists
589 %type <TypeList> TypeListI ArgTypeListI
590 %type <JumpTable> JumpTable
591 %type <BoolVal> GlobalType // GLOBAL or CONSTANT?
593 %type <ValIDVal> ValueRef ConstValueRef // Reference to a definition or BB
594 %type <ValueVal> ResolvedVal // <type> <valref> pair
595 // Tokens and types for handling constant integer values
597 // ESINT64VAL - A negative number within long long range
598 %token <SInt64Val> ESINT64VAL
600 // EUINT64VAL - A positive number within uns. long long range
601 %token <UInt64Val> EUINT64VAL
602 %type <SInt64Val> EINT64VAL
604 %token <SIntVal> SINTVAL // Signed 32 bit ints...
605 %token <UIntVal> UINTVAL // Unsigned 32 bit ints...
606 %type <SIntVal> INTVAL
607 %token <FPVal> FPVAL // Float or Double constant
610 %type <TypeVal> Types TypesV UpRTypes UpRTypesV
611 %type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
612 %token <TypeVal> OPAQUE
613 %token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
614 %token <PrimType> FLOAT DOUBLE TYPE LABEL
616 %token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
617 %type <StrVal> OptVAR_ID OptAssign
620 %token IMPLEMENTATION TRUE FALSE BEGINTOK END DECLARE GLOBAL CONSTANT UNINIT
621 %token TO DOTDOTDOT STRING NULL_TOK CONST
623 // Basic Block Terminating Operators
624 %token <TermOpVal> RET BR SWITCH
627 %type <UnaryOpVal> UnaryOps // all the unary operators
628 %token <UnaryOpVal> NOT
631 %type <BinaryOpVal> BinaryOps // all the binary operators
632 %token <BinaryOpVal> ADD SUB MUL DIV REM
633 %token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
635 // Memory Instructions
636 %token <MemoryOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
639 %type <OtherOpVal> ShiftOps
640 %token <OtherOpVal> PHI CALL CAST SHL SHR
645 // Handle constant integer size restriction and conversion...
650 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
651 ThrowException("Value too large for type!");
656 EINT64VAL : ESINT64VAL // These have same type and can't cause problems...
657 EINT64VAL : EUINT64VAL {
658 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
659 ThrowException("Value too large for type!");
663 // Operations that are notably excluded from this list include:
664 // RET, BR, & SWITCH because they end basic blocks and are treated specially.
667 BinaryOps : ADD | SUB | MUL | DIV | REM
668 BinaryOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE
671 // These are some types that allow classification if we only want a particular
672 // thing... for example, only a signed, unsigned, or integral type.
673 SIntType : LONG | INT | SHORT | SBYTE
674 UIntType : ULONG | UINT | USHORT | UBYTE
675 IntType : SIntType | UIntType
676 FPType : FLOAT | DOUBLE
678 // OptAssign - Value producing statements have an optional assignment component
679 OptAssign : VAR_ID '=' {
687 //===----------------------------------------------------------------------===//
688 // Types includes all predefined types... except void, because it can only be
689 // used in specific contexts (method returning void for example). To have
690 // access to it, a user must explicitly use TypesV.
693 // TypesV includes all of 'Types', but it also includes the void type.
694 TypesV : Types | VOID { $$ = newTH($1); }
695 UpRTypesV : UpRTypes | VOID { $$ = newTH($1); }
702 // Derived types are added later...
704 PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT
705 PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL
706 UpRTypes : OPAQUE | PrimType { $$ = newTH($1); }
707 UpRTypes : ValueRef { // Named types are also simple types...
708 $$ = newTH(getTypeVal($1));
711 // Include derived types in the Types production.
713 UpRTypes : '\\' EUINT64VAL { // Type UpReference
714 if ($2 > (uint64_t)INT64_MAX) ThrowException("Value out of range!");
715 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
716 UpRefs.push_back(make_pair((unsigned)$2, OT)); // Add to vector...
717 $$ = newTH<Type>(OT);
718 UR_OUT("New Upreference!\n");
720 | UpRTypesV '(' ArgTypeListI ')' { // Method derived type?
721 vector<const Type*> Params;
722 mapto($3->begin(), $3->end(), back_inserter(Params),
723 mem_fun_ref(&PATypeHandle<Type>::get));
724 $$ = newTH(HandleUpRefs(MethodType::get(*$1, Params)));
725 delete $3; // Delete the argument list
726 delete $1; // Delete the old type handle
728 | '[' UpRTypesV ']' { // Unsized array type?
729 $$ = newTH<Type>(HandleUpRefs(ArrayType::get(*$2)));
732 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
733 $$ = newTH<Type>(HandleUpRefs(ArrayType::get(*$4, (int)$2)));
736 | '{' TypeListI '}' { // Structure type?
737 vector<const Type*> Elements;
738 mapto($2->begin(), $2->end(), back_inserter(Elements),
739 mem_fun_ref(&PATypeHandle<Type>::get));
741 $$ = newTH<Type>(HandleUpRefs(StructType::get(Elements)));
744 | '{' '}' { // Empty structure type?
745 $$ = newTH<Type>(StructType::get(vector<const Type*>()));
747 | UpRTypes '*' { // Pointer type?
748 $$ = newTH<Type>(HandleUpRefs(PointerType::get(*$1)));
752 // TypeList - Used for struct declarations and as a basis for method type
753 // declaration type lists
755 TypeListI : UpRTypes {
756 $$ = new list<PATypeHolder<Type> >();
757 $$->push_back(*$1); delete $1;
759 | TypeListI ',' UpRTypes {
760 ($$=$1)->push_back(*$3); delete $3;
763 // ArgTypeList - List of types for a method type declaration...
764 ArgTypeListI : TypeListI
765 | TypeListI ',' DOTDOTDOT {
766 ($$=$1)->push_back(Type::VoidTy);
769 ($$ = new list<PATypeHolder<Type> >())->push_back(Type::VoidTy);
772 $$ = new list<PATypeHolder<Type> >();
776 // ConstVal - The various declarations that go into the constant pool. This
777 // includes all forward declarations of types, constants, and functions.
779 ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
780 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
782 ThrowException("Cannot make array constant with type: '" +
783 (*$1)->getDescription() + "'!");
784 const Type *ETy = ATy->getElementType();
785 int NumElements = ATy->getNumElements();
787 // Verify that we have the correct size...
788 if (NumElements != -1 && NumElements != (int)$3->size())
789 ThrowException("Type mismatch: constant sized array initialized with " +
790 utostr($3->size()) + " arguments, but has size of " +
791 itostr(NumElements) + "!");
793 // Verify all elements are correct type!
794 for (unsigned i = 0; i < $3->size(); i++) {
795 if (ETy != (*$3)[i]->getType())
796 ThrowException("Element #" + utostr(i) + " is not of type '" +
797 ETy->getName() + "' as required!\nIt is of type '" +
798 (*$3)[i]->getType()->getName() + "'.");
801 $$ = ConstPoolArray::get(ATy, *$3);
802 delete $1; delete $3;
805 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
807 ThrowException("Cannot make array constant with type: '" +
808 (*$1)->getDescription() + "'!");
810 int NumElements = ATy->getNumElements();
811 if (NumElements != -1 && NumElements != 0)
812 ThrowException("Type mismatch: constant sized array initialized with 0"
813 " arguments, but has size of " + itostr(NumElements) +"!");
814 $$ = ConstPoolArray::get(ATy, vector<ConstPoolVal*>());
817 | Types 'c' STRINGCONSTANT {
818 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
820 ThrowException("Cannot make array constant with type: '" +
821 (*$1)->getDescription() + "'!");
823 int NumElements = ATy->getNumElements();
824 const Type *ETy = ATy->getElementType();
825 char *EndStr = UnEscapeLexed($3, true);
826 if (NumElements != -1 && NumElements != (EndStr-$3))
827 ThrowException("Can't build string constant of size " +
828 itostr((int)(EndStr-$3)) +
829 " when array has size " + itostr(NumElements) + "!");
830 vector<ConstPoolVal*> Vals;
831 if (ETy == Type::SByteTy) {
832 for (char *C = $3; C != EndStr; ++C)
833 Vals.push_back(ConstPoolSInt::get(ETy, *C));
834 } else if (ETy == Type::UByteTy) {
835 for (char *C = $3; C != EndStr; ++C)
836 Vals.push_back(ConstPoolUInt::get(ETy, *C));
839 ThrowException("Cannot build string arrays of non byte sized elements!");
842 $$ = ConstPoolArray::get(ATy, Vals);
845 | Types '{' ConstVector '}' {
846 const StructType *STy = dyn_cast<const StructType>($1->get());
848 ThrowException("Cannot make struct constant with type: '" +
849 (*$1)->getDescription() + "'!");
850 // FIXME: TODO: Check to see that the constants are compatible with the type
852 $$ = ConstPoolStruct::get(STy, *$3);
853 delete $1; delete $3;
856 const PointerType *PTy = dyn_cast<const PointerType>($1->get());
858 ThrowException("Cannot make null pointer constant with type: '" +
859 (*$1)->getDescription() + "'!");
861 $$ = ConstPoolPointer::getNullPointer(PTy);
865 string Name($2); free($2); // Change to a responsible mem manager
866 const PointerType *Ty = dyn_cast<const PointerType>($1->get());
868 ThrowException("Global const reference must be a pointer type!");
870 Value *N = lookupInSymbolTable(Ty, Name);
872 ThrowException("Global pointer reference '%" + Name +
873 "' must be defined before use!");
875 // TODO FIXME: This should also allow methods... when common baseclass
877 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(N)) {
878 $$ = ConstPoolPointerReference::get(GV);
880 ThrowException("'%" + Name + "' is not a global value reference!");
887 ConstVal : SIntType EINT64VAL { // integral constants
888 if (!ConstPoolSInt::isValueValidForType($1, $2))
889 ThrowException("Constant value doesn't fit in type!");
890 $$ = ConstPoolSInt::get($1, $2);
892 | UIntType EUINT64VAL { // integral constants
893 if (!ConstPoolUInt::isValueValidForType($1, $2))
894 ThrowException("Constant value doesn't fit in type!");
895 $$ = ConstPoolUInt::get($1, $2);
897 | BOOL TRUE { // Boolean constants
898 $$ = ConstPoolBool::True;
900 | BOOL FALSE { // Boolean constants
901 $$ = ConstPoolBool::False;
903 | FPType FPVAL { // Float & Double constants
904 $$ = ConstPoolFP::get($1, $2);
907 // ConstVector - A list of comma seperated constants.
908 ConstVector : ConstVector ',' ConstVal {
909 ($$ = $1)->push_back($3);
912 $$ = new vector<ConstPoolVal*>();
917 // GlobalType - Match either GLOBAL or CONSTANT for global declarations...
918 GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; }
921 // ConstPool - Constants with optional names assigned to them.
922 ConstPool : ConstPool OptAssign CONST ConstVal {
923 setValueName($4, $2);
926 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
927 // TODO: FIXME when Type are not const
928 setValueName(const_cast<Type*>($4->get()), $2);
931 InsertType($4->get(),
932 CurMeth.CurrentMethod ? CurMeth.Types : CurModule.Types);
936 ResolveSomeTypes(CurMeth.CurrentMethod ? CurMeth.LateResolveTypes :
937 CurModule.LateResolveTypes);
939 | ConstPool MethodProto { // Method prototypes can be in const pool
941 | ConstPool OptAssign GlobalType ConstVal {
942 const Type *Ty = $4->getType();
943 // Global declarations appear in Constant Pool
944 ConstPoolVal *Initializer = $4;
945 if (Initializer == 0)
946 ThrowException("Global value initializer is not a constant!");
948 GlobalVariable *GV = new GlobalVariable(Ty, $3, Initializer);
949 setValueName(GV, $2);
951 CurModule.CurrentModule->getGlobalList().push_back(GV);
952 InsertValue(GV, CurModule.Values);
954 | ConstPool OptAssign UNINIT GlobalType Types {
955 const Type *Ty = *$5;
956 // Global declarations appear in Constant Pool
957 if (isa<ArrayType>(Ty) && cast<ArrayType>(Ty)->isUnsized()) {
958 ThrowException("Type '" + Ty->getDescription() +
959 "' is not a sized type!");
962 GlobalVariable *GV = new GlobalVariable(Ty, $4);
963 setValueName(GV, $2);
965 CurModule.CurrentModule->getGlobalList().push_back(GV);
966 InsertValue(GV, CurModule.Values);
968 | /* empty: end of list */ {
972 //===----------------------------------------------------------------------===//
973 // Rules to match Modules
974 //===----------------------------------------------------------------------===//
976 // Module rule: Capture the result of parsing the whole file into a result
979 Module : MethodList {
980 $$ = ParserResult = $1;
981 CurModule.ModuleDone();
984 // MethodList - A list of methods, preceeded by a constant pool.
986 MethodList : MethodList Method {
988 if (!$2->getParent())
989 $1->getMethodList().push_back($2);
990 CurMeth.MethodDone();
992 | MethodList MethodProto {
995 | ConstPool IMPLEMENTATION {
996 $$ = CurModule.CurrentModule;
997 // Resolve circular types before we parse the body of the module
998 ResolveTypes(CurModule.LateResolveTypes);
1002 //===----------------------------------------------------------------------===//
1003 // Rules to match Method Headers
1004 //===----------------------------------------------------------------------===//
1006 OptVAR_ID : VAR_ID | /*empty*/ { $$ = 0; }
1008 ArgVal : Types OptVAR_ID {
1009 $$ = new MethodArgument(*$1); delete $1;
1010 setValueName($$, $2);
1013 ArgListH : ArgVal ',' ArgListH {
1018 $$ = new list<MethodArgument*>();
1022 $$ = new list<MethodArgument*>();
1023 $$->push_back(new MethodArgument(Type::VoidTy));
1026 ArgList : ArgListH {
1033 MethodHeaderH : TypesV STRINGCONSTANT '(' ArgList ')' {
1035 vector<const Type*> ParamTypeList;
1037 for (list<MethodArgument*>::iterator I = $4->begin(); I != $4->end(); ++I)
1038 ParamTypeList.push_back((*I)->getType());
1040 const MethodType *MT = MethodType::get(*$1, ParamTypeList);
1041 const PointerType *PMT = PointerType::get(MT);
1045 if (SymbolTable *ST = CurModule.CurrentModule->getSymbolTable()) {
1046 if (Value *V = ST->lookup(PMT, $2)) { // Method already in symtab?
1047 M = cast<Method>(V);
1049 // Yes it is. If this is the case, either we need to be a forward decl,
1050 // or it needs to be.
1051 if (!CurMeth.isDeclare && !M->isExternal())
1052 ThrowException("Redefinition of method '" + string($2) + "'!");
1056 if (M == 0) { // Not already defined?
1057 M = new Method(MT, $2);
1058 InsertValue(M, CurModule.Values);
1061 free($2); // Free strdup'd memory!
1063 CurMeth.MethodStart(M);
1065 // Add all of the arguments we parsed to the method...
1066 if ($4 && !CurMeth.isDeclare) { // Is null if empty...
1067 Method::ArgumentListType &ArgList = M->getArgumentList();
1069 for (list<MethodArgument*>::iterator I = $4->begin(); I != $4->end(); ++I) {
1071 ArgList.push_back(*I);
1073 delete $4; // We're now done with the argument list
1077 MethodHeader : MethodHeaderH ConstPool BEGINTOK {
1078 $$ = CurMeth.CurrentMethod;
1080 // Resolve circular types before we parse the body of the method.
1081 ResolveTypes(CurMeth.LateResolveTypes);
1084 Method : BasicBlockList END {
1088 MethodProto : DECLARE { CurMeth.isDeclare = true; } MethodHeaderH {
1089 $$ = CurMeth.CurrentMethod;
1090 if (!$$->getParent())
1091 CurModule.CurrentModule->getMethodList().push_back($$);
1092 CurMeth.MethodDone();
1095 //===----------------------------------------------------------------------===//
1096 // Rules to match Basic Blocks
1097 //===----------------------------------------------------------------------===//
1099 ConstValueRef : ESINT64VAL { // A reference to a direct constant
1100 $$ = ValID::create($1);
1103 $$ = ValID::create($1);
1105 | FPVAL { // Perhaps it's an FP constant?
1106 $$ = ValID::create($1);
1109 $$ = ValID::create((int64_t)1);
1112 $$ = ValID::create((int64_t)0);
1115 $$ = ValID::createNull();
1119 | STRINGCONSTANT { // Quoted strings work too... especially for methods
1120 $$ = ValID::create_conststr($1);
1124 // ValueRef - A reference to a definition...
1125 ValueRef : INTVAL { // Is it an integer reference...?
1126 $$ = ValID::create($1);
1128 | VAR_ID { // Is it a named reference...?
1129 $$ = ValID::create($1);
1135 // ResolvedVal - a <type> <value> pair. This is used only in cases where the
1136 // type immediately preceeds the value reference, and allows complex constant
1137 // pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
1138 ResolvedVal : Types ValueRef {
1139 $$ = getVal(*$1, $2); delete $1;
1143 BasicBlockList : BasicBlockList BasicBlock {
1144 $1->getBasicBlocks().push_back($2);
1147 | MethodHeader BasicBlock { // Do not allow methods with 0 basic blocks
1148 $$ = $1; // in them...
1149 $1->getBasicBlocks().push_back($2);
1153 // Basic blocks are terminated by branching instructions:
1154 // br, br/cc, switch, ret
1156 BasicBlock : InstructionList BBTerminatorInst {
1157 $1->getInstList().push_back($2);
1161 | LABELSTR InstructionList BBTerminatorInst {
1162 $2->getInstList().push_back($3);
1163 setValueName($2, $1);
1169 InstructionList : InstructionList Inst {
1170 $1->getInstList().push_back($2);
1174 $$ = new BasicBlock();
1177 BBTerminatorInst : RET ResolvedVal { // Return with a result...
1178 $$ = new ReturnInst($2);
1180 | RET VOID { // Return with no result...
1181 $$ = new ReturnInst();
1183 | BR LABEL ValueRef { // Unconditional Branch...
1184 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
1185 } // Conditional Branch...
1186 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
1187 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1188 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
1189 getVal(Type::BoolTy, $3));
1191 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1192 SwitchInst *S = new SwitchInst(getVal($2, $3),
1193 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
1196 list<pair<ConstPoolVal*, BasicBlock*> >::iterator I = $8->begin(),
1198 for (; I != end; ++I)
1199 S->dest_push_back(I->first, I->second);
1202 JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1204 ConstPoolVal *V = cast<ConstPoolVal>(getVal($2, $3, true));
1206 ThrowException("May only switch on a constant pool value!");
1208 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($5, $6))));
1210 | IntType ConstValueRef ',' LABEL ValueRef {
1211 $$ = new list<pair<ConstPoolVal*, BasicBlock*> >();
1212 ConstPoolVal *V = cast<ConstPoolVal>(getVal($1, $2, true));
1215 ThrowException("May only switch on a constant pool value!");
1217 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($4, $5))));
1220 Inst : OptAssign InstVal {
1221 setValueName($2, $1); // Is this definition named?? if so, assign the name...
1227 PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
1228 $$ = new list<pair<Value*, BasicBlock*> >();
1229 $$->push_back(make_pair(getVal(*$1, $3),
1230 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
1233 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1235 $1->push_back(make_pair(getVal($1->front().first->getType(), $4),
1236 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
1240 ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
1241 $$ = new list<Value*>();
1244 | ValueRefList ',' ResolvedVal {
1249 // ValueRefListE - Just like ValueRefList, except that it may also be empty!
1250 ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; }
1252 InstVal : BinaryOps Types ValueRef ',' ValueRef {
1253 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1255 ThrowException("binary operator returned null!");
1258 | UnaryOps ResolvedVal {
1259 $$ = UnaryOperator::create($1, $2);
1261 ThrowException("unary operator returned null!");
1263 | ShiftOps ResolvedVal ',' ResolvedVal {
1264 if ($4->getType() != Type::UByteTy)
1265 ThrowException("Shift amount must be ubyte!");
1266 $$ = new ShiftInst($1, $2, $4);
1268 | CAST ResolvedVal TO Types {
1269 $$ = new CastInst($2, *$4);
1273 const Type *Ty = $2->front().first->getType();
1274 $$ = new PHINode(Ty);
1275 while ($2->begin() != $2->end()) {
1276 if ($2->front().first->getType() != Ty)
1277 ThrowException("All elements of a PHI node must be of the same type!");
1278 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
1281 delete $2; // Free the list...
1283 | CALL TypesV ValueRef '(' ValueRefListE ')' {
1284 const PointerType *PMTy;
1285 const MethodType *Ty;
1287 if (!(PMTy = dyn_cast<PointerType>($2->get())) ||
1288 !(Ty = dyn_cast<MethodType>(PMTy->getValueType()))) {
1289 // Pull out the types of all of the arguments...
1290 vector<const Type*> ParamTypes;
1292 for (list<Value*>::iterator I = $5->begin(), E = $5->end(); I != E; ++I)
1293 ParamTypes.push_back((*I)->getType());
1295 Ty = MethodType::get($2->get(), ParamTypes);
1296 PMTy = PointerType::get(Ty);
1300 Value *V = getVal(PMTy, $3); // Get the method we're calling...
1302 // Create the call node...
1303 if (!$5) { // Has no arguments?
1304 $$ = new CallInst(cast<Method>(V), vector<Value*>());
1305 } else { // Has arguments?
1306 // Loop through MethodType's arguments and ensure they are specified
1309 MethodType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1310 MethodType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
1311 list<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
1313 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1314 if ((*ArgI)->getType() != *I)
1315 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
1316 (*I)->getName() + "'!");
1318 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1319 ThrowException("Invalid number of parameters detected!");
1321 $$ = new CallInst(cast<Method>(V),
1322 vector<Value*>($5->begin(), $5->end()));
1330 // UByteList - List of ubyte values for load and store instructions
1331 UByteList : ',' ConstVector {
1334 $$ = new vector<ConstPoolVal*>();
1337 MemoryInst : MALLOC Types {
1338 $$ = new MallocInst(PointerType::get(*$2));
1341 | MALLOC Types ',' UINT ValueRef {
1342 if (!(*$2)->isArrayType() || cast<const ArrayType>($2->get())->isSized())
1343 ThrowException("Trying to allocate " + (*$2)->getName() +
1344 " as unsized array!");
1345 const Type *Ty = PointerType::get(*$2);
1346 $$ = new MallocInst(Ty, getVal($4, $5));
1350 $$ = new AllocaInst(PointerType::get(*$2));
1353 | ALLOCA Types ',' UINT ValueRef {
1354 if (!(*$2)->isArrayType() || cast<const ArrayType>($2->get())->isSized())
1355 ThrowException("Trying to allocate " + (*$2)->getName() +
1356 " as unsized array!");
1357 const Type *Ty = PointerType::get(*$2);
1358 Value *ArrSize = getVal($4, $5);
1359 $$ = new AllocaInst(Ty, ArrSize);
1362 | FREE ResolvedVal {
1363 if (!$2->getType()->isPointerType())
1364 ThrowException("Trying to free nonpointer type " +
1365 $2->getType()->getName() + "!");
1366 $$ = new FreeInst($2);
1369 | LOAD Types ValueRef UByteList {
1370 if (!(*$2)->isPointerType())
1371 ThrowException("Can't load from nonpointer type: " + (*$2)->getName());
1372 if (LoadInst::getIndexedType(*$2, *$4) == 0)
1373 ThrowException("Invalid indices for load instruction!");
1375 $$ = new LoadInst(getVal(*$2, $3), *$4);
1376 delete $4; // Free the vector...
1379 | STORE ResolvedVal ',' Types ValueRef UByteList {
1380 if (!(*$4)->isPointerType())
1381 ThrowException("Can't store to a nonpointer type: " + (*$4)->getName());
1382 const Type *ElTy = StoreInst::getIndexedType(*$4, *$6);
1384 ThrowException("Can't store into that field list!");
1385 if (ElTy != $2->getType())
1386 ThrowException("Can't store '" + $2->getType()->getName() +
1387 "' into space of type '" + ElTy->getName() + "'!");
1388 $$ = new StoreInst($2, getVal(*$4, $5), *$6);
1389 delete $4; delete $6;
1391 | GETELEMENTPTR Types ValueRef UByteList {
1392 if (!(*$2)->isPointerType())
1393 ThrowException("getelementptr insn requires pointer operand!");
1394 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
1395 ThrowException("Can't get element ptr '" + (*$2)->getName() + "'!");
1396 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
1397 delete $2; delete $4;
1401 int yyerror(const char *ErrorMsg) {
1402 ThrowException(string("Parse error: ") + ErrorMsg);