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