forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIREval.cpp
More file actions
678 lines (600 loc) · 21.2 KB
/
IREval.cpp
File metadata and controls
678 lines (600 loc) · 21.2 KB
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "hermes/IR/IREval.h"
#include "hermes/IR/IRBuilder.h"
#include "hermes/Support/Math.h"
#include "llvh/ADT/SmallString.h"
using namespace hermes;
using llvh::isa;
using llvh::SmallString;
namespace {
/// \returns true when the types \p A and \p B prove that the instances can't
/// be strictly equal.
bool disjointComparisonTypes(Type A, Type B) {
if (!A.isPrimitive() || !B.isPrimitive())
return false;
// Check if types are disjoint.
return Type::intersectTy(A, B).isNoType();
}
bool isNaN(Literal *lit) {
if (auto *number = llvh::dyn_cast<LiteralNumber>(lit)) {
return std::isnan(number->getValue());
}
return false;
}
enum class NumericOrder { LessThan, Equal, GreaterThan, Unordered };
/// \returns the numeric ordering of two values.
llvh::Optional<NumericOrder> getNumericOrder(Literal *LHS, Literal *RHS) {
auto *L = llvh::dyn_cast<LiteralNumber>(LHS);
auto *R = llvh::dyn_cast<LiteralNumber>(RHS);
if (!L || !R)
return llvh::None;
double l = L->getValue();
double r = R->getValue();
if (l < r)
return NumericOrder::LessThan;
if (l > r)
return NumericOrder::GreaterThan;
if (std::isnan(l) || std::isnan(r))
return NumericOrder::Unordered;
return NumericOrder::Equal;
}
SmallString<256> buildString(const StringRef &a, const StringRef &b) {
SmallString<256> result;
result.append(a);
result.append(b);
return result;
}
} // namespace
Literal *hermes::evalUnaryOperator(
UnaryOperatorInst::OpKind kind,
IRBuilder &builder,
Literal *operand) {
switch (kind) {
case UnaryOperatorInst::OpKind::MinusKind:
// Negate constant integers.
switch (operand->getKind()) {
case ValueKind::LiteralNumberKind:
if (auto *literalNum = llvh::dyn_cast<LiteralNumber>(operand)) {
auto V = -literalNum->getValue();
return builder.getLiteralNumber(V);
}
break;
case ValueKind::LiteralUndefinedKind:
return builder.getLiteralNaN();
case ValueKind::LiteralBoolKind:
if (evalIsTrue(builder, operand)) {
return builder.getLiteralNumber(-1);
} else { // evalIsFalse(operand)
return builder.getLiteralNegativeZero();
}
case ValueKind::LiteralNullKind:
return builder.getLiteralNegativeZero();
default:
break;
}
break;
case UnaryOperatorInst::OpKind::TypeofKind:
switch (operand->getKind()) {
case ValueKind::GlobalObjectKind:
case ValueKind::LiteralNullKind:
return builder.getLiteralString("object");
case ValueKind::LiteralUndefinedKind:
return builder.getLiteralString("undefined");
case ValueKind::LiteralBoolKind:
return builder.getLiteralString("boolean");
case ValueKind::LiteralNumberKind:
return builder.getLiteralString("number");
case ValueKind::LiteralStringKind:
return builder.getLiteralString("string");
default:
llvm_unreachable("Invalid literal kind.");
}
break;
case UnaryOperatorInst::OpKind::BangKind:
if (evalIsTrue(builder, operand)) {
return builder.getLiteralBool(false);
}
if (evalIsFalse(builder, operand)) {
return builder.getLiteralBool(true);
}
break;
case UnaryOperatorInst::OpKind::VoidKind:
return builder.getLiteralUndefined();
default:
break;
}
return nullptr;
}
Literal *hermes::evalBinaryOperator(
BinaryOperatorInst::OpKind kind,
IRBuilder &builder,
Literal *lhs,
Literal *rhs) LLVM_NO_SANITIZE("float-divide-by-zero");
Literal *hermes::evalBinaryOperator(
BinaryOperatorInst::OpKind kind,
IRBuilder &builder,
Literal *lhs,
Literal *rhs) {
Type leftTy = lhs->getType();
Type rightTy = rhs->getType();
auto *leftLiteralNum = llvh::dyn_cast<LiteralNumber>(lhs);
auto *rightLiteralNum = llvh::dyn_cast<LiteralNumber>(rhs);
auto *leftNull = llvh::dyn_cast<LiteralNull>(lhs);
auto *rightNull = llvh::dyn_cast<LiteralNull>(rhs);
auto *leftUndef = llvh::dyn_cast<LiteralUndefined>(lhs);
auto *rightUndef = llvh::dyn_cast<LiteralUndefined>(rhs);
auto *leftStr = llvh::dyn_cast<LiteralString>(lhs);
auto *rightStr = llvh::dyn_cast<LiteralString>(rhs);
auto leftNaN = isNaN(lhs);
auto rightNaN = isNaN(rhs);
auto &ctx = builder.getModule()->getContext();
using OpKind = BinaryOperatorInst::OpKind;
if (leftNaN || rightNaN) {
switch (kind) {
case OpKind::EqualKind:
case OpKind::StrictlyEqualKind:
case OpKind::LessThanKind:
case OpKind::LessThanOrEqualKind:
case OpKind::GreaterThanKind:
case OpKind::GreaterThanOrEqualKind:
// Equality and order comparisons with NaN always evaluate to false,
// even in cases like 'NaN == NaN' or 'NaN <= NaN'
return builder.getLiteralBool(false);
case OpKind::NotEqualKind:
case OpKind::StrictlyNotEqualKind:
// Inequality comparisons with NaN always evaluate to true, even in
// cases like 'NaN != NaN' or 'NaN !== NaN'
return builder.getLiteralBool(true);
case OpKind::LeftShiftKind:
case OpKind::RightShiftKind:
case OpKind::UnsignedRightShiftKind:
// The code for bitwise shift operators below properly handles
// NaN, so we break out of the switch and go through to there
break;
case OpKind::AddKind:
// If we're trying to add NaN with a string, then we need to perform
// string concatenation with "NaN" and the string operand
if (leftStr) {
SmallString<256> result =
buildString(ctx.toString(leftStr->getValue()), "NaN");
return builder.getLiteralString(result.str());
}
if (rightStr) {
SmallString<256> result =
buildString("NaN", ctx.toString(rightStr->getValue()));
return builder.getLiteralString(result.str());
}
// None of the operands are strings, so the expression evaluates
// to NaN
return builder.getLiteralNaN();
case OpKind::SubtractKind:
case OpKind::MultiplyKind:
case OpKind::DivideKind:
case OpKind::ModuloKind:
// Binary arithmetic operations involving NaN evaluate to NaN
return builder.getLiteralNaN();
case OpKind::OrKind:
case OpKind::XorKind:
case OpKind::AndKind:
// The code for bitwise logical operators below properly handles
// NaN, so we break out of the switch and go through to there
break;
default:
// All handling of NaN is done is the current block. The rest of this
// code assumes the literals are not NaN (except for bitwise shifts and
// logical ops), so we break out of the function here, giving up on
// finding a compile-time literal representation of the result.
return nullptr;
}
}
auto numericOrder = getNumericOrder(lhs, rhs);
switch (kind) {
case OpKind::EqualKind: // ==
// Identical literals must be equal.
if (lhs == rhs) {
return builder.getLiteralBool(true);
}
// Handle numeric comparisons:
if (numericOrder.hasValue()) {
switch (numericOrder.getValue()) {
case NumericOrder::LessThan:
return builder.getLiteralBool(false);
case NumericOrder::Equal:
return builder.getLiteralBool(true);
case NumericOrder::GreaterThan:
return builder.getLiteralBool(false);
case NumericOrder::Unordered:
break;
}
}
// Handle string equality:
if (leftStr && rightStr) {
return builder.getLiteralBool(
leftStr->getValue() == rightStr->getValue());
}
break;
case OpKind::NotEqualKind: // !=
// Identical operands can't be non-equal.
if (lhs == rhs) {
return builder.getLiteralBool(false);
}
// Handle numeric comparisons:
if (numericOrder.hasValue()) {
switch (numericOrder.getValue()) {
case NumericOrder::LessThan:
return builder.getLiteralBool(true);
case NumericOrder::Equal:
return builder.getLiteralBool(false);
case NumericOrder::GreaterThan:
return builder.getLiteralBool(true);
case NumericOrder::Unordered:
break;
}
}
// Handle string equality:
if (leftStr && rightStr) {
return builder.getLiteralBool(
leftStr->getValue() != rightStr->getValue());
}
break;
case OpKind::StrictlyEqualKind: // ===
// Identical literals must be equal.
if (lhs == rhs) {
return builder.getLiteralBool(true);
}
// Operands of different types can't be strictly equal.
if (disjointComparisonTypes(leftTy, rightTy))
return builder.getLiteralBool(false);
// Handle numeric comparisons:
if (numericOrder.hasValue()) {
switch (numericOrder.getValue()) {
case NumericOrder::LessThan:
return builder.getLiteralBool(false);
case NumericOrder::Equal:
return builder.getLiteralBool(true);
case NumericOrder::GreaterThan:
return builder.getLiteralBool(false);
case NumericOrder::Unordered:
break;
}
}
// Handle string equality:
if (leftStr && rightStr) {
return builder.getLiteralBool(
leftStr->getValue() == rightStr->getValue());
}
break;
case OpKind::StrictlyNotEqualKind: // !===
// Identical operands can't be non-equal.
if (lhs == rhs) {
return builder.getLiteralBool(false);
}
// Handle numeric comparisons:
if (numericOrder.hasValue()) {
switch (numericOrder.getValue()) {
case NumericOrder::LessThan:
return builder.getLiteralBool(true);
case NumericOrder::Equal:
return builder.getLiteralBool(false);
case NumericOrder::GreaterThan:
return builder.getLiteralBool(true);
case NumericOrder::Unordered:
break;
}
}
// Handle string equality:
if (leftStr && rightStr) {
return builder.getLiteralBool(
leftStr->getValue() != rightStr->getValue());
}
break;
case OpKind::LessThanKind: // <
// Handle comparison to self:
if (!leftTy.isUndefinedType() && lhs == rhs)
return builder.getLiteralBool(false);
// Handle numeric comparisons:
if (numericOrder.hasValue()) {
switch (numericOrder.getValue()) {
case NumericOrder::LessThan:
return builder.getLiteralBool(true);
case NumericOrder::Equal:
return builder.getLiteralBool(false);
case NumericOrder::GreaterThan:
return builder.getLiteralBool(false);
case NumericOrder::Unordered:
break;
}
}
break;
case OpKind::LessThanOrEqualKind: // <=
// Handle comparison to self:
if (!leftTy.isUndefinedType() && lhs == rhs)
return builder.getLiteralBool(true);
// Handle numeric comparisons:
if (numericOrder.hasValue()) {
switch (numericOrder.getValue()) {
case NumericOrder::LessThan:
return builder.getLiteralBool(true);
case NumericOrder::Equal:
return builder.getLiteralBool(true);
case NumericOrder::GreaterThan:
return builder.getLiteralBool(false);
case NumericOrder::Unordered:
break;
}
}
break;
case OpKind::GreaterThanKind: // >
// Handle comparison to self:
if (!leftTy.isUndefinedType() && lhs == rhs)
return builder.getLiteralBool(false);
// Handle numeric comparisons:
if (numericOrder.hasValue()) {
switch (numericOrder.getValue()) {
case NumericOrder::LessThan:
return builder.getLiteralBool(false);
case NumericOrder::Equal:
return builder.getLiteralBool(false);
case NumericOrder::GreaterThan:
return builder.getLiteralBool(true);
case NumericOrder::Unordered:
break;
}
}
break;
case OpKind::GreaterThanOrEqualKind: // >=
// Handle comparison to self:
if (!leftTy.isUndefinedType() && lhs == rhs)
return builder.getLiteralBool(true);
// Handle numeric comparisons:
if (numericOrder.hasValue()) {
switch (numericOrder.getValue()) {
case NumericOrder::LessThan:
return builder.getLiteralBool(false);
case NumericOrder::Equal:
return builder.getLiteralBool(true);
case NumericOrder::GreaterThan:
return builder.getLiteralBool(true);
case NumericOrder::Unordered:
break;
}
}
break;
case OpKind::LeftShiftKind: // << (<<=)
case OpKind::RightShiftKind: // >> (>>=)
case OpKind::UnsignedRightShiftKind: { // >>> (>>>=)
// Convert both operands to literal numbers if they can be.
auto *lnum = evalToNumber(builder, lhs);
auto *rnum = evalToNumber(builder, rhs);
if (!lnum || !rnum) {
// Can't be converted to a literal number.
break;
}
uint32_t shiftCount = rnum->truncateToUInt32() & 0x1f;
// Large enough to hold both int32_t and uint32_t values.
int64_t result = 0;
if (kind == OpKind::LeftShiftKind) {
// Truncate to unsigned so that the shift doesn't happen on negative
// values. Cast it to a 32-bit signed int to get the sign back, then
// promote to 64 bits.
result = static_cast<int32_t>(lnum->truncateToUInt32() << shiftCount);
} else if (kind == OpKind::RightShiftKind) {
result = static_cast<int64_t>(lnum->truncateToInt32() >> shiftCount);
} else {
result = static_cast<int64_t>(lnum->truncateToUInt32() >> shiftCount);
}
return builder.getLiteralNumber(result);
}
case OpKind::AddKind: { // + (+=)
// Handle numeric constants:
if (leftLiteralNum && rightLiteralNum) {
return builder.getLiteralNumber(
leftLiteralNum->getValue() + rightLiteralNum->getValue());
}
// Handle string concat:
if (leftStr && rightStr) {
SmallString<256> result = buildString(
ctx.toString(leftStr->getValue()),
ctx.toString(rightStr->getValue()));
return builder.getLiteralString(result.str());
}
if (leftNull && rightNull) {
return builder.getLiteralPositiveZero();
}
if (leftUndef && rightUndef) {
return builder.getLiteralNaN();
}
if (leftNull) {
if (rightLiteralNum) {
return rhs;
} else if (rightStr) {
SmallString<256> result =
buildString("null", ctx.toString(rightStr->getValue()));
return builder.getLiteralString(result.str());
}
}
if (rightNull) {
if (leftLiteralNum) {
return lhs;
} else if (leftStr) {
SmallString<256> result =
buildString(ctx.toString(leftStr->getValue()), "null");
return builder.getLiteralString(result.str());
}
}
if (leftUndef) {
if (rightLiteralNum) {
return builder.getLiteralNaN();
} else if (rightStr) {
SmallString<256> result =
buildString("undefined", ctx.toString(rightStr->getValue()));
return builder.getLiteralString(result.str());
}
}
if (rightUndef) {
if (leftLiteralNum) {
return builder.getLiteralNaN();
} else if (leftStr) {
SmallString<256> result =
buildString(ctx.toString(leftStr->getValue()), "undefined");
return builder.getLiteralString(result.str());
}
}
break;
}
case OpKind::SubtractKind: // - (-=)
// Handle numeric constants:
if (leftLiteralNum && rightLiteralNum) {
return builder.getLiteralNumber(
leftLiteralNum->getValue() - rightLiteralNum->getValue());
}
break;
case OpKind::MultiplyKind: // * (*=)
// Handle numeric constants:
if (leftLiteralNum && rightLiteralNum) {
return builder.getLiteralNumber(
leftLiteralNum->getValue() * rightLiteralNum->getValue());
}
if ((leftNull && rightLiteralNum) || (rightNull && leftLiteralNum) ||
(leftNull && rightNull)) {
if ((leftLiteralNum && std::signbit(leftLiteralNum->getValue())) ||
(rightLiteralNum && std::signbit(rightLiteralNum->getValue()))) {
return builder.getLiteralNegativeZero();
}
return builder.getLiteralPositiveZero();
}
break;
case OpKind::DivideKind: // / (/=)
if (leftLiteralNum && rightLiteralNum) {
// This relies on IEEE 754 double division. All modern compilers
// implement this.
return builder.getLiteralNumber(
leftLiteralNum->getValue() / rightLiteralNum->getValue());
}
break;
case OpKind::ModuloKind: // % (%=)
// Note that fmod differs slightly from the ES spec with regards to how
// numbers not representable by double are rounded. This difference can be
// ignored in practice, so most JS VMs use fmod.
if (leftLiteralNum && rightLiteralNum) {
return builder.getLiteralNumber(
std::fmod(leftLiteralNum->getValue(), rightLiteralNum->getValue()));
}
break;
case OpKind::ExponentiationKind: // ** (**=)
if (leftLiteralNum && rightLiteralNum) {
return builder.getLiteralNumber(hermes::expOp(
leftLiteralNum->getValue(), rightLiteralNum->getValue()));
}
break;
case OpKind::OrKind: // | (|=)
if (leftLiteralNum && rightLiteralNum) {
return builder.getLiteralNumber(
leftLiteralNum->truncateToInt32() |
rightLiteralNum->truncateToInt32());
}
break;
case OpKind::XorKind: // ^ (^=)
if (leftLiteralNum && rightLiteralNum) {
return builder.getLiteralNumber(
leftLiteralNum->truncateToInt32() ^
rightLiteralNum->truncateToInt32());
}
break;
case OpKind::AndKind: // & (&=)
if (leftLiteralNum && rightLiteralNum) {
return builder.getLiteralNumber(
leftLiteralNum->truncateToInt32() &
rightLiteralNum->truncateToInt32());
}
break;
default:
break;
}
return nullptr;
}
LiteralBool *hermes::evalToBoolean(IRBuilder &builder, Literal *operand) {
bool value;
switch (operand->getKind()) {
case ValueKind::LiteralNullKind:
case ValueKind::LiteralUndefinedKind:
value = false;
break;
case ValueKind::LiteralBoolKind:
value = cast<LiteralBool>(operand)->getValue();
break;
case ValueKind::LiteralNumberKind: {
const auto n = cast<LiteralNumber>(operand)->getValue();
value = !std::isnan(n) && n != 0.0;
break;
}
case ValueKind::LiteralStringKind:
value = !cast<LiteralString>(operand)->getValue().str().empty();
break;
default:
return nullptr;
}
return builder.getLiteralBool(value);
}
LiteralString *hermes::evalToString(IRBuilder &builder, Literal *operand) {
if (auto *str = llvh::dyn_cast<LiteralString>(operand))
return str;
if (auto *num = llvh::dyn_cast<LiteralNumber>(operand)) {
char buf[NUMBER_TO_STRING_BUF_SIZE];
auto len = numberToString(num->getValue(), buf, sizeof(buf));
return builder.getLiteralString(StringRef(buf, len));
}
return nullptr;
}
LiteralNumber *hermes::evalToNumber(IRBuilder &builder, Literal *operand) {
if (auto *numLiteral = llvh::dyn_cast<LiteralNumber>(operand)) {
return numLiteral;
}
if (auto *boolLiteral = llvh::dyn_cast<LiteralBool>(operand)) {
return builder.getLiteralNumber(boolLiteral->getValue());
}
if (operand->getType().isUndefinedType()) {
return builder.getLiteralNaN();
}
if (operand->getType().isNullType()) {
return builder.getLiteralPositiveZero();
}
return nullptr;
}
LiteralNumber *hermes::evalToInt32(IRBuilder &builder, Literal *operand) {
// Eval to a number first, then truncate to a 32-bit int.
LiteralNumber *lit = evalToNumber(builder, operand);
if (!lit) {
return nullptr;
}
double val = lit->getValue();
return builder.getLiteralNumber(truncateToInt32(val));
}
LiteralBool *hermes::evalToBoolean(IRBuilder &builder, Value *operand) {
if (auto *L = llvh::dyn_cast<Literal>(operand)) {
return evalToBoolean(builder, L);
}
Type OpTY = operand->getType();
if (OpTY.isObjectType()) {
return builder.getLiteralBool(true);
}
if (OpTY.isNullType() || OpTY.isUndefinedType()) {
return builder.getLiteralBool(false);
}
return nullptr;
}
bool hermes::evalIsTrue(IRBuilder &builder, Literal *operand) {
if (auto *lit = evalToBoolean(builder, operand))
return lit->getValue();
return false;
}
bool hermes::evalIsFalse(IRBuilder &builder, Literal *operand) {
if (auto *lit = evalToBoolean(builder, operand))
return !lit->getValue();
return false;
}