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