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/ParameterAttributes.h"
21 #include "llvm/Function.h"
22 #include "llvm/Instructions.h"
23 #include "llvm/Assembly/Parser.h"
24 #include "llvm/ADT/StringExtras.h"
27 // Global variables exported from the lexer...
29 extern int llvmAsmlineno
; /// FIXME: Not threading friendly
30 extern llvm::ParseError
* TheParseError
; /// FIXME: Not threading friendly
32 extern std::string
&llvmAsmTextin
;
34 // functions exported from the lexer
35 void set_scan_file(FILE * F
);
36 void set_scan_string (const char * str
);
38 // Globals exported by the parser...
39 extern char* llvmAsmtext
;
40 extern int llvmAsmleng
;
45 // Globals exported by the parser...
46 extern std::string CurFilename
; /// FIXME: Not threading friendly
48 // RunVMAsmParser - Parse a file and return Module
49 Module
*RunVMAsmParser(const std::string
&Filename
, FILE *F
);
51 // Parse a string directly
52 Module
*RunVMAsmParser(const char * AsmString
, Module
* M
);
54 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
55 // appropriate character.
56 char *UnEscapeLexed(char *Buffer
);
58 // GenerateError - Wrapper around the ParseException class that automatically
59 // fills in file line number and column number and options info.
61 // This also helps me because I keep typing 'throw new ParseException' instead
62 // of just 'throw ParseException'... sigh...
64 extern void GenerateError(const std::string
&message
, int LineNo
= -1);
66 /// InlineAsmDescriptor - This is a simple class that holds info about inline
67 /// asm blocks, for use by ValID.
68 struct InlineAsmDescriptor
{
69 std::string AsmString
, Constraints
;
72 InlineAsmDescriptor(const std::string
&as
, const std::string
&c
, bool HSE
)
73 : AsmString(as
), Constraints(c
), HasSideEffects(HSE
) {}
77 // ValID - Represents a reference of a definition of some sort. This may either
78 // be a numeric reference or a symbolic (%var) reference. This is just a
79 // discriminated union.
81 // Note that I can't implement this class in a straight forward manner with
82 // constructors and stuff because it goes in a union.
86 LocalID
, GlobalID
, LocalName
, GlobalName
,
87 ConstSIntVal
, ConstUIntVal
, ConstFPVal
, ConstNullVal
,
88 ConstUndefVal
, ConstZeroVal
, ConstantVal
, InlineAsmVal
92 unsigned Num
; // If it's a numeric reference like %1234
93 std::string
*Name
; // If it's a named reference. Memory must be deleted.
94 int64_t ConstPool64
; // Constant pool reference. This is the value
95 uint64_t UConstPool64
;// Unsigned constant pool reference.
96 double ConstPoolFP
; // Floating point constant pool reference
97 Constant
*ConstantValue
; // Fully resolved constant for ConstantVal case.
98 InlineAsmDescriptor
*IAD
;
101 static ValID
createLocalID(unsigned Num
) {
102 ValID D
; D
.Type
= LocalID
; D
.Num
= Num
; return D
;
104 static ValID
createGlobalID(unsigned Num
) {
105 ValID D
; D
.Type
= GlobalID
; D
.Num
= Num
; return D
;
107 static ValID
createLocalName(const std::string
&Name
) {
108 ValID D
; D
.Type
= LocalName
; D
.Name
= new std::string(Name
); return D
;
110 static ValID
createGlobalName(const std::string
&Name
) {
111 ValID D
; D
.Type
= GlobalName
; D
.Name
= new std::string(Name
); return D
;
114 static ValID
create(int64_t Val
) {
115 ValID D
; D
.Type
= ConstSIntVal
; D
.ConstPool64
= Val
; return D
;
118 static ValID
create(uint64_t Val
) {
119 ValID D
; D
.Type
= ConstUIntVal
; D
.UConstPool64
= Val
; return D
;
122 static ValID
create(double Val
) {
123 ValID D
; D
.Type
= ConstFPVal
; D
.ConstPoolFP
= Val
; return D
;
126 static ValID
createNull() {
127 ValID D
; D
.Type
= ConstNullVal
; return D
;
130 static ValID
createUndef() {
131 ValID D
; D
.Type
= ConstUndefVal
; return D
;
134 static ValID
createZeroInit() {
135 ValID D
; D
.Type
= ConstZeroVal
; return D
;
138 static ValID
create(Constant
*Val
) {
139 ValID D
; D
.Type
= ConstantVal
; D
.ConstantValue
= Val
; return D
;
142 static ValID
createInlineAsm(const std::string
&AsmString
,
143 const std::string
&Constraints
,
144 bool HasSideEffects
) {
146 D
.Type
= InlineAsmVal
;
147 D
.IAD
= new InlineAsmDescriptor(AsmString
, Constraints
, HasSideEffects
);
151 inline void destroy() const {
152 if (Type
== LocalName
|| Type
== GlobalName
)
153 delete Name
; // Free this strdup'd memory.
154 else if (Type
== InlineAsmVal
)
158 inline ValID
copy() const {
159 if (Type
!= LocalName
&& Type
!= GlobalName
) return *this;
160 ValID Result
= *this;
161 Result
.Name
= new std::string(*Name
);
165 inline std::string
getName() const {
167 case LocalID
: return '%' + utostr(Num
);
168 case GlobalID
: return '@' + utostr(Num
);
169 case LocalName
: return *Name
;
170 case GlobalName
: return *Name
;
171 case ConstFPVal
: return ftostr(ConstPoolFP
);
172 case ConstNullVal
: return "null";
173 case ConstUndefVal
: return "undef";
174 case ConstZeroVal
: return "zeroinitializer";
176 case ConstSIntVal
: return std::string("%") + itostr(ConstPool64
);
178 if (ConstantValue
== ConstantInt::getTrue()) return "true";
179 if (ConstantValue
== ConstantInt::getFalse()) return "false";
180 return "<constant expression>";
182 assert(0 && "Unknown value!");
188 bool operator<(const ValID
&V
) const {
189 if (Type
!= V
.Type
) return Type
< V
.Type
;
192 case GlobalID
: return Num
< V
.Num
;
194 case GlobalName
: return *Name
< *V
.Name
;
195 case ConstSIntVal
: return ConstPool64
< V
.ConstPool64
;
196 case ConstUIntVal
: return UConstPool64
< V
.UConstPool64
;
197 case ConstFPVal
: return ConstPoolFP
< V
.ConstPoolFP
;
198 case ConstNullVal
: return false;
199 case ConstUndefVal
: return false;
200 case ConstZeroVal
: return false;
201 case ConstantVal
: return ConstantValue
< V
.ConstantValue
;
202 default: assert(0 && "Unknown value type!"); return false;
206 bool operator==(const ValID
&V
) const {
207 if (Type
== V
.Type
) {
210 case GlobalID
: return Num
== V
.Num
;
212 case GlobalName
: return *Name
== *(V
.Name
);
213 case ConstSIntVal
: return ConstPool64
== V
.ConstPool64
;
214 case ConstUIntVal
: return UConstPool64
== V
.UConstPool64
;
215 case ConstFPVal
: return ConstPoolFP
== V
.ConstPoolFP
;
216 case ConstantVal
: return ConstantValue
== V
.ConstantValue
;
217 case ConstNullVal
: return true;
218 case ConstUndefVal
: return true;
219 case ConstZeroVal
: return true;
220 default: assert(0 && "Unknown value type!"); return false;
227 struct TypeWithAttrs
{
228 llvm::PATypeHolder
*Ty
;
232 typedef std::vector
<TypeWithAttrs
> TypeWithAttrsList
;
234 struct ArgListEntry
{
236 llvm::PATypeHolder
*Ty
;
240 typedef std::vector
<struct ArgListEntry
> ArgListType
;
242 struct ValueRefListEntry
{
247 typedef std::vector
<ValueRefListEntry
> ValueRefList
;
250 } // End llvm namespace