1bc744bc4bba3b0a2dd0649b0cc3eb77956febce
[oota-llvm.git] / lib / AsmParser / ParserInternals.h
1 //===-- ParserInternals.h - Definitions internal to the parser --*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This header file defines the various variables that are shared among the
11 //  different components of the parser...
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef PARSER_INTERNALS_H
16 #define PARSER_INTERNALS_H
17
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"
24
25
26 // Global variables exported from the lexer...
27
28 extern int llvmAsmlineno;         /// FIXME: Not threading friendly
29 extern llvm::ParseError* TheParseError; /// FIXME: Not threading friendly
30
31 extern std::string &llvmAsmTextin;
32
33 // functions exported from the lexer
34 void set_scan_file(FILE * F);
35 void set_scan_string (const char * str);
36
37 // Globals exported by the parser...
38 extern char* llvmAsmtext;
39 extern int   llvmAsmleng;
40
41 namespace llvm {
42
43 // Globals exported by the parser...
44 extern std::string CurFilename;   /// FIXME: Not threading friendly
45
46 class Module;
47 Module *RunVMAsmParser(const std::string &Filename, FILE *F);
48
49 // Parse a string directly
50 Module *RunVMAsmParser(const char * AsmString, Module * M);
51
52
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
55 // an error.
56 //
57 // If AllowNull is set to true, the return value of the function points to the
58 // last character of the string in memory.
59 //
60 char *UnEscapeLexed(char *Buffer, bool AllowNull = false);
61
62
63 // ThrowException - Wrapper around the ParseException class that automatically
64 // fills in file line number and column number and options info.
65 //
66 // This also helps me because I keep typing 'throw new ParseException' instead
67 // of just 'throw ParseException'... sigh...
68 //
69 extern void GenerateError(const std::string &message, int LineNo = -1);
70
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;
75   bool HasSideEffects;
76   
77   InlineAsmDescriptor(const std::string &as, const std::string &c, bool HSE)
78     : AsmString(as), Constraints(c), HasSideEffects(HSE) {}
79 };
80
81
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.
85 //
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.
88 //
89 struct ValID {
90   enum {
91     LocalID, GlobalID, LocalName, GlobalName,
92     ConstSIntVal, ConstUIntVal, ConstFPVal, ConstNullVal,
93     ConstUndefVal, ConstZeroVal, ConstantVal, InlineAsmVal
94   } Type;
95
96   union {
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;
104   };
105
106   static ValID createLocalID(unsigned Num) {
107     ValID D; D.Type = LocalID; D.Num = Num; return D;
108   }
109   static ValID createGlobalID(unsigned Num) {
110     ValID D; D.Type = GlobalID; D.Num = Num; return D;
111   }
112   static ValID createLocalName(char *Name) {
113     ValID D; D.Type = LocalName; D.Name = Name; return D;
114   }
115   static ValID createGlobalName(char *Name) {
116     ValID D; D.Type = GlobalName; D.Name = Name; return D;
117   }
118   
119   static ValID create(int64_t Val) {
120     ValID D; D.Type = ConstSIntVal; D.ConstPool64 = Val; return D;
121   }
122
123   static ValID create(uint64_t Val) {
124     ValID D; D.Type = ConstUIntVal; D.UConstPool64 = Val; return D;
125   }
126
127   static ValID create(double Val) {
128     ValID D; D.Type = ConstFPVal; D.ConstPoolFP = Val; return D;
129   }
130
131   static ValID createNull() {
132     ValID D; D.Type = ConstNullVal; return D;
133   }
134
135   static ValID createUndef() {
136     ValID D; D.Type = ConstUndefVal; return D;
137   }
138
139   static ValID createZeroInit() {
140     ValID D; D.Type = ConstZeroVal; return D;
141   }
142   
143   static ValID create(Constant *Val) {
144     ValID D; D.Type = ConstantVal; D.ConstantValue = Val; return D;
145   }
146   
147   static ValID createInlineAsm(const std::string &AsmString,
148                                const std::string &Constraints,
149                                bool HasSideEffects) {
150     ValID D;
151     D.Type = InlineAsmVal;
152     D.IAD = new InlineAsmDescriptor(AsmString, Constraints, HasSideEffects);
153     return D;
154   }
155
156   inline void destroy() const {
157     if (Type == LocalName || Type == GlobalName)
158       free(Name);    // Free this strdup'd memory.
159     else if (Type == InlineAsmVal)
160       delete IAD;
161   }
162
163   inline ValID copy() const {
164     if (Type != LocalName && Type != GlobalName) return *this;
165     ValID Result = *this;
166     Result.Name = strdup(Name);
167     return Result;
168   }
169
170   inline std::string getName() const {
171     switch (Type) {
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";
180     case ConstUIntVal  :
181     case ConstSIntVal  : return std::string("%") + itostr(ConstPool64);
182     case ConstantVal:
183       if (ConstantValue == ConstantInt::getTrue()) return "true";
184       if (ConstantValue == ConstantInt::getFalse()) return "false";
185       return "<constant expression>";
186     default:
187       assert(0 && "Unknown value!");
188       abort();
189       return "";
190     }
191   }
192
193   bool operator<(const ValID &V) const {
194     if (Type != V.Type) return Type < V.Type;
195     switch (Type) {
196     case LocalID:
197     case GlobalID:      return Num < V.Num;
198     case LocalName:
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;
208     }
209   }
210 };
211
212 struct TypeWithAttrs {
213   llvm::PATypeHolder *Ty;
214   FunctionType::ParameterAttributes Attrs;
215 };
216
217 typedef std::vector<TypeWithAttrs> TypeWithAttrsList; 
218
219 struct ArgListEntry {
220   FunctionType::ParameterAttributes Attrs;
221   llvm::PATypeHolder *Ty;
222   char *Name;
223 };
224
225 typedef std::vector<struct ArgListEntry> ArgListType;
226
227 struct ValueRefListEntry {
228   Value *Val;
229   FunctionType::ParameterAttributes Attrs;
230 };
231
232 typedef std::vector<ValueRefListEntry> ValueRefList;
233
234
235 } // End llvm namespace
236
237 #endif