Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1 | //===-- ParserInternals.h - Definitions internal to the parser ---*- C++ -*--=// |
| 2 | // |
| 3 | // This header file defines the various variables that are shared among the |
| 4 | // different components of the parser... |
| 5 | // |
| 6 | //===----------------------------------------------------------------------===// |
| 7 | |
| 8 | #ifndef PARSER_INTERNALS_H |
| 9 | #define PARSER_INTERNALS_H |
| 10 | |
| 11 | #include <stdio.h> |
| 12 | #define __STDC_LIMIT_MACROS |
| 13 | |
| 14 | #include "llvm/InstrTypes.h" |
| 15 | #include "llvm/BasicBlock.h" |
| 16 | #include "llvm/ConstPoolVals.h" |
| 17 | #include "llvm/iOther.h" |
| 18 | #include "llvm/Method.h" |
| 19 | #include "llvm/Type.h" |
| 20 | #include "llvm/Assembly/Parser.h" |
Chris Lattner | 57dbb3a | 2001-07-23 17:46:59 +0000 | [diff] [blame] | 21 | #include "llvm/Support/StringExtras.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 22 | |
| 23 | class Module; |
| 24 | |
| 25 | // Global variables exported from the lexer... |
| 26 | extern FILE *llvmAsmin; |
| 27 | extern int llvmAsmlineno; |
| 28 | |
| 29 | // Globals exported by the parser... |
Chris Lattner | a285043 | 2001-07-22 18:36:00 +0000 | [diff] [blame] | 30 | extern string CurFilename; |
| 31 | Module *RunVMAsmParser(const string &Filename, FILE *F); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 32 | |
| 33 | |
| 34 | // ThrowException - Wrapper around the ParseException class that automatically |
| 35 | // fills in file line number and column number and options info. |
| 36 | // |
| 37 | // This also helps me because I keep typing 'throw new ParseException' instead |
| 38 | // of just 'throw ParseException'... sigh... |
| 39 | // |
| 40 | static inline void ThrowException(const string &message) { |
| 41 | // TODO: column number in exception |
Chris Lattner | a285043 | 2001-07-22 18:36:00 +0000 | [diff] [blame] | 42 | throw ParseException(CurFilename, message, llvmAsmlineno); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | // ValID - Represents a reference of a definition of some sort. This may either |
| 46 | // be a numeric reference or a symbolic (%var) reference. This is just a |
| 47 | // discriminated union. |
| 48 | // |
| 49 | // Note that I can't implement this class in a straight forward manner with |
| 50 | // constructors and stuff because it goes in a union, and GCC doesn't like |
| 51 | // putting classes with ctor's in unions. :( |
| 52 | // |
| 53 | struct ValID { |
| 54 | int Type; // 0 = number, 1 = name, 2 = const pool, |
Chris Lattner | 3d52b2f | 2001-07-15 00:17:01 +0000 | [diff] [blame] | 55 | // 3 = unsigned const pool, 4 = const string, |
| 56 | // 5 = const fp |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 57 | union { |
| 58 | int Num; // If it's a numeric reference |
| 59 | char *Name; // If it's a named reference. Memory must be free'd. |
| 60 | int64_t ConstPool64; // Constant pool reference. This is the value |
| 61 | uint64_t UConstPool64;// Unsigned constant pool reference. |
Chris Lattner | 3d52b2f | 2001-07-15 00:17:01 +0000 | [diff] [blame] | 62 | double ConstPoolFP; // Floating point constant pool reference |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | static ValID create(int Num) { |
| 66 | ValID D; D.Type = 0; D.Num = Num; return D; |
| 67 | } |
| 68 | |
| 69 | static ValID create(char *Name) { |
| 70 | ValID D; D.Type = 1; D.Name = Name; return D; |
| 71 | } |
| 72 | |
| 73 | static ValID create(int64_t Val) { |
| 74 | ValID D; D.Type = 2; D.ConstPool64 = Val; return D; |
| 75 | } |
| 76 | |
| 77 | static ValID create(uint64_t Val) { |
| 78 | ValID D; D.Type = 3; D.UConstPool64 = Val; return D; |
| 79 | } |
| 80 | |
| 81 | static ValID create_conststr(char *Name) { |
| 82 | ValID D; D.Type = 4; D.Name = Name; return D; |
| 83 | } |
| 84 | |
Chris Lattner | 3d52b2f | 2001-07-15 00:17:01 +0000 | [diff] [blame] | 85 | static ValID create(double Val) { |
| 86 | ValID D; D.Type = 5; D.ConstPoolFP = Val; return D; |
| 87 | } |
| 88 | |
Chris Lattner | bcbf6ba | 2001-07-26 16:29:15 +0000 | [diff] [blame] | 89 | inline void destroy() const { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 90 | if (Type == 1 || Type == 4) free(Name); // Free this strdup'd memory... |
| 91 | } |
| 92 | |
| 93 | inline ValID copy() const { |
| 94 | if (Type != 1 && Type != 4) return *this; |
| 95 | ValID Result = *this; |
| 96 | Result.Name = strdup(Name); |
| 97 | return Result; |
| 98 | } |
| 99 | |
| 100 | inline string getName() const { |
| 101 | switch (Type) { |
| 102 | case 0: return string("#") + itostr(Num); |
| 103 | case 1: return Name; |
| 104 | case 4: return string("\"") + Name + string("\""); |
Chris Lattner | 3d52b2f | 2001-07-15 00:17:01 +0000 | [diff] [blame] | 105 | case 5: return ftostr(ConstPoolFP); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 106 | default: return string("%") + itostr(ConstPool64); |
| 107 | } |
| 108 | } |
| 109 | }; |
| 110 | |
| 111 | |
| 112 | |
| 113 | template<class SuperType> |
| 114 | class PlaceholderDef : public SuperType { |
| 115 | ValID D; |
| 116 | // TODO: Placeholder def should hold Line #/Column # of definition in case |
| 117 | // there is an error resolving the defintition! |
| 118 | public: |
| 119 | PlaceholderDef(const Type *Ty, const ValID &d) : SuperType(Ty), D(d) {} |
| 120 | ValID &getDef() { return D; } |
| 121 | }; |
| 122 | |
| 123 | struct InstPlaceHolderHelper : public Instruction { |
| 124 | InstPlaceHolderHelper(const Type *Ty) : Instruction(Ty, UserOp1, "") {} |
| 125 | |
| 126 | virtual Instruction *clone() const { abort(); } |
Chris Lattner | a41f50d | 2001-07-07 19:24:15 +0000 | [diff] [blame] | 127 | virtual const char *getOpcodeName() const { return "placeholder"; } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | struct BBPlaceHolderHelper : public BasicBlock { |
| 131 | BBPlaceHolderHelper(const Type *Ty) : BasicBlock() { |
| 132 | assert(Ty->isLabelType()); |
| 133 | } |
| 134 | }; |
| 135 | |
| 136 | struct MethPlaceHolderHelper : public Method { |
| 137 | MethPlaceHolderHelper(const Type *Ty) |
| 138 | : Method((const MethodType*)Ty) { |
| 139 | assert(Ty->isMethodType() && "Method placeholders must be method types!"); |
| 140 | } |
| 141 | }; |
| 142 | |
| 143 | typedef PlaceholderDef<InstPlaceHolderHelper> DefPlaceHolder; |
| 144 | typedef PlaceholderDef<BBPlaceHolderHelper> BBPlaceHolder; |
| 145 | typedef PlaceholderDef<MethPlaceHolderHelper> MethPlaceHolder; |
| 146 | //typedef PlaceholderDef<ModulePlaceHolderHelper> ModulePlaceHolder; |
| 147 | |
| 148 | static inline ValID &getValIDFromPlaceHolder(Value *Def) { |
| 149 | switch (Def->getType()->getPrimitiveID()) { |
| 150 | case Type::LabelTyID: return ((BBPlaceHolder*)Def)->getDef(); |
| 151 | case Type::MethodTyID: return ((MethPlaceHolder*)Def)->getDef(); |
| 152 | //case Type::ModuleTyID: return ((ModulePlaceHolder*)Def)->getDef(); |
| 153 | default: return ((DefPlaceHolder*)Def)->getDef(); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | #endif |