IR support for extractvalue and insertvalue instructions. Also, begin
[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 is distributed under the University of Illinois Open Source
6 // 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 #include "llvm/ADT/APFloat.h"
26 namespace llvm { class MemoryBuffer; }
27
28 // Global variables exported from the lexer...
29
30 extern llvm::ParseError* TheParseError; /// FIXME: Not threading friendly
31
32 // functions exported from the lexer
33 void InitLLLexer(llvm::MemoryBuffer *MB);
34 const char *LLLgetTokenStart();
35 unsigned LLLgetTokenLength();
36 std::string LLLgetFilename();
37 unsigned LLLgetLineNo();
38 void FreeLexer();
39
40 namespace llvm {
41 class Module;
42
43 // RunVMAsmParser - Parse a buffer and return Module
44 Module *RunVMAsmParser(llvm::MemoryBuffer *MB);
45
46 // GenerateError - Wrapper around the ParseException class that automatically
47 // fills in file line number and column number and options info.
48 //
49 // This also helps me because I keep typing 'throw new ParseException' instead
50 // of just 'throw ParseException'... sigh...
51 //
52 extern void GenerateError(const std::string &message, int LineNo = -1);
53
54 /// InlineAsmDescriptor - This is a simple class that holds info about inline
55 /// asm blocks, for use by ValID.
56 struct InlineAsmDescriptor {
57   std::string AsmString, Constraints;
58   bool HasSideEffects;
59   
60   InlineAsmDescriptor(const std::string &as, const std::string &c, bool HSE)
61     : AsmString(as), Constraints(c), HasSideEffects(HSE) {}
62 };
63
64
65 // ValID - Represents a reference of a definition of some sort.  This may either
66 // be a numeric reference or a symbolic (%var) reference.  This is just a
67 // discriminated union.
68 //
69 // Note that I can't implement this class in a straight forward manner with
70 // constructors and stuff because it goes in a union.
71 //
72 struct ValID {
73   enum {
74     LocalID, GlobalID, LocalName, GlobalName,
75     ConstSIntVal, ConstUIntVal, ConstFPVal, ConstNullVal,
76     ConstUndefVal, ConstZeroVal, ConstantVal, InlineAsmVal
77   } Type;
78
79   union {
80     unsigned Num;         // If it's a numeric reference like %1234
81     std::string *Name;    // If it's a named reference.  Memory must be deleted.
82     int64_t  ConstPool64; // Constant pool reference.  This is the value
83     uint64_t UConstPool64;// Unsigned constant pool reference.
84     APFloat *ConstPoolFP; // Floating point constant pool reference
85     Constant *ConstantValue; // Fully resolved constant for ConstantVal case.
86     InlineAsmDescriptor *IAD;
87  };
88
89   static ValID createLocalID(unsigned Num) {
90     ValID D; D.Type = LocalID; D.Num = Num; return D;
91   }
92   static ValID createGlobalID(unsigned Num) {
93     ValID D; D.Type = GlobalID; D.Num = Num; return D;
94   }
95   static ValID createLocalName(const std::string &Name) {
96     ValID D; D.Type = LocalName; D.Name = new std::string(Name); return D;
97   }
98   static ValID createGlobalName(const std::string &Name) {
99     ValID D; D.Type = GlobalName; D.Name = new std::string(Name); return D;
100   }
101   
102   static ValID create(int64_t Val) {
103     ValID D; D.Type = ConstSIntVal; D.ConstPool64 = Val; return D;
104   }
105
106   static ValID create(uint64_t Val) {
107     ValID D; D.Type = ConstUIntVal; D.UConstPool64 = Val; return D;
108   }
109
110   static ValID create(APFloat *Val) {
111     ValID D; D.Type = ConstFPVal; D.ConstPoolFP = Val; return D;
112   }
113
114   static ValID createNull() {
115     ValID D; D.Type = ConstNullVal; return D;
116   }
117
118   static ValID createUndef() {
119     ValID D; D.Type = ConstUndefVal; return D;
120   }
121
122   static ValID createZeroInit() {
123     ValID D; D.Type = ConstZeroVal; return D;
124   }
125   
126   static ValID create(Constant *Val) {
127     ValID D; D.Type = ConstantVal; D.ConstantValue = Val; return D;
128   }
129   
130   static ValID createInlineAsm(const std::string &AsmString,
131                                const std::string &Constraints,
132                                bool HasSideEffects) {
133     ValID D;
134     D.Type = InlineAsmVal;
135     D.IAD = new InlineAsmDescriptor(AsmString, Constraints, HasSideEffects);
136     return D;
137   }
138
139   inline void destroy() const {
140     if (Type == LocalName || Type == GlobalName)
141       delete Name;    // Free this strdup'd memory.
142     else if (Type == InlineAsmVal)
143       delete IAD;
144   }
145
146   inline ValID copy() const {
147     if (Type != LocalName && Type != GlobalName) return *this;
148     ValID Result = *this;
149     Result.Name = new std::string(*Name);
150     return Result;
151   }
152
153   inline std::string getName() const {
154     switch (Type) {
155     case LocalID       : return '%' + utostr(Num);
156     case GlobalID      : return '@' + utostr(Num);
157     case LocalName     : return *Name;
158     case GlobalName    : return *Name;
159     case ConstFPVal    : return ftostr(*ConstPoolFP);
160     case ConstNullVal  : return "null";
161     case ConstUndefVal : return "undef";
162     case ConstZeroVal  : return "zeroinitializer";
163     case ConstUIntVal  :
164     case ConstSIntVal  : return std::string("%") + itostr(ConstPool64);
165     case ConstantVal:
166       if (ConstantValue == ConstantInt::getTrue()) return "true";
167       if (ConstantValue == ConstantInt::getFalse()) return "false";
168       return "<constant expression>";
169     default:
170       assert(0 && "Unknown value!");
171       abort();
172       return "";
173     }
174   }
175
176   bool operator<(const ValID &V) const {
177     if (Type != V.Type) return Type < V.Type;
178     switch (Type) {
179     case LocalID:
180     case GlobalID:      return Num < V.Num;
181     case LocalName:
182     case GlobalName:    return *Name < *V.Name;
183     case ConstSIntVal:  return ConstPool64  < V.ConstPool64;
184     case ConstUIntVal:  return UConstPool64 < V.UConstPool64;
185     case ConstFPVal:    return ConstPoolFP->compare(*V.ConstPoolFP) ==
186                                APFloat::cmpLessThan;
187     case ConstNullVal:  return false;
188     case ConstUndefVal: return false;
189     case ConstZeroVal: return false;
190     case ConstantVal:   return ConstantValue < V.ConstantValue;
191     default:  assert(0 && "Unknown value type!"); return false;
192     }
193   }
194
195   bool operator==(const ValID &V) const {
196     if (Type == V.Type) {
197       switch (Type) {
198         case LocalID:
199         case GlobalID: return Num == V.Num;
200         case LocalName:
201         case GlobalName: return *Name == *(V.Name);
202         case ConstSIntVal:  return ConstPool64  == V.ConstPool64;
203         case ConstUIntVal:  return UConstPool64 == V.UConstPool64;
204         case ConstFPVal:    return ConstPoolFP->compare(*V.ConstPoolFP) == 
205                                    APFloat::cmpEqual;
206         case ConstantVal:   return ConstantValue == V.ConstantValue;
207         case ConstNullVal:  return true;
208         case ConstUndefVal: return true;
209         case ConstZeroVal:  return true;
210         default:  assert(0 && "Unknown value type!"); return false;
211       }
212     }
213     return false;
214   }
215 };
216
217 struct TypeWithAttrs {
218   llvm::PATypeHolder *Ty;
219   ParameterAttributes Attrs;
220 };
221
222 typedef std::vector<TypeWithAttrs> TypeWithAttrsList; 
223
224 struct ArgListEntry {
225   ParameterAttributes Attrs;
226   llvm::PATypeHolder *Ty;
227   std::string *Name;
228 };
229
230 typedef std::vector<struct ArgListEntry> ArgListType;
231
232 struct ParamListEntry {
233   Value *Val;
234   ParameterAttributes Attrs;
235 };
236
237 typedef std::vector<ParamListEntry> ParamList;
238
239
240 } // End llvm namespace
241
242 #endif