Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===-- ParserInternals.h - Definitions internal to the parser --*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This header file defines the various variables that are shared among the |
| 11 | // different components of the parser... |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef PARSER_INTERNALS_H |
| 16 | #define PARSER_INTERNALS_H |
| 17 | |
| 18 | #include "llvm/Constants.h" |
| 19 | #include "llvm/DerivedTypes.h" |
| 20 | #include "llvm/ParameterAttributes.h" |
| 21 | #include "llvm/Function.h" |
| 22 | #include "llvm/Instructions.h" |
| 23 | #include "llvm/Assembly/Parser.h" |
| 24 | #include "llvm/ADT/StringExtras.h" |
Dale Johannesen | b9de9f0 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/APFloat.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 26 | |
| 27 | // Global variables exported from the lexer... |
| 28 | |
| 29 | extern int llvmAsmlineno; /// FIXME: Not threading friendly |
| 30 | extern llvm::ParseError* TheParseError; /// FIXME: Not threading friendly |
| 31 | |
| 32 | extern std::string &llvmAsmTextin; |
| 33 | |
| 34 | // functions exported from the lexer |
| 35 | void set_scan_file(FILE * F); |
| 36 | void set_scan_string (const char * str); |
| 37 | |
| 38 | // Globals exported by the parser... |
| 39 | extern char* llvmAsmtext; |
| 40 | extern int llvmAsmleng; |
| 41 | |
| 42 | namespace llvm { |
| 43 | class Module; |
| 44 | |
| 45 | // Globals exported by the parser... |
| 46 | extern std::string CurFilename; /// FIXME: Not threading friendly |
| 47 | |
| 48 | // RunVMAsmParser - Parse a file and return Module |
| 49 | Module *RunVMAsmParser(const std::string &Filename, FILE *F); |
| 50 | |
| 51 | // Parse a string directly |
| 52 | Module *RunVMAsmParser(const char * AsmString, Module * M); |
| 53 | |
| 54 | // UnEscapeLexed - Run through the specified buffer and change \xx codes to the |
| 55 | // appropriate character. |
| 56 | char *UnEscapeLexed(char *Buffer); |
| 57 | |
| 58 | // GenerateError - Wrapper around the ParseException class that automatically |
| 59 | // fills in file line number and column number and options info. |
| 60 | // |
| 61 | // This also helps me because I keep typing 'throw new ParseException' instead |
| 62 | // of just 'throw ParseException'... sigh... |
| 63 | // |
| 64 | extern void GenerateError(const std::string &message, int LineNo = -1); |
| 65 | |
| 66 | /// InlineAsmDescriptor - This is a simple class that holds info about inline |
| 67 | /// asm blocks, for use by ValID. |
| 68 | struct InlineAsmDescriptor { |
| 69 | std::string AsmString, Constraints; |
| 70 | bool HasSideEffects; |
| 71 | |
| 72 | InlineAsmDescriptor(const std::string &as, const std::string &c, bool HSE) |
| 73 | : AsmString(as), Constraints(c), HasSideEffects(HSE) {} |
| 74 | }; |
| 75 | |
| 76 | |
| 77 | // ValID - Represents a reference of a definition of some sort. This may either |
| 78 | // be a numeric reference or a symbolic (%var) reference. This is just a |
| 79 | // discriminated union. |
| 80 | // |
| 81 | // Note that I can't implement this class in a straight forward manner with |
| 82 | // constructors and stuff because it goes in a union. |
| 83 | // |
| 84 | struct ValID { |
| 85 | enum { |
| 86 | LocalID, GlobalID, LocalName, GlobalName, |
| 87 | ConstSIntVal, ConstUIntVal, ConstFPVal, ConstNullVal, |
| 88 | ConstUndefVal, ConstZeroVal, ConstantVal, InlineAsmVal |
| 89 | } Type; |
| 90 | |
| 91 | union { |
| 92 | unsigned Num; // If it's a numeric reference like %1234 |
| 93 | std::string *Name; // If it's a named reference. Memory must be deleted. |
| 94 | int64_t ConstPool64; // Constant pool reference. This is the value |
| 95 | uint64_t UConstPool64;// Unsigned constant pool reference. |
Dale Johannesen | b9de9f0 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 96 | APFloat *ConstPoolFP; // Floating point constant pool reference |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 97 | Constant *ConstantValue; // Fully resolved constant for ConstantVal case. |
| 98 | InlineAsmDescriptor *IAD; |
Dale Johannesen | b9de9f0 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 99 | }; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 100 | |
| 101 | static ValID createLocalID(unsigned Num) { |
| 102 | ValID D; D.Type = LocalID; D.Num = Num; return D; |
| 103 | } |
| 104 | static ValID createGlobalID(unsigned Num) { |
| 105 | ValID D; D.Type = GlobalID; D.Num = Num; return D; |
| 106 | } |
| 107 | static ValID createLocalName(const std::string &Name) { |
| 108 | ValID D; D.Type = LocalName; D.Name = new std::string(Name); return D; |
| 109 | } |
| 110 | static ValID createGlobalName(const std::string &Name) { |
| 111 | ValID D; D.Type = GlobalName; D.Name = new std::string(Name); return D; |
| 112 | } |
| 113 | |
| 114 | static ValID create(int64_t Val) { |
| 115 | ValID D; D.Type = ConstSIntVal; D.ConstPool64 = Val; return D; |
| 116 | } |
| 117 | |
| 118 | static ValID create(uint64_t Val) { |
| 119 | ValID D; D.Type = ConstUIntVal; D.UConstPool64 = Val; return D; |
| 120 | } |
| 121 | |
Dale Johannesen | b9de9f0 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 122 | static ValID create(APFloat *Val) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 123 | ValID D; D.Type = ConstFPVal; D.ConstPoolFP = Val; return D; |
| 124 | } |
| 125 | |
| 126 | static ValID createNull() { |
| 127 | ValID D; D.Type = ConstNullVal; return D; |
| 128 | } |
| 129 | |
| 130 | static ValID createUndef() { |
| 131 | ValID D; D.Type = ConstUndefVal; return D; |
| 132 | } |
| 133 | |
| 134 | static ValID createZeroInit() { |
| 135 | ValID D; D.Type = ConstZeroVal; return D; |
| 136 | } |
| 137 | |
| 138 | static ValID create(Constant *Val) { |
| 139 | ValID D; D.Type = ConstantVal; D.ConstantValue = Val; return D; |
| 140 | } |
| 141 | |
| 142 | static ValID createInlineAsm(const std::string &AsmString, |
| 143 | const std::string &Constraints, |
| 144 | bool HasSideEffects) { |
| 145 | ValID D; |
| 146 | D.Type = InlineAsmVal; |
| 147 | D.IAD = new InlineAsmDescriptor(AsmString, Constraints, HasSideEffects); |
| 148 | return D; |
| 149 | } |
| 150 | |
| 151 | inline void destroy() const { |
| 152 | if (Type == LocalName || Type == GlobalName) |
| 153 | delete Name; // Free this strdup'd memory. |
| 154 | else if (Type == InlineAsmVal) |
| 155 | delete IAD; |
| 156 | } |
| 157 | |
| 158 | inline ValID copy() const { |
| 159 | if (Type != LocalName && Type != GlobalName) return *this; |
| 160 | ValID Result = *this; |
| 161 | Result.Name = new std::string(*Name); |
| 162 | return Result; |
| 163 | } |
| 164 | |
| 165 | inline std::string getName() const { |
| 166 | switch (Type) { |
| 167 | case LocalID : return '%' + utostr(Num); |
| 168 | case GlobalID : return '@' + utostr(Num); |
| 169 | case LocalName : return *Name; |
| 170 | case GlobalName : return *Name; |
Dale Johannesen | b9de9f0 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 171 | case ConstFPVal : return ftostr(*ConstPoolFP); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 172 | case ConstNullVal : return "null"; |
| 173 | case ConstUndefVal : return "undef"; |
| 174 | case ConstZeroVal : return "zeroinitializer"; |
| 175 | case ConstUIntVal : |
| 176 | case ConstSIntVal : return std::string("%") + itostr(ConstPool64); |
| 177 | case ConstantVal: |
| 178 | if (ConstantValue == ConstantInt::getTrue()) return "true"; |
| 179 | if (ConstantValue == ConstantInt::getFalse()) return "false"; |
| 180 | return "<constant expression>"; |
| 181 | default: |
| 182 | assert(0 && "Unknown value!"); |
| 183 | abort(); |
| 184 | return ""; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | bool operator<(const ValID &V) const { |
| 189 | if (Type != V.Type) return Type < V.Type; |
| 190 | switch (Type) { |
| 191 | case LocalID: |
| 192 | case GlobalID: return Num < V.Num; |
| 193 | case LocalName: |
| 194 | case GlobalName: return *Name < *V.Name; |
| 195 | case ConstSIntVal: return ConstPool64 < V.ConstPool64; |
| 196 | case ConstUIntVal: return UConstPool64 < V.UConstPool64; |
Dale Johannesen | b9de9f0 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 197 | case ConstFPVal: return ConstPoolFP->compare(*V.ConstPoolFP) == |
| 198 | APFloat::cmpLessThan; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 199 | case ConstNullVal: return false; |
| 200 | case ConstUndefVal: return false; |
| 201 | case ConstZeroVal: return false; |
| 202 | case ConstantVal: return ConstantValue < V.ConstantValue; |
| 203 | default: assert(0 && "Unknown value type!"); return false; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | bool operator==(const ValID &V) const { |
| 208 | if (Type == V.Type) { |
| 209 | switch (Type) { |
| 210 | case LocalID: |
| 211 | case GlobalID: return Num == V.Num; |
| 212 | case LocalName: |
| 213 | case GlobalName: return *Name == *(V.Name); |
| 214 | case ConstSIntVal: return ConstPool64 == V.ConstPool64; |
| 215 | case ConstUIntVal: return UConstPool64 == V.UConstPool64; |
Dale Johannesen | b9de9f0 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 216 | case ConstFPVal: return ConstPoolFP->compare(*V.ConstPoolFP) == |
| 217 | APFloat::cmpEqual; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 218 | case ConstantVal: return ConstantValue == V.ConstantValue; |
| 219 | case ConstNullVal: return true; |
| 220 | case ConstUndefVal: return true; |
| 221 | case ConstZeroVal: return true; |
| 222 | default: assert(0 && "Unknown value type!"); return false; |
| 223 | } |
| 224 | } |
| 225 | return false; |
| 226 | } |
| 227 | }; |
| 228 | |
| 229 | struct TypeWithAttrs { |
| 230 | llvm::PATypeHolder *Ty; |
| 231 | uint16_t Attrs; |
| 232 | }; |
| 233 | |
| 234 | typedef std::vector<TypeWithAttrs> TypeWithAttrsList; |
| 235 | |
| 236 | struct ArgListEntry { |
| 237 | uint16_t Attrs; |
| 238 | llvm::PATypeHolder *Ty; |
| 239 | std::string *Name; |
| 240 | }; |
| 241 | |
| 242 | typedef std::vector<struct ArgListEntry> ArgListType; |
| 243 | |
Dale Johannesen | cfb19e6 | 2007-11-05 21:20:28 +0000 | [diff] [blame^] | 244 | struct ParamListEntry { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 245 | Value *Val; |
| 246 | uint16_t Attrs; |
| 247 | }; |
| 248 | |
Dale Johannesen | cfb19e6 | 2007-11-05 21:20:28 +0000 | [diff] [blame^] | 249 | typedef std::vector<ParamListEntry> ParamList; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 250 | |
| 251 | |
| 252 | } // End llvm namespace |
| 253 | |
| 254 | #endif |