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;
44 // Globals exported by the parser...
45 extern std::string CurFilename; /// FIXME: Not threading friendly
48 Module *RunVMAsmParser(const std::string &Filename, FILE *F);
50 // Parse a string directly
51 Module *RunVMAsmParser(const char * AsmString, Module * M);
54 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
55 // appropriate character. If AllowNull is set to false, a \00 value will cause
58 // If AllowNull is set to true, the return value of the function points to the
59 // last character of the string in memory.
61 char *UnEscapeLexed(char *Buffer, bool AllowNull = false);
64 // ThrowException - Wrapper around the ParseException class that automatically
65 // fills in file line number and column number and options info.
67 // This also helps me because I keep typing 'throw new ParseException' instead
68 // of just 'throw ParseException'... sigh...
70 extern void GenerateError(const std::string &message, int LineNo = -1);
72 /// InlineAsmDescriptor - This is a simple class that holds info about inline
73 /// asm blocks, for use by ValID.
74 struct InlineAsmDescriptor {
75 std::string AsmString, Constraints;
78 InlineAsmDescriptor(const std::string &as, const std::string &c, bool HSE)
79 : AsmString(as), Constraints(c), HasSideEffects(HSE) {}
83 // ValID - Represents a reference of a definition of some sort. This may either
84 // be a numeric reference or a symbolic (%var) reference. This is just a
85 // discriminated union.
87 // Note that I can't implement this class in a straight forward manner with
88 // constructors and stuff because it goes in a union.
92 LocalID, GlobalID, LocalName, GlobalName,
93 ConstSIntVal, ConstUIntVal, ConstFPVal, ConstNullVal,
94 ConstUndefVal, ConstZeroVal, ConstantVal, InlineAsmVal
98 unsigned Num; // If it's a numeric reference like %1234
99 char *Name; // If it's a named reference. Memory must be free'd.
100 int64_t ConstPool64; // Constant pool reference. This is the value
101 uint64_t UConstPool64;// Unsigned constant pool reference.
102 double ConstPoolFP; // Floating point constant pool reference
103 Constant *ConstantValue; // Fully resolved constant for ConstantVal case.
104 InlineAsmDescriptor *IAD;
107 static ValID createLocalID(unsigned Num) {
108 ValID D; D.Type = LocalID; D.Num = Num; return D;
110 static ValID createGlobalID(unsigned Num) {
111 ValID D; D.Type = GlobalID; D.Num = Num; return D;
113 static ValID createLocalName(char *Name) {
114 ValID D; D.Type = LocalName; D.Name = Name; return D;
116 static ValID createGlobalName(char *Name) {
117 ValID D; D.Type = GlobalName; D.Name = Name; return D;
120 static ValID create(int64_t Val) {
121 ValID D; D.Type = ConstSIntVal; D.ConstPool64 = Val; return D;
124 static ValID create(uint64_t Val) {
125 ValID D; D.Type = ConstUIntVal; D.UConstPool64 = Val; return D;
128 static ValID create(double Val) {
129 ValID D; D.Type = ConstFPVal; D.ConstPoolFP = Val; return D;
132 static ValID createNull() {
133 ValID D; D.Type = ConstNullVal; return D;
136 static ValID createUndef() {
137 ValID D; D.Type = ConstUndefVal; return D;
140 static ValID createZeroInit() {
141 ValID D; D.Type = ConstZeroVal; return D;
144 static ValID create(Constant *Val) {
145 ValID D; D.Type = ConstantVal; D.ConstantValue = Val; return D;
148 static ValID createInlineAsm(const std::string &AsmString,
149 const std::string &Constraints,
150 bool HasSideEffects) {
152 D.Type = InlineAsmVal;
153 D.IAD = new InlineAsmDescriptor(AsmString, Constraints, HasSideEffects);
157 inline void destroy() const {
158 if (Type == LocalName || Type == GlobalName)
159 free(Name); // Free this strdup'd memory.
160 else if (Type == InlineAsmVal)
164 inline ValID copy() const {
165 if (Type != LocalName && Type != GlobalName) return *this;
166 ValID Result = *this;
167 Result.Name = strdup(Name);
171 inline std::string getName() const {
173 case LocalID : return '%' + utostr(Num);
174 case GlobalID : return '@' + utostr(Num);
175 case LocalName : return Name;
176 case GlobalName : return Name;
177 case ConstFPVal : return ftostr(ConstPoolFP);
178 case ConstNullVal : return "null";
179 case ConstUndefVal : return "undef";
180 case ConstZeroVal : return "zeroinitializer";
182 case ConstSIntVal : return std::string("%") + itostr(ConstPool64);
184 if (ConstantValue == ConstantInt::getTrue()) return "true";
185 if (ConstantValue == ConstantInt::getFalse()) return "false";
186 return "<constant expression>";
188 assert(0 && "Unknown value!");
194 bool operator<(const ValID &V) const {
195 if (Type != V.Type) return Type < V.Type;
198 case GlobalID: return Num < V.Num;
200 case GlobalName: return strcmp(Name, V.Name) < 0;
201 case ConstSIntVal: return ConstPool64 < V.ConstPool64;
202 case ConstUIntVal: return UConstPool64 < V.UConstPool64;
203 case ConstFPVal: return ConstPoolFP < V.ConstPoolFP;
204 case ConstNullVal: return false;
205 case ConstUndefVal: return false;
206 case ConstZeroVal: return false;
207 case ConstantVal: return ConstantValue < V.ConstantValue;
208 default: assert(0 && "Unknown value type!"); return false;
212 bool operator==(const ValID &V) const {
213 if (Type == V.Type) {
216 case GlobalID: return Num == V.Num;
218 case GlobalName: return strcmp(Name, V.Name) == 0;
219 case ConstSIntVal: return ConstPool64 == V.ConstPool64;
220 case ConstUIntVal: return UConstPool64 == V.UConstPool64;
221 case ConstFPVal: return ConstPoolFP == V.ConstPoolFP;
222 case ConstantVal: return ConstantValue == V.ConstantValue;
223 case ConstNullVal: return true;
224 case ConstUndefVal: return true;
225 case ConstZeroVal: return true;
226 default: assert(0 && "Unknown value type!"); return false;
233 struct TypeWithAttrs {
234 llvm::PATypeHolder *Ty;
238 typedef std::vector<TypeWithAttrs> TypeWithAttrsList;
240 struct ArgListEntry {
242 llvm::PATypeHolder *Ty;
246 typedef std::vector<struct ArgListEntry> ArgListType;
248 struct ValueRefListEntry {
253 typedef std::vector<ValueRefListEntry> ValueRefList;
256 } // End llvm namespace