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/Assembly/Parser.h"
23 #include "llvm/ADT/StringExtras.h"
26 // Global variables exported from the lexer...
28 extern int llvmAsmlineno; /// FIXME: Not threading friendly
29 extern llvm::ParseError* TheParseError; /// FIXME: Not threading friendly
31 extern std::string &llvmAsmTextin;
33 // functions exported from the lexer
34 void set_scan_file(FILE * F);
35 void set_scan_string (const char * str);
37 // Globals exported by the parser...
38 extern char* llvmAsmtext;
39 extern int llvmAsmleng;
43 // Globals exported by the parser...
44 extern std::string CurFilename; /// FIXME: Not threading friendly
47 Module *RunVMAsmParser(const std::string &Filename, FILE *F);
49 // Parse a string directly
50 Module *RunVMAsmParser(const char * AsmString, Module * M);
53 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
54 // appropriate character. If AllowNull is set to false, a \00 value will cause
57 // If AllowNull is set to true, the return value of the function points to the
58 // last character of the string in memory.
60 char *UnEscapeLexed(char *Buffer, bool AllowNull = false);
63 // ThrowException - Wrapper around the ParseException class that automatically
64 // fills in file line number and column number and options info.
66 // This also helps me because I keep typing 'throw new ParseException' instead
67 // of just 'throw ParseException'... sigh...
69 extern void GenerateError(const std::string &message, int LineNo = -1);
71 /// InlineAsmDescriptor - This is a simple class that holds info about inline
72 /// asm blocks, for use by ValID.
73 struct InlineAsmDescriptor {
74 std::string AsmString, Constraints;
77 InlineAsmDescriptor(const std::string &as, const std::string &c, bool HSE)
78 : AsmString(as), Constraints(c), HasSideEffects(HSE) {}
82 // ValID - Represents a reference of a definition of some sort. This may either
83 // be a numeric reference or a symbolic (%var) reference. This is just a
84 // discriminated union.
86 // Note that I can't implement this class in a straight forward manner with
87 // constructors and stuff because it goes in a union.
91 LocalID, GlobalID, LocalName, GlobalName,
92 ConstSIntVal, ConstUIntVal, ConstFPVal, ConstNullVal,
93 ConstUndefVal, ConstZeroVal, ConstantVal, InlineAsmVal
97 unsigned Num; // If it's a numeric reference like %1234
98 char *Name; // If it's a named reference. Memory must be free'd.
99 int64_t ConstPool64; // Constant pool reference. This is the value
100 uint64_t UConstPool64;// Unsigned constant pool reference.
101 double ConstPoolFP; // Floating point constant pool reference
102 Constant *ConstantValue; // Fully resolved constant for ConstantVal case.
103 InlineAsmDescriptor *IAD;
106 static ValID createLocalID(unsigned Num) {
107 ValID D; D.Type = LocalID; D.Num = Num; return D;
109 static ValID createGlobalID(unsigned Num) {
110 ValID D; D.Type = GlobalID; D.Num = Num; return D;
112 static ValID createLocalName(char *Name) {
113 ValID D; D.Type = LocalName; D.Name = Name; return D;
115 static ValID createGlobalName(char *Name) {
116 ValID D; D.Type = GlobalName; D.Name = Name; return D;
119 static ValID create(int64_t Val) {
120 ValID D; D.Type = ConstSIntVal; D.ConstPool64 = Val; return D;
123 static ValID create(uint64_t Val) {
124 ValID D; D.Type = ConstUIntVal; D.UConstPool64 = Val; return D;
127 static ValID create(double Val) {
128 ValID D; D.Type = ConstFPVal; D.ConstPoolFP = Val; return D;
131 static ValID createNull() {
132 ValID D; D.Type = ConstNullVal; return D;
135 static ValID createUndef() {
136 ValID D; D.Type = ConstUndefVal; return D;
139 static ValID createZeroInit() {
140 ValID D; D.Type = ConstZeroVal; return D;
143 static ValID create(Constant *Val) {
144 ValID D; D.Type = ConstantVal; D.ConstantValue = Val; return D;
147 static ValID createInlineAsm(const std::string &AsmString,
148 const std::string &Constraints,
149 bool HasSideEffects) {
151 D.Type = InlineAsmVal;
152 D.IAD = new InlineAsmDescriptor(AsmString, Constraints, HasSideEffects);
156 inline void destroy() const {
157 if (Type == LocalName || Type == GlobalName)
158 free(Name); // Free this strdup'd memory.
159 else if (Type == InlineAsmVal)
163 inline ValID copy() const {
164 if (Type != LocalName && Type != GlobalName) return *this;
165 ValID Result = *this;
166 Result.Name = strdup(Name);
170 inline std::string getName() const {
172 case LocalID : return '%' + utostr(Num);
173 case GlobalID : return '@' + utostr(Num);
174 case LocalName : return Name;
175 case GlobalName : return Name;
176 case ConstFPVal : return ftostr(ConstPoolFP);
177 case ConstNullVal : return "null";
178 case ConstUndefVal : return "undef";
179 case ConstZeroVal : return "zeroinitializer";
181 case ConstSIntVal : return std::string("%") + itostr(ConstPool64);
183 if (ConstantValue == ConstantInt::getTrue()) return "true";
184 if (ConstantValue == ConstantInt::getFalse()) return "false";
185 return "<constant expression>";
187 assert(0 && "Unknown value!");
193 bool operator<(const ValID &V) const {
194 if (Type != V.Type) return Type < V.Type;
197 case GlobalID: return Num < V.Num;
199 case GlobalName: return strcmp(Name, V.Name) < 0;
200 case ConstSIntVal: return ConstPool64 < V.ConstPool64;
201 case ConstUIntVal: return UConstPool64 < V.UConstPool64;
202 case ConstFPVal: return ConstPoolFP < V.ConstPoolFP;
203 case ConstNullVal: return false;
204 case ConstUndefVal: return false;
205 case ConstZeroVal: return false;
206 case ConstantVal: return ConstantValue < V.ConstantValue;
207 default: assert(0 && "Unknown value type!"); return false;
211 bool operator==(const ValID &V) const {
212 if (Type == V.Type) {
215 case GlobalID: return Num == V.Num;
217 case GlobalName: return strcmp(Name, V.Name) == 0;
218 case ConstSIntVal: return ConstPool64 == V.ConstPool64;
219 case ConstUIntVal: return UConstPool64 == V.UConstPool64;
220 case ConstFPVal: return ConstPoolFP == V.ConstPoolFP;
221 case ConstantVal: return ConstantValue == V.ConstantValue;
222 case ConstNullVal: return true;
223 case ConstUndefVal: return true;
224 case ConstZeroVal: return true;
225 default: assert(0 && "Unknown value type!"); return false;
232 struct TypeWithAttrs {
233 llvm::PATypeHolder *Ty;
234 FunctionType::ParameterAttributes Attrs;
237 typedef std::vector<TypeWithAttrs> TypeWithAttrsList;
239 struct ArgListEntry {
240 FunctionType::ParameterAttributes Attrs;
241 llvm::PATypeHolder *Ty;
245 typedef std::vector<struct ArgListEntry> ArgListType;
247 struct ValueRefListEntry {
249 FunctionType::ParameterAttributes Attrs;
252 typedef std::vector<ValueRefListEntry> ValueRefList;
255 } // End llvm namespace