For PR1256:
[oota-llvm.git] / tools / llvm-upgrade / UpgradeInternals.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/ADT/StringExtras.h"
23 #include <list>
24
25
26 // Global variables exported from the lexer.
27 extern int yydebug;
28 extern void error(const std::string& msg, int line = -1);
29 extern char* Upgradetext;
30 extern int   Upgradeleng;
31 extern int Upgradelineno;
32
33 namespace llvm {
34
35
36 class Module;
37 Module* UpgradeAssembly(const std::string &infile, std::istream& in, 
38                         bool debug, bool addAttrs);
39
40
41 extern std::istream* LexInput;
42
43 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
44 // appropriate character.  If AllowNull is set to false, a \00 value will cause
45 // an error.
46 //
47 // If AllowNull is set to true, the return value of the function points to the
48 // last character of the string in memory.
49 //
50 char *UnEscapeLexed(char *Buffer, bool AllowNull = false);
51
52 /// InlineAsmDescriptor - This is a simple class that holds info about inline
53 /// asm blocks, for use by ValID.
54 struct InlineAsmDescriptor {
55   std::string AsmString, Constraints;
56   bool HasSideEffects;
57   
58   InlineAsmDescriptor(const std::string &as, const std::string &c, bool HSE)
59     : AsmString(as), Constraints(c), HasSideEffects(HSE) {}
60 };
61
62 /// An enumeration for defining the Signedness of a type or value. Signless
63 /// means the signedness is not relevant to the type or value.
64 enum Signedness { Signless, Unsigned, Signed };
65
66
67 // ValID - Represents a reference of a definition of some sort.  This may either
68 // be a numeric reference or a symbolic (%var) reference.  This is just a
69 // discriminated union.
70 //
71 // Note that I can't implement this class in a straight forward manner with
72 // constructors and stuff because it goes in a union.
73 //
74 struct ValID {
75   enum {
76     NumberVal, NameVal, ConstSIntVal, ConstUIntVal, ConstFPVal, ConstNullVal,
77     ConstUndefVal, ConstZeroVal, ConstantVal, InlineAsmVal
78   } Type;
79
80   union {
81     int      Num;         // If it's a numeric reference
82     char    *Name;        // If it's a named reference.  Memory must be free'd.
83     int64_t  ConstPool64; // Constant pool reference.  This is the value
84     uint64_t UConstPool64;// Unsigned constant pool reference.
85     double   ConstPoolFP; // Floating point constant pool reference
86     Constant *ConstantValue; // Fully resolved constant for ConstantVal case.
87     InlineAsmDescriptor *IAD;
88   };
89   Signedness S;
90
91   static ValID create(int Num, Signedness Sign) {
92     ValID D; D.Type = NumberVal; D.Num = Num; D.S = Sign;
93     return D;
94   }
95
96   static ValID create(char *Name, Signedness Sign) {
97     ValID D; D.Type = NameVal; D.Name = Name; D.S = Sign; 
98     return D;
99   }
100
101   static ValID create(int64_t Val) {
102     ValID D; D.Type = ConstSIntVal; D.ConstPool64 = Val; D.S = Signed; 
103     return D;
104   }
105
106   static ValID create(uint64_t Val) {
107     ValID D; D.Type = ConstUIntVal; D.UConstPool64 = Val; D.S = Unsigned; 
108     return D;
109   }
110
111   static ValID create(double Val) {
112     ValID D; D.Type = ConstFPVal; D.ConstPoolFP = Val; D.S = Signless;
113     return D;
114   }
115
116   static ValID createNull() {
117     ValID D; D.Type = ConstNullVal; D.S = Signless;
118     return D;
119   }
120
121   static ValID createUndef() {
122     ValID D; D.Type = ConstUndefVal; D.S = Signless;
123     return D;
124   }
125
126   static ValID createZeroInit() {
127     ValID D; D.Type = ConstZeroVal; D.S = Signless;
128     return D;
129   }
130   
131   static ValID create(Constant *Val, Signedness Sign) {
132     ValID D; D.Type = ConstantVal; D.ConstantValue = Val; D.S = Sign;
133     return D;
134   }
135   
136   static ValID createInlineAsm(const std::string &AsmString,
137                                const std::string &Constraints,
138                                bool HasSideEffects) {
139     ValID D;
140     D.Type = InlineAsmVal;
141     D.IAD = new InlineAsmDescriptor(AsmString, Constraints, HasSideEffects);
142     return D;
143   }
144
145   inline void destroy() const {
146     if (Type == NameVal)
147       free(Name);    // Free this strdup'd memory.
148     else if (Type == InlineAsmVal)
149       delete IAD;
150   }
151
152   inline ValID copy() const {
153     if (Type != NameVal) return *this;
154     ValID Result = *this;
155     Result.Name = strdup(Name);
156     return Result;
157   }
158
159   inline std::string getName() const {
160     switch (Type) {
161     case NumberVal     : return std::string("#") + itostr(Num);
162     case NameVal       : return Name;
163     case ConstFPVal    : return ftostr(ConstPoolFP);
164     case ConstNullVal  : return "null";
165     case ConstUndefVal : return "undef";
166     case ConstZeroVal  : return "zeroinitializer";
167     case ConstUIntVal  :
168     case ConstSIntVal  : return std::string("%") + itostr(ConstPool64);
169     case ConstantVal:
170       if (ConstantValue == ConstantInt::get(Type::Int1Ty, true)) 
171         return "true";
172       if (ConstantValue == ConstantInt::get(Type::Int1Ty, false))
173         return "false";
174       return "<constant expression>";
175     default:
176       assert(0 && "Unknown value!");
177       abort();
178       return "";
179     }
180   }
181
182   bool operator<(const ValID &V) const {
183     if (Type != V.Type) return Type < V.Type;
184     switch (Type) {
185     case NumberVal:     return Num < V.Num;
186     case NameVal:       return strcmp(Name, V.Name) < 0;
187     case ConstSIntVal:  return ConstPool64  < V.ConstPool64;
188     case ConstUIntVal:  return UConstPool64 < V.UConstPool64;
189     case ConstFPVal:    return ConstPoolFP  < V.ConstPoolFP;
190     case ConstNullVal:  return false;
191     case ConstUndefVal: return false;
192     case ConstZeroVal: return false;
193     case ConstantVal:   return ConstantValue < V.ConstantValue;
194     default:  assert(0 && "Unknown value type!"); return false;
195     }
196   }
197 };
198
199 /// The following enums are used to keep track of prior opcodes. The lexer will
200 /// retain the ability to parse obsolete opcode mnemonics and generates semantic
201 /// values containing one of these enumerators.
202 enum TermOps {
203   RetOp, BrOp, SwitchOp, InvokeOp, UnwindOp, UnreachableOp
204 };
205
206 enum BinaryOps {
207   AddOp, SubOp, MulOp,
208   DivOp, UDivOp, SDivOp, FDivOp, 
209   RemOp, URemOp, SRemOp, FRemOp, 
210   AndOp, OrOp, XorOp,
211   ShlOp, ShrOp, LShrOp, AShrOp,
212   SetEQ, SetNE, SetLE, SetGE, SetLT, SetGT
213 };
214
215 enum MemoryOps {
216   MallocOp, FreeOp, AllocaOp, LoadOp, StoreOp, GetElementPtrOp
217 };
218
219 enum OtherOps {
220   PHIOp, CallOp, SelectOp, UserOp1, UserOp2, VAArg,
221   ExtractElementOp, InsertElementOp, ShuffleVectorOp,
222   ICmpOp, FCmpOp
223 };
224
225 enum CastOps {
226   CastOp, TruncOp, ZExtOp, SExtOp, FPTruncOp, FPExtOp, FPToUIOp, FPToSIOp,
227   UIToFPOp, SIToFPOp, PtrToIntOp, IntToPtrOp, BitCastOp
228 };
229
230 // An enumeration for the old calling conventions, ala LLVM 1.9
231 namespace OldCallingConv {
232   enum ID {
233     C = 0, CSRet = 1, Fast = 8, Cold = 9, X86_StdCall = 64, X86_FastCall = 65,
234     None = 99999
235   };
236 }
237
238 /// These structures are used as the semantic values returned from various
239 /// productions in the grammar. They simply bundle an LLVM IR object with
240 /// its Signedness value. These help track signedness through the various
241 /// productions. 
242 struct TypeInfo {
243   const llvm::Type *T;
244   Signedness S;
245   bool operator<(const TypeInfo& that) const {
246     if (this == &that)
247       return false;
248     return (T < that.T) || (T == that.T && S < that.S);
249   }
250   bool operator==(const TypeInfo& that) const {
251     if (this == &that)
252       return true;
253     return T == that.T && S == that.S;
254   }
255 };
256
257 struct PATypeInfo {
258   llvm::PATypeHolder* PAT;
259   Signedness S;
260 };
261
262 struct ConstInfo {
263   llvm::Constant* C;
264   Signedness S;
265 };
266
267 struct ValueInfo {
268   llvm::Value* V;
269   Signedness S;
270 };
271
272 struct InstrInfo {
273   llvm::Instruction *I;
274   Signedness S;
275 };
276
277 struct PHIListInfo {
278   std::list<std::pair<llvm::Value*, llvm::BasicBlock*> > *P;
279   Signedness S;
280 };
281
282 } // End llvm namespace
283
284 #endif