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 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 41 | extern std::istream* LexInput; |
Reid Spencer | 90eb4d6 | 2007-01-05 17:18:58 +0000 | [diff] [blame] | 42 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 43 | // UnEscapeLexed - Run through the specified buffer and change \xx codes to the |
| 44 | // appropriate character. If AllowNull is set to false, a \00 value will cause |
| 45 | // an error. |
| 46 | // |
| 47 | // If AllowNull is set to true, the return value of the function points to the |
| 48 | // last character of the string in memory. |
| 49 | // |
| 50 | char *UnEscapeLexed(char *Buffer, bool AllowNull = false); |
Reid Spencer | 90eb4d6 | 2007-01-05 17:18:58 +0000 | [diff] [blame] | 51 | |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 52 | /// InlineAsmDescriptor - This is a simple class that holds info about inline |
| 53 | /// asm blocks, for use by ValID. |
| 54 | struct InlineAsmDescriptor { |
| 55 | std::string AsmString, Constraints; |
| 56 | bool HasSideEffects; |
| 57 | |
| 58 | InlineAsmDescriptor(const std::string &as, const std::string &c, bool HSE) |
| 59 | : AsmString(as), Constraints(c), HasSideEffects(HSE) {} |
| 60 | }; |
| 61 | |
| 62 | |
| 63 | // ValID - Represents a reference of a definition of some sort. This may either |
| 64 | // be a numeric reference or a symbolic (%var) reference. This is just a |
| 65 | // discriminated union. |
| 66 | // |
| 67 | // Note that I can't implement this class in a straight forward manner with |
| 68 | // constructors and stuff because it goes in a union. |
| 69 | // |
| 70 | struct ValID { |
| 71 | enum { |
| 72 | NumberVal, NameVal, ConstSIntVal, ConstUIntVal, ConstFPVal, ConstNullVal, |
| 73 | ConstUndefVal, ConstZeroVal, ConstantVal, InlineAsmVal |
| 74 | } Type; |
| 75 | |
| 76 | union { |
| 77 | int Num; // If it's a numeric reference |
| 78 | char *Name; // If it's a named reference. Memory must be free'd. |
| 79 | int64_t ConstPool64; // Constant pool reference. This is the value |
| 80 | uint64_t UConstPool64;// Unsigned constant pool reference. |
| 81 | double ConstPoolFP; // Floating point constant pool reference |
| 82 | Constant *ConstantValue; // Fully resolved constant for ConstantVal case. |
| 83 | InlineAsmDescriptor *IAD; |
| 84 | }; |
| 85 | |
| 86 | static ValID create(int Num) { |
| 87 | ValID D; D.Type = NumberVal; D.Num = Num; return D; |
| 88 | } |
| 89 | |
| 90 | static ValID create(char *Name) { |
| 91 | ValID D; D.Type = NameVal; D.Name = Name; return D; |
| 92 | } |
| 93 | |
| 94 | static ValID create(int64_t Val) { |
| 95 | ValID D; D.Type = ConstSIntVal; D.ConstPool64 = Val; return D; |
| 96 | } |
| 97 | |
| 98 | static ValID create(uint64_t Val) { |
| 99 | ValID D; D.Type = ConstUIntVal; D.UConstPool64 = Val; return D; |
| 100 | } |
| 101 | |
| 102 | static ValID create(double Val) { |
| 103 | ValID D; D.Type = ConstFPVal; D.ConstPoolFP = Val; return D; |
| 104 | } |
| 105 | |
| 106 | static ValID createNull() { |
| 107 | ValID D; D.Type = ConstNullVal; return D; |
| 108 | } |
| 109 | |
| 110 | static ValID createUndef() { |
| 111 | ValID D; D.Type = ConstUndefVal; return D; |
| 112 | } |
| 113 | |
| 114 | static ValID createZeroInit() { |
| 115 | ValID D; D.Type = ConstZeroVal; return D; |
| 116 | } |
| 117 | |
| 118 | static ValID create(Constant *Val) { |
| 119 | ValID D; D.Type = ConstantVal; D.ConstantValue = Val; return D; |
| 120 | } |
| 121 | |
| 122 | static ValID createInlineAsm(const std::string &AsmString, |
| 123 | const std::string &Constraints, |
| 124 | bool HasSideEffects) { |
| 125 | ValID D; |
| 126 | D.Type = InlineAsmVal; |
| 127 | D.IAD = new InlineAsmDescriptor(AsmString, Constraints, HasSideEffects); |
| 128 | return D; |
| 129 | } |
| 130 | |
| 131 | inline void destroy() const { |
| 132 | if (Type == NameVal) |
| 133 | free(Name); // Free this strdup'd memory. |
| 134 | else if (Type == InlineAsmVal) |
| 135 | delete IAD; |
| 136 | } |
| 137 | |
| 138 | inline ValID copy() const { |
| 139 | if (Type != NameVal) return *this; |
| 140 | ValID Result = *this; |
| 141 | Result.Name = strdup(Name); |
| 142 | return Result; |
| 143 | } |
| 144 | |
| 145 | inline std::string getName() const { |
| 146 | switch (Type) { |
| 147 | case NumberVal : return std::string("#") + itostr(Num); |
| 148 | case NameVal : return Name; |
| 149 | case ConstFPVal : return ftostr(ConstPoolFP); |
| 150 | case ConstNullVal : return "null"; |
| 151 | case ConstUndefVal : return "undef"; |
| 152 | case ConstZeroVal : return "zeroinitializer"; |
| 153 | case ConstUIntVal : |
| 154 | case ConstSIntVal : return std::string("%") + itostr(ConstPool64); |
| 155 | case ConstantVal: |
| 156 | if (ConstantValue == ConstantInt::get(Type::Int1Ty, true)) |
| 157 | return "true"; |
| 158 | if (ConstantValue == ConstantInt::get(Type::Int1Ty, false)) |
| 159 | return "false"; |
| 160 | return "<constant expression>"; |
| 161 | default: |
| 162 | assert(0 && "Unknown value!"); |
| 163 | abort(); |
| 164 | return ""; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | bool operator<(const ValID &V) const { |
| 169 | if (Type != V.Type) return Type < V.Type; |
| 170 | switch (Type) { |
| 171 | case NumberVal: return Num < V.Num; |
| 172 | case NameVal: return strcmp(Name, V.Name) < 0; |
| 173 | case ConstSIntVal: return ConstPool64 < V.ConstPool64; |
| 174 | case ConstUIntVal: return UConstPool64 < V.UConstPool64; |
| 175 | case ConstFPVal: return ConstPoolFP < V.ConstPoolFP; |
| 176 | case ConstNullVal: return false; |
| 177 | case ConstUndefVal: return false; |
| 178 | case ConstZeroVal: return false; |
| 179 | case ConstantVal: return ConstantValue < V.ConstantValue; |
| 180 | default: assert(0 && "Unknown value type!"); return false; |
| 181 | } |
| 182 | } |
| 183 | }; |
| 184 | |
Reid Spencer | 9373d27 | 2007-01-26 17:13:53 +0000 | [diff] [blame^] | 185 | /// The following enums are used to keep track of prior opcodes. The lexer will |
| 186 | /// retain the ability to parse obsolete opcode mnemonics and generates semantic |
| 187 | /// values containing one of these enumerators. |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 188 | enum TermOps { |
| 189 | RetOp, BrOp, SwitchOp, InvokeOp, UnwindOp, UnreachableOp |
| 190 | }; |
| 191 | |
| 192 | enum BinaryOps { |
| 193 | AddOp, SubOp, MulOp, |
| 194 | DivOp, UDivOp, SDivOp, FDivOp, |
| 195 | RemOp, URemOp, SRemOp, FRemOp, |
| 196 | AndOp, OrOp, XorOp, |
| 197 | SetEQ, SetNE, SetLE, SetGE, SetLT, SetGT |
| 198 | }; |
| 199 | |
| 200 | enum MemoryOps { |
| 201 | MallocOp, FreeOp, AllocaOp, LoadOp, StoreOp, GetElementPtrOp |
| 202 | }; |
| 203 | |
| 204 | enum OtherOps { |
| 205 | PHIOp, CallOp, ShlOp, ShrOp, SelectOp, UserOp1, UserOp2, VAArg, |
| 206 | ExtractElementOp, InsertElementOp, ShuffleVectorOp, |
| 207 | ICmpOp, FCmpOp, |
| 208 | LShrOp, AShrOp |
| 209 | }; |
| 210 | |
| 211 | enum CastOps { |
| 212 | CastOp, TruncOp, ZExtOp, SExtOp, FPTruncOp, FPExtOp, FPToUIOp, FPToSIOp, |
| 213 | UIToFPOp, SIToFPOp, PtrToIntOp, IntToPtrOp, BitCastOp |
| 214 | }; |
| 215 | |
Reid Spencer | 9373d27 | 2007-01-26 17:13:53 +0000 | [diff] [blame^] | 216 | /// An enumeration for defining the Signedness of a type or value. Signless |
| 217 | /// means the signedness is not relevant to the type or value. |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 218 | enum Signedness { Signless, Unsigned, Signed }; |
| 219 | |
Reid Spencer | 9373d27 | 2007-01-26 17:13:53 +0000 | [diff] [blame^] | 220 | /// These structures are used as the semantic values returned from various |
| 221 | /// productions in the grammar. They simply bundle an LLVM IR object with |
| 222 | /// its Signedness value. These help track signedness through the various |
| 223 | /// productions. |
Reid Spencer | efd53d5 | 2007-01-26 08:18:34 +0000 | [diff] [blame] | 224 | struct TypeInfo { |
| 225 | const llvm::Type *T; |
| 226 | Signedness S; |
| 227 | }; |
| 228 | |
| 229 | struct PATypeInfo { |
| 230 | llvm::PATypeHolder* T; |
| 231 | Signedness S; |
| 232 | }; |
| 233 | |
| 234 | struct ConstInfo { |
| 235 | llvm::Constant* C; |
| 236 | Signedness S; |
| 237 | }; |
| 238 | |
| 239 | struct ValueInfo { |
| 240 | llvm::Value* V; |
| 241 | Signedness S; |
| 242 | }; |
| 243 | |
| 244 | struct InstrInfo { |
| 245 | llvm::Instruction *I; |
| 246 | Signedness S; |
| 247 | }; |
| 248 | |
| 249 | struct PHIListInfo { |
| 250 | std::list<std::pair<llvm::Value*, llvm::BasicBlock*> > *P; |
| 251 | Signedness S; |
| 252 | }; |
| 253 | |
| 254 | } // End llvm namespace |
Reid Spencer | 90eb4d6 | 2007-01-05 17:18:58 +0000 | [diff] [blame] | 255 | |
| 256 | #endif |