Implement the GEP upgrade for non-constant unsigned indices
[oota-llvm.git] / tools / llvm-upgrade / ParserInternals.h
1 //===-- ParserInternals.h - Definitions internal to the parser --*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Reid Spencer 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 variables that are shared between the lexer,
11 //  the parser, and the main program.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef PARSER_INTERNALS_H
16 #define PARSER_INTERNALS_H
17
18 #include <string>
19 #include <istream>
20 #include <vector>
21
22 // Global variables exported from the lexer...
23
24 extern std::string CurFileName;
25 extern std::string Textin;
26 extern int Upgradelineno;
27 extern std::istream* LexInput;
28
29
30 void UpgradeAssembly(
31   const std::string & infile, std::istream& in, std::ostream &out, bool debug);
32
33 // Globals exported by the parser...
34 extern char* Upgradetext;
35 extern int   Upgradeleng;
36 extern unsigned SizeOfPointer;
37
38 int yyerror(const char *ErrorMsg) ;
39
40 /// This enum is used to keep track of the original (1.9) type used to form
41 /// a type. These are needed for type upgrades and to determine how to upgrade
42 /// signed instructions with signless operands.
43 enum Types {
44   BoolTy, SByteTy, UByteTy, ShortTy, UShortTy, IntTy, UIntTy, LongTy, ULongTy,
45   FloatTy, DoubleTy, PointerTy, PackedTy, ArrayTy, StructTy, OpaqueTy, VoidTy,
46   LabelTy, FunctionTy, UnresolvedTy, NumericTy
47 };
48
49 /// This type is used to keep track of the signedness of the obsolete
50 /// integer types. Instead of creating an llvm::Type directly, the Lexer will
51 /// create instances of TypeInfo which retains the signedness indication so
52 /// it can be used by the parser for upgrade decisions.
53 /// For example if "uint" is encountered then the "first" field will be set 
54 /// to "int32" and the "second" field will be set to "isUnsigned".  If the 
55 /// type is not obsolete then "second" will be set to "isSignless".
56 struct TypeInfo {
57   std::string* newTy;
58   Types oldTy;
59
60   void destroy() const { delete newTy; }
61
62   bool isSigned() const {
63     return oldTy == SByteTy || oldTy == ShortTy || 
64            oldTy == IntTy || oldTy == LongTy;
65   }
66
67   bool isUnsigned() const {
68     return oldTy == UByteTy || oldTy == UShortTy || 
69            oldTy == UIntTy || oldTy == ULongTy;
70   }
71
72   bool isSignless() const { return !isSigned() && !isUnsigned(); }
73   bool isInteger() const { return isSigned() || isUnsigned(); }
74   bool isIntegral() const { return oldTy == BoolTy || isInteger(); }
75   bool isFloatingPoint() const { return oldTy == DoubleTy || oldTy == FloatTy; }
76   bool isPacked() const { return oldTy == PackedTy; }
77   bool isPointer() const { return oldTy == PointerTy; }
78   bool isOther() const { 
79     return !isPacked() && !isPointer() && !isFloatingPoint() && !isIntegral(); }
80
81   unsigned getBitWidth() const {
82     switch (oldTy) {
83       case LabelTy:
84       case VoidTy : return 0;
85       case BoolTy : return 1;
86       case SByteTy: case UByteTy : return 8;
87       case ShortTy: case UShortTy : return 16;
88       case IntTy: case UIntTy: case FloatTy: return 32;
89       case LongTy: case ULongTy: case DoubleTy : return 64;
90       case PointerTy: return SizeOfPointer; // global var
91       default:
92         return 128; /// Struct/Packed/Array --> doesn't matter
93       
94     }
95   }
96 };
97
98 /// This type is used to keep track of the signedness of values. Instead
99 /// of creating llvm::Value directly, the parser will create ValueInfo which
100 /// associates a Value* with a Signedness indication.
101 struct ValueInfo {
102   std::string* val;
103   TypeInfo type;
104   bool constant;
105   bool isConstant() const { return constant; }
106   void destroy() { delete val; type.destroy(); }
107 };
108
109 /// This type is used to keep track of the signedness of constants.
110 struct ConstInfo {
111   std::string *cnst;
112   TypeInfo type;
113   void destroy() { delete cnst; type.destroy(); }
114 };
115
116 typedef std::vector<ValueInfo> ValueList;
117
118
119 #endif