Handle vaarg instructions correctly.
[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/ParameterAttributes.h"
21 #include "llvm/Function.h"
22 #include "llvm/Instructions.h"
23 #include "llvm/Assembly/Parser.h"
24 #include "llvm/ADT/StringExtras.h"
25
26
27 // Global variables exported from the lexer...
28
29 extern int llvmAsmlineno;         /// FIXME: Not threading friendly
30 extern llvm::ParseError* TheParseError; /// FIXME: Not threading friendly
31
32 extern std::string &llvmAsmTextin;
33
34 // functions exported from the lexer
35 void set_scan_file(FILE * F);
36 void set_scan_string (const char * str);
37
38 // Globals exported by the parser...
39 extern char* llvmAsmtext;
40 extern int   llvmAsmleng;
41
42 namespace llvm {
43 class Module;
44
45 // Globals exported by the parser...
46 extern std::string CurFilename;   /// FIXME: Not threading friendly
47
48 // RunVMAsmParser - Parse a file and return Module
49 Module *RunVMAsmParser(const std::string &Filename, FILE *F);
50
51 // Parse a string directly
52 Module *RunVMAsmParser(const char * AsmString, Module * M);
53
54 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
55 // appropriate character.  
56 char *UnEscapeLexed(char *Buffer);
57
58 // GenerateError - Wrapper around the ParseException class that automatically
59 // fills in file line number and column number and options info.
60 //
61 // This also helps me because I keep typing 'throw new ParseException' instead
62 // of just 'throw ParseException'... sigh...
63 //
64 extern void GenerateError(const std::string &message, int LineNo = -1);
65
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;
70   bool HasSideEffects;
71   
72   InlineAsmDescriptor(const std::string &as, const std::string &c, bool HSE)
73     : AsmString(as), Constraints(c), HasSideEffects(HSE) {}
74 };
75
76
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.
80 //
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.
83 //
84 struct ValID {
85   enum {
86     LocalID, GlobalID, LocalName, GlobalName,
87     ConstSIntVal, ConstUIntVal, ConstFPVal, ConstNullVal,
88     ConstUndefVal, ConstZeroVal, ConstantVal, InlineAsmVal
89   } Type;
90
91   union {
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;
99   };
100
101   static ValID createLocalID(unsigned Num) {
102     ValID D; D.Type = LocalID; D.Num = Num; return D;
103   }
104   static ValID createGlobalID(unsigned Num) {
105     ValID D; D.Type = GlobalID; D.Num = Num; return D;
106   }
107   static ValID createLocalName(const std::string &Name) {
108     ValID D; D.Type = LocalName; D.Name = new std::string(Name); return D;
109   }
110   static ValID createGlobalName(const std::string &Name) {
111     ValID D; D.Type = GlobalName; D.Name = new std::string(Name); return D;
112   }
113   
114   static ValID create(int64_t Val) {
115     ValID D; D.Type = ConstSIntVal; D.ConstPool64 = Val; return D;
116   }
117
118   static ValID create(uint64_t Val) {
119     ValID D; D.Type = ConstUIntVal; D.UConstPool64 = Val; return D;
120   }
121
122   static ValID create(double Val) {
123     ValID D; D.Type = ConstFPVal; D.ConstPoolFP = Val; return D;
124   }
125
126   static ValID createNull() {
127     ValID D; D.Type = ConstNullVal; return D;
128   }
129
130   static ValID createUndef() {
131     ValID D; D.Type = ConstUndefVal; return D;
132   }
133
134   static ValID createZeroInit() {
135     ValID D; D.Type = ConstZeroVal; return D;
136   }
137   
138   static ValID create(Constant *Val) {
139     ValID D; D.Type = ConstantVal; D.ConstantValue = Val; return D;
140   }
141   
142   static ValID createInlineAsm(const std::string &AsmString,
143                                const std::string &Constraints,
144                                bool HasSideEffects) {
145     ValID D;
146     D.Type = InlineAsmVal;
147     D.IAD = new InlineAsmDescriptor(AsmString, Constraints, HasSideEffects);
148     return D;
149   }
150
151   inline void destroy() const {
152     if (Type == LocalName || Type == GlobalName)
153       delete Name;    // Free this strdup'd memory.
154     else if (Type == InlineAsmVal)
155       delete IAD;
156   }
157
158   inline ValID copy() const {
159     if (Type != LocalName && Type != GlobalName) return *this;
160     ValID Result = *this;
161     Result.Name = new std::string(*Name);
162     return Result;
163   }
164
165   inline std::string getName() const {
166     switch (Type) {
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";
175     case ConstUIntVal  :
176     case ConstSIntVal  : return std::string("%") + itostr(ConstPool64);
177     case ConstantVal:
178       if (ConstantValue == ConstantInt::getTrue()) return "true";
179       if (ConstantValue == ConstantInt::getFalse()) return "false";
180       return "<constant expression>";
181     default:
182       assert(0 && "Unknown value!");
183       abort();
184       return "";
185     }
186   }
187
188   bool operator<(const ValID &V) const {
189     if (Type != V.Type) return Type < V.Type;
190     switch (Type) {
191     case LocalID:
192     case GlobalID:      return Num < V.Num;
193     case LocalName:
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;
203     }
204   }
205
206   bool operator==(const ValID &V) const {
207     if (Type == V.Type) {
208       switch (Type) {
209         case LocalID:
210         case GlobalID: return Num == V.Num;
211         case LocalName:
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;
221       }
222     }
223     return false;
224   }
225 };
226
227 struct TypeWithAttrs {
228   llvm::PATypeHolder *Ty;
229   uint16_t Attrs;
230 };
231
232 typedef std::vector<TypeWithAttrs> TypeWithAttrsList; 
233
234 struct ArgListEntry {
235   uint16_t Attrs;
236   llvm::PATypeHolder *Ty;
237   std::string *Name;
238 };
239
240 typedef std::vector<struct ArgListEntry> ArgListType;
241
242 struct ValueRefListEntry {
243   Value *Val;
244   uint16_t Attrs;
245 };
246
247 typedef std::vector<ValueRefListEntry> ValueRefList;
248
249
250 } // End llvm namespace
251
252 #endif