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