aboutsummaryrefslogtreecommitdiff
path: root/tools/llvm-upgrade/ParserInternals.h
blob: 9de3588f8c8f500ed92942435358f95298f9984c (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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
//===-- ParserInternals.h - Definitions internal to the parser --*- 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 PARSER_INTERNALS_H
#define PARSER_INTERNALS_H

#include <llvm/ADT/StringExtras.h>
#include <string>
#include <istream>
#include <vector>
#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<TypeInfo*> TypeList;

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

TypeInfo* ResolveType(TypeInfo*& Ty);

// 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, NumericTy
};

/// 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;
  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 {
  TypeInfo() 
    : newTy(0), oldTy(UnresolvedTy), elemTy(0), resultTy(0), elements(0),
      nelems(0) {
  }

  TypeInfo(const char * newType, Types oldType)
    : newTy(0), oldTy(oldType), elemTy(0), resultTy(0), elements(0), nelems(0) {
    newTy = new std::string(newType);
  }

  TypeInfo(std::string *newType, Types oldType, TypeInfo* eTy = 0, 
           TypeInfo *rTy = 0) 
    : newTy(newType), oldTy(oldType), elemTy(eTy), resultTy(rTy), elements(0),
      nelems(0) { 
  }

  TypeInfo(std::string *newType, Types oldType, TypeInfo *eTy, uint64_t elems)
    : newTy(newType), oldTy(oldType), elemTy(eTy), resultTy(0), elements(0), 
      nelems(elems) {
  }

  TypeInfo(std::string *newType, Types oldType, TypeList* TL)
    : newTy(newType), oldTy(oldType), elemTy(0), resultTy(0), elements(TL),
      nelems(0) {
  }

  TypeInfo(std::string *newType, TypeInfo* resTy, TypeList* TL) 
    : newTy(newType), oldTy(FunctionTy), elemTy(0), resultTy(resTy), 
    elements(TL), nelems(0) {
  }

  TypeInfo(const TypeInfo& that)
    : newTy(0), oldTy(that.oldTy), elemTy(0), resultTy(0), elements(0), 
      nelems(0) {
    *this = that;
  }

  TypeInfo& operator=(const TypeInfo& that) {
    oldTy = that.oldTy;
    nelems = that.nelems;
    if (that.newTy)
      newTy = new std::string(*that.newTy);
    if (that.elemTy)
      elemTy = that.elemTy->clone();
    if (that.resultTy)
      resultTy = that.resultTy->clone();
    if (that.elements) {
      elements = new std::vector<TypeInfo*>(that.elements->size());
      *elements = *that.elements;
    }
    return *this;
  }

  ~TypeInfo() { 
    delete newTy; delete elemTy; delete resultTy; delete elements;
  }

  TypeInfo* clone() const { 
    return new TypeInfo(*this);
  }

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

  const std::string& getNewTy() const { return *newTy; }
  void setOldTy(Types Ty) { oldTy = Ty; }

  TypeInfo* getResultType() const { return resultTy; }
  TypeInfo* getElementType() const { return elemTy; }

  TypeInfo* getPointerType() const {
    std::string* ty = new std::string(*newTy + "*");
    return new TypeInfo(ty, PointerTy, this->clone(), (TypeInfo*)0);
  }

  bool isUnresolved() const { return oldTy == UnresolvedTy; }
  bool isNumeric() const { return oldTy == NumericTy; }
  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;
  }

  unsigned getBitWidth() const {
    switch (oldTy) {
      default:
      case LabelTy:
      case VoidTy : return 0;
      case BoolTy : return 1;
      case SByteTy: case UByteTy : return 8;
      case ShortTy: case UShortTy : return 16;
      case IntTy: case UIntTy: case FloatTy: return 32;
      case LongTy: case ULongTy: case DoubleTy : return 64;
      case PointerTy: return SizeOfPointer; // global var
      case PackedTy: 
      case ArrayTy: 
        return nelems * elemTy->getBitWidth();
      case StructTy:
      case PackedStructTy: {
        uint64_t size = 0;
        for (unsigned i<