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 APFloat
*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(APFloat
* 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
->compare(*V
.ConstPoolFP
) ==
275 APFloat::cmpLessThan
;
276 case ConstNullVal
: return false;
277 case ConstUndefVal
: return false;
278 case ConstZeroVal
: return false;
279 case ConstantVal
: return ConstantValue
< V
.ConstantValue
;
280 default: assert(0 && "Unknown value type!"); return false;
285 /// The following enums are used to keep track of prior opcodes. The lexer will
286 /// retain the ability to parse obsolete opcode mnemonics and generates semantic
287 /// values containing one of these enumerators.
289 RetOp
, BrOp
, SwitchOp
, InvokeOp
, UnwindOp
, UnreachableOp
294 DivOp
, UDivOp
, SDivOp
, FDivOp
,
295 RemOp
, URemOp
, SRemOp
, FRemOp
,
297 ShlOp
, ShrOp
, LShrOp
, AShrOp
,
298 SetEQ
, SetNE
, SetLE
, SetGE
, SetLT
, SetGT
302 MallocOp
, FreeOp
, AllocaOp
, LoadOp
, StoreOp
, GetElementPtrOp
306 PHIOp
, CallOp
, SelectOp
, UserOp1
, UserOp2
, VAArg
,
307 ExtractElementOp
, InsertElementOp
, ShuffleVectorOp
,
312 CastOp
, TruncOp
, ZExtOp
, SExtOp
, FPTruncOp
, FPExtOp
, FPToUIOp
, FPToSIOp
,
313 UIToFPOp
, SIToFPOp
, PtrToIntOp
, IntToPtrOp
, BitCastOp
316 // An enumeration for the old calling conventions, ala LLVM 1.9
317 namespace OldCallingConv
{
319 C
= 0, CSRet
= 1, Fast
= 8, Cold
= 9, X86_StdCall
= 64, X86_FastCall
= 65,
324 /// These structures are used as the semantic values returned from various
325 /// productions in the grammar. They simply bundle an LLVM IR object with
326 /// its Signedness value. These help track signedness through the various
331 bool operator<(const TypeInfo
& that
) const {
337 bool result
= S
< that
.S
;
338 //#define TYPEINFO_DEBUG
339 #ifdef TYPEINFO_DEBUG
340 std::cerr
<< (result
?"true ":"false ") << T
->getDescription() << " (";
342 std::cerr
<< ") < " << that
.T
->getDescription() << " (";
350 bool operator==(const TypeInfo
& that
) const {
353 return T
== that
.T
&& S
== that
.S
;
355 void destroy() { S
.destroy(); }
359 llvm::PATypeHolder
* PAT
;
361 void destroy() { S
.destroy(); delete PAT
; }
367 void destroy() { S
.destroy(); }
373 void destroy() { S
.destroy(); }
377 llvm::Instruction
*I
;
379 void destroy() { S
.destroy(); }
382 struct TermInstInfo
{
383 llvm::TerminatorInst
*TI
;
385 void destroy() { S
.destroy(); }
389 std::list
<std::pair
<llvm::Value
*, llvm::BasicBlock
*> > *P
;
391 void destroy() { S
.destroy(); delete P
; }
394 } // End llvm namespace