blob: 9c8b6a01425d1170e5062cd0569cb384e4eae3ad [file] [log] [blame]
Reid Spencer90eb4d62007-01-05 17:18:58 +00001//===-- UpgradeInternals.h - Internal parser definitionsr -------*- 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 UPGRADE_INTERNALS_H
16#define UPGRADE_INTERNALS_H
17
18#include <llvm/ADT/StringExtras.h>
19#include <string>
20#include <istream>
21#include <vector>
22#include <set>
23#include <cassert>
24
25// Global variables exported from the lexer...
26
27extern std::string CurFileName;
28extern std::string Textin;
29extern int Upgradelineno;
30extern std::istream* LexInput;
31
32struct TypeInfo;
33typedef std::vector<const TypeInfo*> TypeList;
34
35void UpgradeAssembly(
36 const std::string & infile, std::istream& in, std::ostream &out, bool debug,
37 bool addAttrs);
38
39// Globals exported by the parser...
40extern char* Upgradetext;
41extern int Upgradeleng;
42extern unsigned SizeOfPointer;
43
44int yyerror(const char *ErrorMsg) ;
45
46/// This enum is used to keep track of the original (1.9) type used to form
47/// a type. These are needed for type upgrades and to determine how to upgrade
48/// signed instructions with signless operands.
49enum Types {
50 BoolTy, SByteTy, UByteTy, ShortTy, UShortTy, IntTy, UIntTy, LongTy, ULongTy,
51 FloatTy, DoubleTy, PointerTy, PackedTy, ArrayTy, StructTy, PackedStructTy,
52 OpaqueTy, VoidTy, LabelTy, FunctionTy, UnresolvedTy, UpRefTy
53};
54
55/// This type is used to keep track of the signedness of values. Instead
56/// of creating llvm::Value directly, the parser will create ValueInfo which
57/// associates a Value* with a Signedness indication.
58struct ValueInfo {
59 std::string* val;
60 const TypeInfo* type;
61 bool constant;
62 bool isConstant() const { return constant; }
63 inline void destroy();
64};
65
66/// This type is used to keep track of the signedness of the obsolete
67/// integer types. Instead of creating an llvm::Type directly, the Lexer will
68/// create instances of TypeInfo which retains the signedness indication so
69/// it can be used by the parser for upgrade decisions.
70/// For example if "uint" is encountered then the "first" field will be set
71/// to "int32" and the "second" field will be set to "isUnsigned". If the
72/// type is not obsolete then "second" will be set to "isSignless".
73struct TypeInfo {
74
75 static const TypeInfo* get(const std::string &newType, Types oldType);
76 static const TypeInfo* get(const std::string& newType, Types oldType,
77 const TypeInfo* eTy, const TypeInfo* rTy);
78
79 static const TypeInfo* get(const std::string& newType, Types oldType,
80 const TypeInfo *eTy, uint64_t elems);
81
82 static const TypeInfo* get(const std::string& newType, Types oldType,
83 TypeList* TL);
84
85 static const TypeInfo* get(const std::string& newType, const TypeInfo* resTy,
86 TypeList* TL);
87
88 const TypeInfo* resolve() const;
89 bool operator<(const TypeInfo& that) const;
90
91 bool sameNewTyAs(const TypeInfo* that) const {
92 return this->newTy == that->newTy;
93 }
94
95 bool sameOldTyAs(const TypeInfo* that) const;
96
97 Types getElementTy() const {
98 if (elemTy) {
99 return elemTy->oldTy;
100 }
101 return UnresolvedTy;
102 }
103
104 unsigned getUpRefNum() const {
105 assert(oldTy == UpRefTy && "Can't getUpRefNum on non upreference");
106 return atoi(&((getNewTy().c_str())[1])); // skip the slash
107 }
108
Reid Spencercb03b5a2007-01-06 06:03:09 +0000109 typedef std::vector<const TypeInfo*> UpRefStack;
110 void getSignedness(unsigned &sNum, unsigned &uNum, UpRefStack& stk) const;
111 std::string makeUniqueName(const std::string& BaseName) const;
112
Reid Spencer90eb4d62007-01-05 17:18:58 +0000113 const std::string& getNewTy() const { return newTy; }
114 const TypeInfo* getResultType() const { return resultTy; }
115 const TypeInfo* getElementType() const { return elemTy; }
116
117 const TypeInfo* getPointerType() const {
118 return get(newTy + "*", PointerTy, this, (TypeInfo*)0);
119 }
120
121 bool isUnresolved() const { return oldTy == UnresolvedTy; }
122 bool isUpReference() const { return oldTy == UpRefTy; }
123 bool isVoid() const { return oldTy == VoidTy; }
124 bool isBool() const { return oldTy == BoolTy; }
125 bool isSigned() const {
126 return oldTy == SByteTy || oldTy == ShortTy ||
127 oldTy == IntTy || oldTy == LongTy;
128 }
129
130 bool isUnsigned() const {
131 return oldTy == UByteTy || oldTy == UShortTy ||
132 oldTy == UIntTy || oldTy == ULongTy;
133 }
134 bool isSignless() const { return !isSigned() && !isUnsigned(); }
135 bool isInteger() const { return isSigned() || isUnsigned(); }
136 bool isIntegral() const { return oldTy == BoolTy || isInteger(); }
137 bool isFloatingPoint() const { return oldTy == DoubleTy || oldTy == FloatTy; }
138 bool isPacked() const { return oldTy == PackedTy; }
139 bool isPointer() const { return oldTy == PointerTy; }
140 bool isStruct() const { return oldTy == StructTy || oldTy == PackedStructTy; }
141 bool isArray() const { return oldTy == ArrayTy; }
142 bool isOther() const {
143 return !isPacked() && !isPointer() && !isFloatingPoint() && !isIntegral(); }
144 bool isFunction() const { return oldTy == FunctionTy; }
145 bool isComposite() const {
146 return isStruct() || isPointer() || isArray() || isPacked();
147 }
148
149 bool isAttributeCandidate() const {
150 return isIntegral() && getBitWidth() < 32;
151 }
152
153 bool isUnresolvedDeep() const;
154
155 unsigned getBitWidth() const;
156
157 const TypeInfo* getIndexedType(const ValueInfo& VI) const;
158
159 unsigned getNumStructElements() const {
160 return (elements ? elements->size() : 0);
161 }
162
163 const TypeInfo* getElement(unsigned idx) const {
164 if (elements)
165 if (idx < elements->size())
166 return (*elements)[idx];
167 return 0;
168 }
169
170
171private:
172 TypeInfo()
173 : newTy(), oldTy(UnresolvedTy), elemTy(0), resultTy(0), elements(0),
174 nelems(0) {
175 }
176
177 TypeInfo(const TypeInfo& that); // do not implement
178 TypeInfo& operator=(const TypeInfo& that); // do not implement
179
180 ~TypeInfo() { delete elements; }
181
182 struct ltfunctor
183 {
184 bool operator()(const TypeInfo* X, const TypeInfo* Y) const {
185 assert(X && "Can't compare null pointer");
186 assert(Y && "Can't compare null pointer");
187 return *X < *Y;
188 }
189 };
190
191 typedef std::set<const TypeInfo*, ltfunctor> TypeRegMap;
192 static const TypeInfo* add_new_type(TypeInfo* existing);
193
194 std::string newTy;
195 Types oldTy;
196 TypeInfo *elemTy;
197 TypeInfo *resultTy;
198 TypeList *elements;
199 uint64_t nelems;
200 static TypeRegMap registry;
201};
202
203/// This type is used to keep track of the signedness of constants.
204struct ConstInfo {
205 std::string *cnst;
206 const TypeInfo *type;
207 void destroy() { delete cnst; }
208};
209
210typedef std::vector<ValueInfo> ValueList;
211
212inline void ValueInfo::destroy() { delete val; }
213
214#endif