@@ -148,129 +148,63 @@ StringRef getBinaryOpcodeString(TIL_BinaryOpcode Op);
148
148
// / All variables and expressions must have a value type.
149
149
// / Pointer types are further subdivided into the various heap-allocated
150
150
// / types, such as functions, records, etc.
151
- // / Structured types that are passed by value (e.g. complex numbers)
152
- // / require special handling; they use BT_ValueRef, and size ST_0.
153
151
struct ValueType {
154
152
enum BaseType : unsigned char {
155
- BT_Void = 0 ,
156
153
BT_Bool,
154
+ BT_AsciiChar,
155
+ BT_WideChar,
156
+ BT_UTF16Char,
157
+ BT_UTF32Char,
157
158
BT_Int,
158
- BT_Float,
159
- BT_String, // String literals
159
+ BT_String, // String literals
160
160
BT_Pointer,
161
- BT_ValueRef
162
161
};
163
162
164
- enum SizeType : unsigned char {
165
- ST_0 = 0 ,
166
- ST_1,
167
- ST_8,
168
- ST_16,
169
- ST_32,
170
- ST_64,
171
- ST_128
172
- };
173
-
174
- ValueType (BaseType B, SizeType Sz, bool S, unsigned char VS)
175
- : Base(B), Size(Sz), Signed(S), VectSize(VS) {}
176
-
177
- inline static SizeType getSizeType (unsigned nbytes);
163
+ ValueType (BaseType B) : Base(B) {}
178
164
179
165
template <class T >
180
166
inline static ValueType getValueType ();
181
167
182
168
BaseType Base;
183
- SizeType Size;
184
- bool Signed;
185
-
186
- // 0 for scalar, otherwise num elements in vector
187
- unsigned char VectSize;
188
169
};
189
170
190
- inline ValueType::SizeType ValueType::getSizeType (unsigned nbytes) {
191
- switch (nbytes) {
192
- case 1 : return ST_8;
193
- case 2 : return ST_16;
194
- case 4 : return ST_32;
195
- case 8 : return ST_64;
196
- case 16 : return ST_128;
197
- default : return ST_0;
198
- }
199
- }
200
-
201
- template <>
202
- inline ValueType ValueType::getValueType<void >() {
203
- return ValueType (BT_Void, ST_0, false , 0 );
171
+ inline bool operator ==(const ValueType &a, const ValueType &b) {
172
+ return a.Base == b.Base ;
204
173
}
205
174
206
175
template <>
207
176
inline ValueType ValueType::getValueType<bool >() {
208
- return ValueType (BT_Bool, ST_1, false , 0 );
209
- }
210
-
211
- template <>
212
- inline ValueType ValueType::getValueType<int8_t >() {
213
- return ValueType (BT_Int, ST_8, true , 0 );
214
- }
215
-
216
- template <>
217
- inline ValueType ValueType::getValueType<uint8_t >() {
218
- return ValueType (BT_Int, ST_8, false , 0 );
177
+ return ValueType (BT_Bool);
219
178
}
220
179
221
- template <>
222
- inline ValueType ValueType::getValueType<int16_t >() {
223
- return ValueType (BT_Int, ST_16, true , 0 );
224
- }
225
-
226
- template <>
227
- inline ValueType ValueType::getValueType<uint16_t >() {
228
- return ValueType (BT_Int, ST_16, false , 0 );
229
- }
230
-
231
- template <>
232
- inline ValueType ValueType::getValueType<int32_t >() {
233
- return ValueType (BT_Int, ST_32, true , 0 );
234
- }
235
-
236
- template <>
237
- inline ValueType ValueType::getValueType<uint32_t >() {
238
- return ValueType (BT_Int, ST_32, false , 0 );
239
- }
240
-
241
- template <>
242
- inline ValueType ValueType::getValueType<int64_t >() {
243
- return ValueType (BT_Int, ST_64, true , 0 );
180
+ template <> inline ValueType ValueType::getValueType<char >() {
181
+ return ValueType (BT_AsciiChar);
244
182
}
245
183
246
- template <>
247
- inline ValueType ValueType::getValueType<uint64_t >() {
248
- return ValueType (BT_Int, ST_64, false , 0 );
184
+ template <> inline ValueType ValueType::getValueType<wchar_t >() {
185
+ return ValueType (BT_WideChar);
249
186
}
250
187
251
- template <>
252
- inline ValueType ValueType::getValueType<float >() {
253
- return ValueType (BT_Float, ST_32, true , 0 );
188
+ template <> inline ValueType ValueType::getValueType<char16_t >() {
189
+ return ValueType (BT_UTF16Char);
254
190
}
255
191
256
- template <>
257
- inline ValueType ValueType::getValueType<double >() {
258
- return ValueType (BT_Float, ST_64, true , 0 );
192
+ template <> inline ValueType ValueType::getValueType<char32_t >() {
193
+ return ValueType (BT_UTF32Char);
259
194
}
260
195
261
- template <>
262
- inline ValueType ValueType::getValueType<long double >() {
263
- return ValueType (BT_Float, ST_128, true , 0 );
196
+ template <> inline ValueType ValueType::getValueType<llvm::APInt>() {
197
+ return ValueType (BT_Int);
264
198
}
265
199
266
200
template <>
267
201
inline ValueType ValueType::getValueType<StringRef>() {
268
- return ValueType (BT_String, getSizeType ( sizeof (StringRef)), false , 0 );
202
+ return ValueType (BT_String);
269
203
}
270
204
271
205
template <>
272
206
inline ValueType ValueType::getValueType<void *>() {
273
- return ValueType (BT_Pointer, getSizeType ( sizeof ( void *)), false , 0 );
207
+ return ValueType (BT_Pointer);
274
208
}
275
209
276
210
// / Base class for AST nodes in the typed intermediate language.
@@ -532,37 +466,29 @@ template <class T> class LiteralT;
532
466
533
467
// Base class for literal values.
534
468
class Literal : public SExpr {
535
- public:
536
- Literal (const Expr *C)
537
- : SExpr(COP_Literal), ValType(ValueType::getValueType<void >()), Cexpr(C) {}
469
+ protected:
538
470
Literal (ValueType VT) : SExpr(COP_Literal), ValType(VT) {}
539
- Literal (const Literal &) = default ;
540
471
472
+ public:
541
473
static bool classof (const SExpr *E) { return E->opcode () == COP_Literal; }
542
474
543
- // The clang expression for this literal.
544
- const Expr *clangExpr () const { return Cexpr; }
545
-
546
475
ValueType valueType () const { return ValType; }
547
476
548
477
template <class T > const LiteralT<T>& as () const {
478
+ assert (ValType == ValueType::getValueType<T>());
549
479
return *static_cast <const LiteralT<T>*>(this );
550
480
}
551
481
template <class T > LiteralT<T>& as () {
482
+ assert (ValType == ValueType::getValueType<T>());
552
483
return *static_cast <LiteralT<T>*>(this );
553
484
}
554
485
555
486
template <class V > typename V::R_SExpr traverse (V &Vs, typename V::R_Ctx Ctx);
556
487
557
- template <class C >
558
- typename C::CType compare (const Literal* E, C& Cmp) const {
559
- // TODO: defer actual comparison to LiteralT
560
- return Cmp.trueResult ();
561
- }
488
+ template <class C > typename C::CType compare (const Literal *E, C &Cmp) const ;
562
489
563
490
private:
564
491
const ValueType ValType;
565
- const Expr *Cexpr = nullptr ;
566
492
};
567
493
568
494
// Derived class for literal values, which stores the actual value.
@@ -585,58 +511,55 @@ class LiteralT : public Literal {
585
511
586
512
template <class V >
587
513
typename V::R_SExpr Literal::traverse (V &Vs, typename V::R_Ctx Ctx) {
588
- if (Cexpr)
589
- return Vs.reduceLiteral (*this );
590
-
591
514
switch (ValType.Base ) {
592
- case ValueType::BT_Void:
593
- break ;
594
515
case ValueType::BT_Bool:
595
516
return Vs.reduceLiteralT (as<bool >());
596
- case ValueType::BT_Int: {
597
- switch (ValType.Size ) {
598
- case ValueType::ST_8:
599
- if (ValType.Signed )
600
- return Vs.reduceLiteralT (as<int8_t >());
601
- else
602
- return Vs.reduceLiteralT (as<uint8_t >());
603
- case ValueType::ST_16:
604
- if (ValType.Signed )
605
- return Vs.reduceLiteralT (as<int16_t >());
606
- else
607
- return Vs.reduceLiteralT (as<uint16_t >());
608
- case ValueType::ST_32:
609
- if (ValType.Signed )
610
- return Vs.reduceLiteralT (as<int32_t >());
611
- else
612
- return Vs.reduceLiteralT (as<uint32_t >());
613
- case ValueType::ST_64:
614
- if (ValType.Signed )
615
- return Vs.reduceLiteralT (as<int64_t >());
616
- else
617
- return Vs.reduceLiteralT (as<uint64_t >());
618
- default :
619
- break ;
620
- }
621
- }
622
- case ValueType::BT_Float: {
623
- switch (ValType.Size ) {
624
- case ValueType::ST_32:
625
- return Vs.reduceLiteralT (as<float >());
626
- case ValueType::ST_64:
627
- return Vs.reduceLiteralT (as<double >());
628
- default :
629
- break ;
630
- }
631
- }
517
+ case ValueType::BT_AsciiChar:
518
+ return Vs.reduceLiteralT (as<char >());
519
+ case ValueType::BT_WideChar:
520
+ return Vs.reduceLiteralT (as<wchar_t >());
521
+ case ValueType::BT_UTF16Char:
522
+ return Vs.reduceLiteralT (as<char16_t >());
523
+ case ValueType::BT_UTF32Char:
524
+ return Vs.reduceLiteralT (as<char32_t >());
525
+ case ValueType::BT_Int:
526
+ return Vs.reduceLiteralT (as<llvm::APInt>());
632
527
case ValueType::BT_String:
633
528
return Vs.reduceLiteralT (as<StringRef>());
634
529
case ValueType::BT_Pointer:
635
- return Vs.reduceLiteralT (as<void *>());
636
- case ValueType::BT_ValueRef:
637
- break ;
530
+ return Vs.reduceLiteralT (as<void *>());
531
+ }
532
+ llvm_unreachable (" Invalid BaseType" );
533
+ }
534
+
535
+ template <class C >
536
+ typename C::CType Literal::compare (const Literal *E, C &Cmp) const {
537
+ typename C::CType Ct = Cmp.compareIntegers (ValType.Base , E->ValType .Base );
538
+ if (Cmp.notTrue (Ct))
539
+ return Ct;
540
+ switch (ValType.Base ) {
541
+ case ValueType::BT_Bool:
542
+ return Cmp.compareIntegers (as<bool >().value (), E->as <bool >().value ());
543
+ case ValueType::BT_AsciiChar:
544
+ return Cmp.compareIntegers (as<char >().value (), E->as <char >().value ());
545
+ case ValueType::BT_WideChar:
546
+ return Cmp.compareIntegers (as<wchar_t >().value (), E->as <wchar_t >().value ());
547
+ case ValueType::BT_UTF16Char:
548
+ return Cmp.compareIntegers (as<char16_t >().value (),
549
+ E->as <char16_t >().value ());
550
+ case ValueType::BT_UTF32Char:
551
+ return Cmp.compareIntegers (as<char32_t >().value (),
552
+ E->as <char32_t >().value ());
553
+ case ValueType::BT_Int:
554
+ return Cmp.compareIntegers (as<llvm::APInt>().value (),
555
+ E->as <llvm::APInt>().value ());
556
+ case ValueType::BT_String:
557
+ return Cmp.compareStrings (as<StringRef>().value (),
558
+ E->as <StringRef>().value ());
559
+ case ValueType::BT_Pointer:
560
+ return Cmp.trueResult ();
638
561
}
639
- return Vs. reduceLiteral (* this );
562
+ llvm_unreachable ( " Invalid BaseType " );
640
563
}
641
564
642
565
// / A Literal pointer to an object allocated in memory.
0 commit comments