Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 1 | //===-- ParserInternals.h - Definitions internal to the parser --*- C++ -*-===// |
Reid Spencer | 90eb4d6 | 2007-01-05 17:18:58 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 5 | // This file was developed by the LLVM research group and is distributed under |
Reid Spencer | 90eb4d6 | 2007-01-05 17:18:58 +0000 | [diff] [blame] | 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 10 | // This header file defines the various variables that are shared among the |
| 11 | // different components of the parser... |
Reid Spencer | 90eb4d6 | 2007-01-05 17:18:58 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 15 | #ifndef PARSER_INTERNALS_H |
| 16 | #define PARSER_INTERNALS_H |
Reid Spencer | 90eb4d6 | 2007-01-05 17:18:58 +0000 | [diff] [blame] | 17 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 18 | #include "llvm/Constants.h" |
| 19 | #include "llvm/DerivedTypes.h" |
| 20 | #include "llvm/Function.h" |
| 21 | #include "llvm/Instructions.h" |
| 22 | #include "llvm/ADT/StringExtras.h" |
| 23 | #include <list> |
| 24 | |
Reid Spencer | 90eb4d6 | 2007-01-05 17:18:58 +0000 | [diff] [blame] | 25 | |
Reid Spencer | e0a15bb | 2007-01-15 00:25:53 +0000 | [diff] [blame] | 26 | // Global variables exported from the lexer. |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 27 | extern int yydebug; |
| 28 | extern void error(const std::string& msg, int line = -1); |
Reid Spencer | 90eb4d6 | 2007-01-05 17:18:58 +0000 | [diff] [blame] | 29 | extern char* Upgradetext; |
| 30 | extern int Upgradeleng; |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 31 | extern int Upgradelineno; |
Reid Spencer | 90eb4d6 | 2007-01-05 17:18:58 +0000 | [diff] [blame] | 32 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 33 | namespace llvm { |
Reid Spencer | 90eb4d6 | 2007-01-05 17:18:58 +0000 | [diff] [blame] | 34 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 35 | |
| 36 | class Module; |
| 37 | Module* UpgradeAssembly(const std::string &infile, std::istream& in, |
| 38 | bool debug, bool addAttrs); |
| 39 | |
| 40 | |
| 41 | class SignedType : public IntegerType { |
| 42 | const IntegerType *base_type; |
| 43 | static SignedType *SByteTy; |
| 44 | static SignedType *SShortTy; |
| 45 | static SignedType *SIntTy; |
| 46 | static SignedType *SLongTy; |
| 47 | SignedType(const IntegerType* ITy); |
| 48 | public: |
| 49 | static const SignedType *get(const IntegerType* ITy); |
| 50 | |
| 51 | bool isSigned() const { return true; } |
| 52 | const IntegerType* getBaseType() const { |
| 53 | return base_type; |
| 54 | } |
| 55 | const IntegerType* resolve() const { |
| 56 | ForwardType = base_type; |
| 57 | return base_type; |
| 58 | } |
| 59 | |
| 60 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 61 | static inline bool classof(const SignedType *T) { return true; } |
| 62 | static inline bool classof(const Type *T); |
Reid Spencer | 90eb4d6 | 2007-01-05 17:18:58 +0000 | [diff] [blame] | 63 | }; |
| 64 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 65 | extern std::istream* LexInput; |
Reid Spencer | 90eb4d6 | 2007-01-05 17:18:58 +0000 | [diff] [blame] | 66 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 67 | // UnEscapeLexed - Run through the specified buffer and change \xx codes to the |
| 68 | // appropriate character. If AllowNull is set to false, a \00 value will cause |
| 69 | // an error. |
| 70 | // |
| 71 | // If AllowNull is set to true, the return value of the function points to the |
| 72 | // last character of the string in memory. |
| 73 | // |
| 74 | char *UnEscapeLexed(char *Buffer, bool AllowNull = false); |
Reid Spencer | 90eb4d6 | 2007-01-05 17:18:58 +0000 | [diff] [blame] | 75 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 76 | /// InlineAsmDescriptor - This is a simple class that holds info about inline |
| 77 | /// asm blocks, for use by ValID. |
| 78 | struct InlineAsmDescriptor { |
| 79 | std::string AsmString, Constraints; |
| 80 | bool HasSideEffects; |
| 81 | |
| 82 | InlineAsmDescriptor(const std::string &as, const std::string &c, bool HSE) |
| 83 | : AsmString(as), Constraints(c), HasSideEffects(HSE) {} |
| 84 | }; |
| 85 | |
| 86 | |
| 87 | // ValID - Represents a reference of a definition of some sort. This may either |
| 88 | // be a numeric reference or a symbolic (%var) reference. This is just a |
| 89 | // discriminated union. |
| 90 | // |
| 91 | // Note that I can't implement this class in a straight forward manner with |
| 92 | // constructors and stuff because it goes in a union. |
| 93 | // |
| 94 | struct ValID { |
| 95 | enum { |
| 96 | NumberVal, NameVal, ConstSIntVal, ConstUIntVal, ConstFPVal, ConstNullVal, |
| 97 | ConstUndefVal, ConstZeroVal, ConstantVal, InlineAsmVal |
| 98 | } Type; |
| 99 | |
| 100 | union { |
| 101 | int Num; // If it's a numeric reference |
| 102 | char *Name; // If it's a named reference. Memory must be free'd. |
| 103 | int64_t ConstPool64; // Constant pool reference. This is the value |
| 104 | uint64_t UConstPool64;// Unsigned constant pool reference. |
| 105 | double ConstPoolFP; // Floating point constant pool reference |
| 106 | Constant *ConstantValue; // Fully resolved constant for ConstantVal case. |
| 107 | InlineAsmDescriptor *IAD; |
| 108 | }; |
| 109 | |
| 110 | static ValID create(int Num) { |
| 111 | ValID D; D.Type = NumberVal; D.Num = Num; return D; |
| 112 | } |
| 113 | |
| 114 | static ValID create(char *Name) { |
| 115 | ValID D; D.Type = NameVal; D.Name = Name; return D; |
| 116 | } |
| 117 | |
| 118 | static ValID create(int64_t Val) { |
| 119 | ValID D; D.Type = ConstSIntVal; D.ConstPool64 = Val; return D; |
| 120 | } |
| 121 | |
| 122 | static ValID create(uint64_t Val) { |
| 123 | ValID D; D.Type = ConstUIntVal; D.UConstPool64 = Val; return D; |
| 124 | } |
| 125 | |
| 126 | static ValID create(double Val) { |
| 127 | ValID D; D.Type = ConstFPVal; D.ConstPoolFP = Val; return D; |
| 128 | } |
| 129 | |
| 130 | static ValID createNull() { |
| 131 | ValID D; D.Type = ConstNullVal; return D; |
| 132 | } |
| 133 | |
| 134 | static ValID createUndef() { |
| 135 | ValID D; D.Type = ConstUndefVal; return D; |
| 136 | } |
| 137 | |
| 138 | static ValID createZeroInit() { |
| 139 | ValID D; D.Type = ConstZeroVal; return D; |
| 140 | } |
| 141 | |
| 142 | static ValID create(Constant *Val) { |
| 143 | ValID D; D.Type = ConstantVal; D.ConstantValue = Val; return D; |
| 144 | } |
| 145 | |
| 146 | static ValID createInlineAsm(const std::string &AsmString, |
| 147 | const std::string &Constraints, |
| 148 | bool HasSideEffects) { |
| 149 | ValID D; |
| 150 | D.Type = InlineAsmVal; |
| 151 | D.IAD = new InlineAsmDescriptor(AsmString, Constraints, HasSideEffects); |
| 152 | return D; |
| 153 | } |
| 154 | |
| 155 | inline void destroy() const { |
| 156 | if (Type == NameVal) |
| 157 | free(Name); // Free this strdup'd memory. |
| 158 | else if (Type == InlineAsmVal) |
| 159 | delete IAD; |
| 160 | } |
| 161 | |
| 162 | inline ValID copy() const { |
| 163 | if (Type != NameVal) return *this; |
| 164 | ValID Result = *this; |
| 165 | Result.Name = strdup(Name); |
| 166 | return Result; |
| 167 | } |
| 168 | |
| 169 | inline std::string getName() const { |
| 170 | switch (Type) { |
| 171 | case NumberVal : return std::string("#") + itostr(Num); |
| 172 | case NameVal : return Name; |
| 173 | case ConstFPVal : return ftostr(ConstPoolFP); |
| 174 | case ConstNullVal : return "null"; |
| 175 | case ConstUndefVal : return "undef"; |
| 176 | case ConstZeroVal : return "zeroinitializer"; |
| 177 | case ConstUIntVal : |
| 178 | case ConstSIntVal : return std::string("%") + itostr(ConstPool64); |
| 179 | case ConstantVal: |
| 180 | if (ConstantValue == ConstantInt::get(Type::Int1Ty, true)) |
| 181 | return "true"; |
| 182 | if (ConstantValue == ConstantInt::get(Type::Int1Ty, false)) |
| 183 | return "false"; |
| 184 | return "<constant expression>"; |
| 185 | default: |
| 186 | assert(0 && "Unknown value!"); |
| 187 | abort(); |
| 188 | return ""; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | bool operator<(const ValID &V) const { |
| 193 | if (Type != V.Type) return Type < V.Type; |
| 194 | switch (Type) { |
| 195 | case NumberVal: return Num < V.Num; |
| 196 | case NameVal: return strcmp(Name, V.Name) < 0; |
| 197 | case ConstSIntVal: return ConstPool64 < V.ConstPool64; |
| 198 | case ConstUIntVal: return UConstPool64 < V.UConstPool64; |
| 199 | case ConstFPVal: return ConstPoolFP < V.ConstPoolFP; |
| 200 | case ConstNullVal: return false; |
| 201 | case ConstUndefVal: return false; |
| 202 | case ConstZeroVal: return false; |
| 203 | case ConstantVal: return ConstantValue < V.ConstantValue; |
| 204 | default: assert(0 && "Unknown value type!"); return false; |
| 205 | } |
| 206 | } |
| 207 | }; |
| 208 | |
| 209 | |
| 210 | // This structure is used to keep track of obsolete opcodes. The lexer will |
| 211 | // retain the ability to parse obsolete opcode mnemonics. In this case it will |
| 212 | // set "obsolete" to true and the opcode will be the replacement opcode. For |
| 213 | // example if "rem" is encountered then opcode will be set to "urem" and the |
| 214 | // "obsolete" flag will be true. If the opcode is not obsolete then "obsolete" |
| 215 | // will be false. |
| 216 | |
| 217 | enum TermOps { |
| 218 | RetOp, BrOp, SwitchOp, InvokeOp, UnwindOp, UnreachableOp |
| 219 | }; |
| 220 | |
| 221 | enum BinaryOps { |
| 222 | AddOp, SubOp, MulOp, |
| 223 | DivOp, UDivOp, SDivOp, FDivOp, |
| 224 | RemOp, URemOp, SRemOp, FRemOp, |
| 225 | AndOp, OrOp, XorOp, |
| 226 | SetEQ, SetNE, SetLE, SetGE, SetLT, SetGT |
| 227 | }; |
| 228 | |
| 229 | enum MemoryOps { |
| 230 | MallocOp, FreeOp, AllocaOp, LoadOp, StoreOp, GetElementPtrOp |
| 231 | }; |
| 232 | |
| 233 | enum OtherOps { |
| 234 | PHIOp, CallOp, ShlOp, ShrOp, SelectOp, UserOp1, UserOp2, VAArg, |
| 235 | ExtractElementOp, InsertElementOp, ShuffleVectorOp, |
| 236 | ICmpOp, FCmpOp, |
| 237 | LShrOp, AShrOp |
| 238 | }; |
| 239 | |
| 240 | enum CastOps { |
| 241 | CastOp, TruncOp, ZExtOp, SExtOp, FPTruncOp, FPExtOp, FPToUIOp, FPToSIOp, |
| 242 | UIToFPOp, SIToFPOp, PtrToIntOp, IntToPtrOp, BitCastOp |
| 243 | }; |
| 244 | |
| 245 | enum Signedness { Signless, Unsigned, Signed }; |
| 246 | |
| 247 | struct TypeInfo { |
| 248 | const llvm::Type *T; |
| 249 | Signedness S; |
| 250 | }; |
| 251 | |
| 252 | struct PATypeInfo { |
| 253 | llvm::PATypeHolder* T; |
| 254 | Signedness S; |
| 255 | }; |
| 256 | |
| 257 | struct ConstInfo { |
| 258 | llvm::Constant* C; |
| 259 | Signedness S; |
| 260 | }; |
| 261 | |
| 262 | struct ValueInfo { |
| 263 | llvm::Value* V; |
| 264 | Signedness S; |
| 265 | }; |
| 266 | |
| 267 | struct InstrInfo { |
| 268 | llvm::Instruction *I; |
| 269 | Signedness S; |
| 270 | }; |
| 271 | |
| 272 | struct PHIListInfo { |
| 273 | std::list<std::pair<llvm::Value*, llvm::BasicBlock*> > *P; |
| 274 | Signedness S; |
| 275 | }; |
| 276 | |
| 277 | } // End llvm namespace |
Reid Spencer | 90eb4d6 | 2007-01-05 17:18:58 +0000 | [diff] [blame] | 278 | |
| 279 | #endif |