It is pointless to turn a UINT_TO_FP into an
[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 #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   }
156
157   inline ValID copy() const {
158     ValID Result = *this;
159     if (Type == ConstAPInt)
160       Result.ConstPoolInt = new APSInt(*ConstPoolInt);
161
162     if (Type != LocalName && Type != GlobalName) return Result;
163     Result.Name = new std::string(*Name);
164     return Result;
165   }
166
167   inline std::string getName() const {
168     switch (Type) {
169     case LocalID       : return '%' + utostr(Num);
170     case GlobalID      : return '@' + utostr(Num);
171     case LocalName     : return *Name;
172     case GlobalName    : return *Name;
173     case ConstAPInt    : return ConstPoolInt->toString();
174     case ConstFPVal    : return ftostr(*ConstPoolFP);
175     case ConstNullVal  : return "null";
176     case ConstUndefVal : return "undef";
177     case ConstZeroVal  : return "zeroinitializer";
178     case ConstUIntVal  :
179     case ConstSIntVal  : return std::string("%") + itostr(ConstPool64);
180     case ConstantVal:
181       if (ConstantValue == ConstantInt::getTrue()) return "true";
182       if (ConstantValue == ConstantInt::getFalse()) return "false";
183       return "<constant expression>";
184     default:
185       assert(0 && "Unknown value!");
186       abort();
187       return "";
188     }
189   }
190
191   bool operator<(const ValID &V) const {
192     if (Type != V.Type) return Type < V.Type;
193     switch (Type) {
194     case LocalID:
195     case GlobalID:      return Num < V.Num;
196     case LocalName:
197     case GlobalName:    return *Name < *V.Name;
198     case ConstSIntVal:  return ConstPool64  < V.ConstPool64;
199     case ConstUIntVal:  return UConstPool64 < V.UConstPool64;
200     case ConstAPInt:    return ConstPoolInt->ult(*V.ConstPoolInt);
201     case ConstFPVal:    return ConstPoolFP->compare(*V.ConstPoolFP) ==
202                                APFloat::cmpLessThan;
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   bool operator==(const ValID &V) const {
212     if (Type != V.Type) return false;
213
214     switch (Type) {
215     default:  assert(0 && "Unknown value type!");
216     case LocalID:
217     case GlobalID:      return Num == V.Num;
218     case LocalName:
219     case GlobalName:    return *Name == *(V.Name);
220     case ConstSIntVal:  return ConstPool64  == V.ConstPool64;
221     case ConstUIntVal:  return UConstPool64 == V.UConstPool64;
222     case ConstAPInt:    return *ConstPoolInt == *V.ConstPoolInt;
223     case ConstFPVal:    return ConstPoolFP->compare(*V.ConstPoolFP) ==
224                                APFloat::cmpEqual;
225     case ConstantVal:   return ConstantValue == V.ConstantValue;
226     case ConstNullVal:  return true;
227     case ConstUndefVal: return true;
228     case ConstZeroVal:  return true;
229     }
230   }
231 };
232
233 struct TypeWithAttrs {
234   llvm::PATypeHolder *Ty;
235   ParameterAttributes Attrs;
236 };
237
238 typedef std::vector<TypeWithAttrs> TypeWithAttrsList;
239
240 struct ArgListEntry {
241   ParameterAttributes Attrs;
242   llvm::PATypeHolder *Ty;
243   std::string *Name;
244 };
245
246 typedef std::vector<struct ArgListEntry> ArgListType;
247
248 struct ParamListEntry {
249   Value *Val;
250   ParameterAttributes Attrs;
251 };
252
253 typedef std::vector<ParamListEntry> ParamList;
254
255
256 } // End llvm namespace
257
258 #endif