1 //===-- ParserInternals.h - Definitions internal to the parser --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This header file defines the various variables that are shared among the
11 // different components of the parser...
13 //===----------------------------------------------------------------------===//
15 #ifndef PARSER_INTERNALS_H
16 #define PARSER_INTERNALS_H
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"
27 // Global variables exported from the lexer.
29 extern void error(const std::string& msg, int line = -1);
30 extern char* Upgradetext;
31 extern int Upgradeleng;
32 extern int Upgradelineno;
37 Module* UpgradeAssembly(const std::string &infile, std::istream& in,
38 bool debug, bool addAttrs);
40 extern std::istream* LexInput;
42 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
43 // appropriate character. If AllowNull is set to false, a \00 value will cause
46 // If AllowNull is set to true, the return value of the function points to the
47 // last character of the string in memory.
49 char *UnEscapeLexed(char *Buffer, bool AllowNull = false);
51 /// InlineAsmDescriptor - This is a simple class that holds info about inline
52 /// asm blocks, for use by ValID.
53 struct InlineAsmDescriptor {
54 std::string AsmString, Constraints;
57 InlineAsmDescriptor(const std::string &as, const std::string &c, bool HSE)
58 : AsmString(as), Constraints(c), HasSideEffects(HSE) {}
61 /// This class keeps track of the signedness of a type or value. It allows the
62 /// signedness of a composite type to be captured in a relatively simple form.
63 /// This is needed in order to retain the signedness of pre LLVM 2.0 types so
64 /// they can be upgraded properly. Signedness of composite types must be
65 /// captured in order to accurately get the signedness of a value through a
67 /// @brief Class to track signedness of types and values.
69 /// The basic kinds of signedness values.
71 Signless, ///< The type doesn't have any sign.
72 Unsigned, ///< The type is an unsigned integer.
73 Signed, ///< The type is a signed integer.
74 Named, ///< The type is a named type (probably forward ref or up ref).
75 Composite ///< The type is composite (struct, array, pointer).
79 /// @brief Keeps track of Signedness for composite types
80 typedef std::vector<Signedness> SignVector;
81 Kind kind; ///< The kind of signedness node
83 SignVector *sv; ///< The vector of Signedness for composite types
84 std::string *name; ///< The name of the type for named types.
87 /// The Signedness class is used as a member of a union so it cannot have
88 /// a constructor or assignment operator. This function suffices.
89 /// @brief Copy one signedness value to another
90 void copy(const Signedness &that);
91 /// The Signedness class is used as a member of a union so it cannot have
93 /// @brief Release memory, if any allocated.
96 /// @brief Make a Signless node.
97 void makeSignless() { kind = Signless; sv = 0; }
98 /// @brief Make a Signed node.
99 void makeSigned() { kind = Signed; sv = 0; }
100 /// @brief Make an Unsigned node.
101 void makeUnsigned() { kind = Unsigned; sv = 0; }
102 /// @brief Make a Named node.
103 void makeNamed(const std::string& nm){
104 kind = Named; name = new std::string(nm);
106 /// @brief Make an empty Composite node.
107 void makeComposite() { kind = Composite; sv = new SignVector(); }
108 /// @brief Make an Composite node, with the first element given.
109 void makeComposite(const Signedness &S) {
111 sv = new SignVector();
114 /// @brief Add an element to a Composite node.
115 void add(const Signedness &S) {
116 assert(isComposite() && "Must be composite to use add");
119 bool operator<(const Signedness &that) const;
120 bool operator==(const Signedness &that) const;
121 bool isSigned() const { return kind == Signed; }
122 bool isUnsigned() const { return kind == Unsigned; }
123 bool isSignless() const { return kind == Signless; }
124 bool isNamed() const { return kind == Named; }
125 bool isComposite() const { return kind == Composite; }
126 /// This is used by GetElementPtr to extract the sign of an element.
127 /// @brief Get a specific element from a Composite node.
128 Signedness get(uint64_t idx) const {
129 assert(isComposite() && "Invalid Signedness type for get()");
130 assert(sv && idx < sv->size() && "Invalid index");
133 /// @brief Get the name from a Named node.
134 const std::string& getName() const {
135 assert(isNamed() && "Can't get name from non-name Sign");
144 // ValID - Represents a reference of a definition of some sort. This may either
145 // be a numeric reference or a symbolic (%var) reference. This is just a
146 // discriminated union.
148 // Note that I can't implement this class in a straight forward manner with
149 // constructors and stuff because it goes in a union.
153 NumberVal, NameVal, ConstSIntVal, ConstUIntVal, ConstFPVal, ConstNullVal,
154 ConstUndefVal, ConstZeroVal, ConstantVal, InlineAsmVal
158 int Num; // If it's a numeric reference
159 char *Name; // If it's a named reference. Memory must be free'd.
160 int64_t ConstPool64; // Constant pool reference. This is the value
161 uint64_t UConstPool64;// Unsigned constant pool reference.
162 double ConstPoolFP; // Floating point constant pool reference
163 Constant *ConstantValue; // Fully resolved constant for ConstantVal case.
164 InlineAsmDescriptor *IAD;
168 static ValID create(int Num) {
169 ValID D; D.Type = NumberVal; D.Num = Num; D.S.makeSignless();
173 static ValID create(char *Name) {
174 ValID D; D.Type = NameVal; D.Name = Name; D.S.makeSignless();
178 static ValID create(int64_t Val) {
179 ValID D; D.Type = ConstSIntVal; D.ConstPool64 = Val;
184 static ValID create(uint64_t Val) {
185 ValID D; D.Type = ConstUIntVal; D.UConstPool64 = Val;
190 static ValID create(double Val) {
191 ValID D; D.Type = ConstFPVal; D.ConstPoolFP = Val;
196 static ValID createNull() {
197 ValID D; D.Type = ConstNullVal;
202 static ValID createUndef() {
203 ValID D; D.Type = ConstUndefVal;
208 static ValID createZeroInit() {
209 ValID D; D.Type = ConstZeroVal;
214 static ValID create(Constant *Val) {
215 ValID D; D.Type = ConstantVal; D.ConstantValue = Val;
220 static ValID createInlineAsm(const std::string &AsmString,
221 const std::string &Constraints,
222 bool HasSideEffects) {
224 D.Type = InlineAsmVal;
225 D.IAD = new InlineAsmDescriptor(AsmString, Constraints, HasSideEffects);
230 inline void destroy() const {
232 free(Name); // Free this strdup'd memory.
233 else if (Type == InlineAsmVal)
237 inline ValID copy() const {
238 if (Type != NameVal) return *this;
239 ValID Result = *this;
240 Result.Name = strdup(Name);
244 inline std::string getName() const {
246 case NumberVal : return std::string("#") + itostr(Num);
247 case NameVal : return Name;
248 case ConstFPVal : return ftostr(ConstPoolFP);
249 case ConstNullVal : return "null";
250 case ConstUndefVal : return "undef";
251 case ConstZeroVal : return "zeroinitializer";
253 case ConstSIntVal : return std::string("%") + itostr(ConstPool64);
255 if (ConstantValue == ConstantInt::get(Type::Int1Ty, true))
257 if (ConstantValue == ConstantInt::get(Type::Int1Ty, false))
259 return "<constant expression>";
261 assert(0 && "Unknown value!");
267 bool operator<(const ValID &V) const {
268 if (Type != V.Type) return Type < V.Type;
270 case NumberVal: return Num < V.Num;
271 case NameVal: return strcmp(Name, V.Name) < 0;
272 case ConstSIntVal: return ConstPool64 < V.ConstPool64;
273 case ConstUIntVal: return UConstPool64 < V.UConstPool64;
274 case ConstFPVal: return ConstPoolFP < V.ConstPoolFP;
275 case ConstNullVal: return false;
276 case ConstUndefVal: return false;
277 case ConstZeroVal: return false;
278 case ConstantVal: return ConstantValue < V.ConstantValue;
279 default: assert(0 && "Unknown value type!"); return false;
284 /// The following enums are used to keep track of prior opcodes. The lexer will
285 /// retain the ability to parse obsolete opcode mnemonics and generates semantic
286 /// values containing one of these enumerators.
288 RetOp, BrOp, SwitchOp, InvokeOp, UnwindOp, UnreachableOp
293 DivOp, UDivOp, SDivOp, FDivOp,
294 RemOp, URemOp, SRemOp, FRemOp,
296 ShlOp, ShrOp, LShrOp, AShrOp,
297 SetEQ, SetNE, SetLE, SetGE, SetLT, SetGT
301 MallocOp, FreeOp, AllocaOp, LoadOp, StoreOp, GetElementPtrOp
305 PHIOp, CallOp, SelectOp, UserOp1, UserOp2, VAArg,
306 ExtractElementOp, InsertElementOp, ShuffleVectorOp,
311 CastOp, TruncOp, ZExtOp, SExtOp, FPTruncOp, FPExtOp, FPToUIOp, FPToSIOp,
312 UIToFPOp, SIToFPOp, PtrToIntOp, IntToPtrOp, BitCastOp
315 // An enumeration for the old calling conventions, ala LLVM 1.9
316 namespace OldCallingConv {
318 C = 0, CSRet = 1, Fast = 8, Cold = 9, X86_StdCall = 64, X86_FastCall = 65,
323 /// These structures are used as the semantic values returned from various
324 /// productions in the grammar. They simply bundle an LLVM IR object with
325 /// its Signedness value. These help track signedness through the various
330 bool operator<(const TypeInfo& that) const {
336 bool result = S < that.S;
337 //#define TYPEINFO_DEBUG
338 #ifdef TYPEINFO_DEBUG
339 std::cerr << (result?"true ":"false ") << T->getDescription() << " (";
341 std::cerr << ") < " << that.T->getDescription() << " (";
349 bool operator==(const TypeInfo& that) const {
352 return T == that.T && S == that.S;
354 void destroy() { S.destroy(); }
358 llvm::PATypeHolder* PAT;
360 void destroy() { S.destroy(); delete PAT; }
366 void destroy() { S.destroy(); }
372 void destroy() { S.destroy(); }
376 llvm::Instruction *I;
378 void destroy() { S.destroy(); }
381 struct TermInstInfo {
382 llvm::TerminatorInst *TI;
384 void destroy() { S.destroy(); }
388 std::list<std::pair<llvm::Value*, llvm::BasicBlock*> > *P;
390 void destroy() { S.destroy(); delete P; }
393 } // End llvm namespace