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