aboutsummaryrefslogtreecommitdiffstats
path: root/tools/llvm-upgrade/UpgradeInternals.h
blob: 9c8b6a01425d1170e5062cd0569cb384e4eae3ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//===-- UpgradeInternals.h - Internal parser definitionsr -------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file was developed by Reid Spencer and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
//  This header file defines the variables that are shared between the lexer,
//  the parser, and the main program.
//
//===----------------------------------------------------------------------===//

#ifndef UPGRADE_INTERNALS_H
#define UPGRADE_INTERNALS_H

#include <llvm/ADT/StringExtras.h>
#include <string>
#include <istream>
#include <vector>
#include <set>
#include <cassert>

// Global variables exported from the lexer...

extern std::string CurFileName;
extern std::string Textin;
extern int Upgradelineno;
extern std::istream* LexInput;

struct TypeInfo;
typedef std::vector<const TypeInfo*> TypeList;

void UpgradeAssembly(
  const std::string & infile, std::istream& in, std::ostream &out, bool debug,
  bool addAttrs);

// Globals exported by the parser...
extern char* Upgradetext;
extern int   Upgradeleng;
extern unsigned SizeOfPointer;

int yyerror(const char *ErrorMsg) ;

/// This enum is used to keep track of the original (1.9) type used to form
/// a type. These are needed for type upgrades and to determine how to upgrade
/// signed instructions with signless operands.
enum Types {
  BoolTy, SByteTy, UByteTy, ShortTy, UShortTy, IntTy, UIntTy, LongTy, ULongTy,
  FloatTy, DoubleTy, PointerTy, PackedTy, ArrayTy, StructTy, PackedStructTy, 
  OpaqueTy, VoidTy, LabelTy, FunctionTy, UnresolvedTy, UpRefTy
};

/// This type is used to keep track of the signedness of values. Instead
/// of creating llvm::Value directly, the parser will create ValueInfo which
/// associates a Value* with a Signedness indication.
struct ValueInfo {
  std::string* val;
  const TypeInfo* type;
  bool constant;
  bool isConstant() const { return constant; }
  inline void destroy();
};

/// This type is used to keep track of the signedness of the obsolete
/// integer types. Instead of creating an llvm::Type directly, the Lexer will
/// create instances of TypeInfo which retains the signedness indication so
/// it can be used by the parser for upgrade decisions.
/// For example if "uint" is encountered then the "first" field will be set 
/// to "int32" and the "second" field will be set to "isUnsigned".  If the 
/// type is not obsolete then "second" will be set to "isSignless".
struct TypeInfo {

  static const TypeInfo* get(const std::string &newType, Types oldType);
  static const TypeInfo* get(const std::string& newType, Types oldType, 
                             const TypeInfo* eTy, const TypeInfo* rTy);

  static const TypeInfo* get(const std::string& newType, Types oldType, 
                       const TypeInfo *eTy, uint64_t elems);

  static const TypeInfo* get(const std::string& newType, Types oldType, 
                       TypeList* TL);

  static const TypeInfo* get(const std::string& newType, const TypeInfo* resTy, 
                       TypeList* TL);

  const TypeInfo* resolve() const;
  bool operator<(const TypeInfo& that) const;

  bool sameNewTyAs(const TypeInfo* that) const {
    return this->newTy == that->newTy;
  }

  bool sameOldTyAs(const TypeInfo* that) const;

  Types getElementTy() const {
    if (elemTy) {
      return elemTy->oldTy;
    }
    return UnresolvedTy;
  }

  unsigned getUpRefNum() const {
    assert(oldTy == UpRefTy && "Can't getUpRefNum on non upreference");
    return atoi(&((getNewTy().c_str())[1])); // skip the slash
  }

  typedef std::vector<const TypeInfo*> UpRefStack;
  void getSignedness(unsigned &sNum, unsigned &uNum, UpRefStack& stk) const;
  std::string makeUniqueName(const std::string& BaseName) const;

  const std::string& getNewTy() const { return newTy; }
  const TypeInfo* getResultType() const { return resultTy; }
  const TypeInfo* getElementType() const { return elemTy; }

  const TypeInfo* getPointerType() const {
    return get(newTy + "*", PointerTy, this, (TypeInfo*)0);
  }

  bool isUnresolved() const { return oldTy == UnresolvedTy; }
  bool isUpReference() const { return oldTy == UpRefTy; }
  bool isVoid() const { return oldTy == VoidTy; }
  bool isBool() const { return oldTy == BoolTy; }
  bool isSigned() const {
    return oldTy == SByteTy || oldTy == ShortTy || 
           oldTy == IntTy || oldTy == LongTy;
  }

  bool isUnsigned() const {
    return oldTy == UByteTy || oldTy == UShortTy || 
           oldTy == UIntTy || oldTy == ULongTy;
  }
  bool isSignless() const { return !isSigned() && !isUnsigned(); }
  bool isInteger() const { return isSigned() || isUnsigned(); }
  bool isIntegral() const { return oldTy == BoolTy || isInteger(); }
  bool isFloatingPoint() const { return oldTy == DoubleTy || oldTy == FloatTy; }
  bool isPacked() const { return oldTy == PackedTy; }
  bool isPointer() const { return oldTy == PointerTy; }
  bool isStruct() const { return oldTy == StructTy || oldTy == PackedStructTy; }
  bool isArray() const { return oldTy == ArrayTy; }
  bool isOther() const { 
    return !isPacked() && !isPointer() && !isFloatingPoint() && !isIntegral(); }
  bool isFunction() const { return oldTy == FunctionTy; }
  bool isComposite() const {
    return isStruct() || isPointer() || isArray() || isPacked();
  }

  bool isAttributeCandidate() const {
    return isIntegral() && getBitWidth() < 32;
  }

  bool isUnresolvedDeep() const;

  unsigned getBitWidth() const;

  const TypeInfo* getIndexedType(const ValueInfo&  VI) const;

  unsigned getNumStructElements() const { 
    return (elements ? elements->size() : 0);
  }

  const TypeInfo* getElement(unsigned idx) const {
    if (elements)
      if (idx < elements->size())
        return (*elements)[idx];
    return 0;
  }


private:
  TypeInfo() 
    : newTy(), oldTy(UnresolvedTy), elemTy(0), resultTy(0), elements(0),
      nelems(0) {
  }

  TypeInfo(const TypeInfo& that); // do not implement
  TypeInfo& operator=(const TypeInfo& that); // do not implement

  ~TypeInfo() { delete elements; }

  struct ltfunctor
  {
    bool operator()(const TypeInfo* X, const TypeInfo* Y) const {
      assert(X && "Can't compare null pointer");
      assert(Y && "Can't compare null pointer");
      return *X < *Y;
    }
  };

  typedef std::set<const TypeInfo*, ltfunctor> TypeRegMap;
  static const TypeInfo* add_new_type(TypeInfo* existing);

  std::string newTy;
  Types oldTy;
  TypeInfo *elemTy;
  TypeInfo *resultTy;
  TypeList *elements;
  uint64_t nelems;
  static TypeRegMap registry;
};

/// This type is used to keep track of the signedness of constants.
struct ConstInfo {
  std::string *cnst;
  const TypeInfo *type;
  void destroy() { delete cnst; }
};

typedef std::vector<ValueInfo> ValueList;

inline void ValueInfo::destroy() { delete val; }

#endif