-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary.ts
More file actions
863 lines (759 loc) · 31.9 KB
/
binary.ts
File metadata and controls
863 lines (759 loc) · 31.9 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
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
import {
Expression,
SourceLocation,
NumberNode,
StringNode,
MethodCallNode,
IndexAccessNode,
} from "../../../ast/types.js";
import type { IStringGenerator } from "../../infrastructure/generator-context.js";
import { getWantsI1, setWantsI1 } from "../condition-generator.js";
interface ControlFlowGeneratorLike {
generateLogicalOp(op: string, left: Expression, right: Expression, params: string[]): string;
}
export interface BinaryExpressionGeneratorContext {
nextTemp(): string;
nextLabel(prefix: string): string;
getCurrentLabel(): string;
emit(instruction: string): void;
emitIcmp(pred: string, type: string, lhs: string, rhs: string): string;
emitBr(label: string): void;
emitBrCond(cond: string, thenLabel: string, elseLabel: string): void;
emitLabel(name: string): void;
emitCall(retType: string, func: string, args: string): string;
emitBitcast(value: string, fromType: string, toType: string): string;
isStringExpression(expr: Expression): boolean;
variableTypes: Map<string, string>;
getVariableType(name: string): string | undefined;
setVariableType(name: string, type: string): void;
ensureDouble(value: string): string;
ensureI64(value: string): string;
controlFlowGen: ControlFlowGeneratorLike;
readonly stringGen: IStringGenerator;
generateExpression(expr: Expression, params: string[]): string;
emitError(message: string, loc?: SourceLocation, suggestion?: string): never;
emitLoad(type: string, ptr: string): string;
isUint8ArrayExpression(expr: Expression): boolean;
}
/**
* BinaryExpressionGenerator
*
* Handles binary operations:
* - Logical operators (&&, ||) with short-circuit evaluation
* - String concatenation (+)
* - Arithmetic operators (+, -, *, /, %)
* - Bitwise operators (&, |, ^, <<, >>)
* - Comparison operators (<, >, <=, >=, ==, !=, ===, !==)
* - String comparisons use strcmp
* - Numeric comparisons use fcmp
*/
export class BinaryExpressionGenerator {
constructor(private ctx: BinaryExpressionGeneratorContext) {}
generate(op: string, left: Expression, right: Expression, params: string[]): string {
if (op === "&&" || op === "||" || op === "??") {
const savedI1 = getWantsI1();
setWantsI1(false);
const result = this.ctx.controlFlowGen.generateLogicalOp(op, left, right, params);
setWantsI1(savedI1);
return result;
}
if (op === "+" && (this.ctx.isStringExpression(left) || this.ctx.isStringExpression(right))) {
return this.ctx.stringGen.doGenerateStringConcat(left, right, params);
}
if (op === "===" || op === "!==" || op === "==" || op === "!=") {
const charAtResult = this.tryOptimizeCharAtComparison(op, left, right, params);
if (charAtResult !== "") return charAtResult;
}
if (
op === "===" ||
op === "!==" ||
op === "==" ||
op === "!=" ||
op === "<" ||
op === ">" ||
op === "<=" ||
op === ">="
) {
const u8Result = this.tryOptimizeUint8ArrayComparison(op, left, right, params);
if (u8Result !== "") return u8Result;
}
const leftValue = this.ctx.generateExpression(left, params);
const rightValue = this.ctx.generateExpression(right, params);
// Arithmetic operators (floating-point) — fast-math flags enable LLVM optimizations
const arithMap: { [key: string]: string } = {
"+": "fadd nnan ninf nsz arcp contract reassoc afn",
"-": "fsub nnan ninf nsz arcp contract reassoc afn",
"*": "fmul nnan ninf nsz arcp contract reassoc afn",
"/": "fdiv nnan ninf nsz arcp contract reassoc afn",
};
// Bitwise operators (need to convert double -> i64 -> operate -> double)
const bitwiseMap: { [key: string]: string } = {
"&": "and",
"|": "or",
"^": "xor",
"<<": "shl",
">>": "ashr", // arithmetic shift right (preserves sign)
">>>": "lshr", // logical shift right (zero-fill)
};
// Comparison operators (fcmp returns i1, need to extend to i32)
const cmpMap: { [key: string]: string } = {
"<": "olt", // ordered less than
">": "ogt", // ordered greater than
"<=": "ole", // ordered less or equal
">=": "oge", // ordered greater or equal
"==": "oeq", // ordered equal
"!=": "une", // unordered not equal (true if NaN or not equal)
"===": "oeq", // Strict equality (same as == for double)
"!==": "une", // unordered not equal (true if NaN or not equal)
};
if (op === "**") {
return this.generateExponentiation(leftValue, rightValue);
} else if (op === "%") {
return this.generateModulo(leftValue, rightValue, left, right);
} else if (arithMap[op]) {
return this.generateArithmetic(op, arithMap[op], leftValue, rightValue);
} else if (bitwiseMap[op]) {
return this.generateBitwise(op, bitwiseMap[op], leftValue, rightValue);
} else if (cmpMap[op]) {
return this.generateComparison(op, cmpMap[op], leftValue, rightValue, left, right);
} else {
return this.ctx.emitError(
"Unknown operator: " + op,
undefined,
"supported operators: +, -, *, /, %, **, ==, !=, <, >, <=, >=, &&, ||, ??",
);
}
}
private generateArithmetic(_op: string, llvmOp: string, left: string, right: string): string {
const leftType = this.ctx.getVariableType(left);
const rightType = this.ctx.getVariableType(right);
if (leftType === "i64" && rightType === "i64" && _op !== "/") {
const i64Op = _op === "+" ? "add" : _op === "-" ? "sub" : "mul";
const temp = this.ctx.nextTemp();
this.ctx.emit(`${temp} = ${i64Op} i64 ${left}, ${right}`);
this.ctx.setVariableType(temp, "i64");
return temp;
}
if (leftType === "i8*" || (leftType && leftType.indexOf("*") !== -1)) {
const asInt = this.ctx.nextTemp();
this.ctx.emit(`${asInt} = ptrtoint ${leftType} ${left} to i64`);
const asDouble = this.ctx.nextTemp();
this.ctx.emit(`${asDouble} = sitofp i64 ${asInt} to double`);
left = asDouble;
} else {
left = this.ctx.ensureDouble(left);
}
if (rightType === "i8*" || (rightType && rightType.indexOf("*") !== -1)) {
const asInt = this.ctx.nextTemp();
this.ctx.emit(`${asInt} = ptrtoint ${rightType} ${right} to i64`);
const asDouble = this.ctx.nextTemp();
this.ctx.emit(`${asDouble} = sitofp i64 ${asInt} to double`);
right = asDouble;
} else {
right = this.ctx.ensureDouble(right);
}
const temp = this.ctx.nextTemp();
this.ctx.emit(`${temp} = ${llvmOp} double ${left}, ${right}`);
this.ctx.setVariableType(temp, "double");
return temp;
}
private isKnownInteger(expr: Expression): boolean {
const exprTyped = expr as NumberNode;
if (exprTyped.type === "number" && typeof exprTyped.value === "number") {
return Number.isInteger(exprTyped.value);
}
return false;
}
private generateModulo(
left: string,
right: string,
leftExpr: Expression,
rightExpr: Expression,
): string {
const leftType = this.ctx.getVariableType(left);
const rightType = this.ctx.getVariableType(right);
if (leftType === "i64" && rightType === "i64") {
const isZero = this.ctx.emitIcmp("eq", "i64", right, "0");
const sremLabel = this.ctx.nextLabel("mod_i64_srem");
const zeroLabel = this.ctx.nextLabel("mod_i64_zero");
const mergeLabel = this.ctx.nextLabel("mod_i64_merge");
this.ctx.emitBrCond(isZero, zeroLabel, sremLabel);
this.ctx.emitLabel(sremLabel);
const sremVal = this.ctx.nextTemp();
this.ctx.emit(`${sremVal} = srem i64 ${left}, ${right}`);
const sremEnd = this.ctx.getCurrentLabel();
this.ctx.emitBr(mergeLabel);
this.ctx.emitLabel(zeroLabel);
const zeroEnd = this.ctx.getCurrentLabel();
this.ctx.emitBr(mergeLabel);
this.ctx.emitLabel(mergeLabel);
const temp = this.ctx.nextTemp();
this.ctx.emit(`${temp} = phi i64 [ ${sremVal}, %${sremEnd} ], [ 0, %${zeroEnd} ]`);
this.ctx.setVariableType(temp, "i64");
return temp;
}
if (leftType === "i8*" || (leftType && leftType.indexOf("*") !== -1)) {
const asInt = this.ctx.nextTemp();
this.ctx.emit(`${asInt} = ptrtoint ${leftType} ${left} to i64`);
const asDouble = this.ctx.nextTemp();
this.ctx.emit(`${asDouble} = sitofp i64 ${asInt} to double`);
left = asDouble;
} else {
left = this.ctx.ensureDouble(left);
}
if (rightType === "i8*" || (rightType && rightType.indexOf("*") !== -1)) {
const asInt = this.ctx.nextTemp();
this.ctx.emit(`${asInt} = ptrtoint ${rightType} ${right} to i64`);
const asDouble = this.ctx.nextTemp();
this.ctx.emit(`${asDouble} = sitofp i64 ${asInt} to double`);
right = asDouble;
} else {
right = this.ctx.ensureDouble(right);
}
const leftIsKnown = this.isKnownInteger(leftExpr);
const rightIsKnown = this.isKnownInteger(rightExpr);
if (leftIsKnown && rightIsKnown) {
const rightIsZero = this.ctx.nextTemp();
this.ctx.emit(`${rightIsZero} = fcmp oeq double ${right}, 0.0`);
const knownSremLabel = this.ctx.nextLabel("mod_known_srem");
const knownNanLabel = this.ctx.nextLabel("mod_known_nan");
const knownMergeLabel = this.ctx.nextLabel("mod_known_merge");
this.ctx.emitBrCond(rightIsZero, knownNanLabel, knownSremLabel);
this.ctx.emitLabel(knownSremLabel);
const leftInt = this.ctx.nextTemp();
this.ctx.emit(`${leftInt} = fptosi double ${left} to i64`);
const rightInt = this.ctx.nextTemp();
this.ctx.emit(`${rightInt} = fptosi double ${right} to i64`);
const sremResult = this.ctx.nextTemp();
this.ctx.emit(`${sremResult} = srem i64 ${leftInt}, ${rightInt}`);
const intResult = this.ctx.nextTemp();
this.ctx.emit(`${intResult} = sitofp i64 ${sremResult} to double`);
const knownSremEnd = this.ctx.getCurrentLabel();
this.ctx.emitBr(knownMergeLabel);
this.ctx.emitLabel(knownNanLabel);
const knownNanEnd = this.ctx.getCurrentLabel();
this.ctx.emitBr(knownMergeLabel);
this.ctx.emitLabel(knownMergeLabel);
const result = this.ctx.nextTemp();
this.ctx.emit(
`${result} = phi double [ ${intResult}, %${knownSremEnd} ], [ 0x7FF8000000000000, %${knownNanEnd} ]`,
);
this.ctx.setVariableType(result, "double");
return result;
}
let bothInt: string;
if (leftIsKnown) {
const rightTrunc = this.ctx.emitCall("double", "@llvm.trunc.f64", `double ${right}`);
bothInt = this.ctx.nextTemp();
this.ctx.emit(`${bothInt} = fcmp oeq double ${right}, ${rightTrunc}`);
} else if (rightIsKnown) {
const leftTrunc = this.ctx.emitCall("double", "@llvm.trunc.f64", `double ${left}`);
bothInt = this.ctx.nextTemp();
this.ctx.emit(`${bothInt} = fcmp oeq double ${left}, ${leftTrunc}`);
} else {
const leftTrunc = this.ctx.emitCall("double", "@llvm.trunc.f64", `double ${left}`);
const leftIsInt = this.ctx.nextTemp();
this.ctx.emit(`${leftIsInt} = fcmp oeq double ${left}, ${leftTrunc}`);
const rightTrunc = this.ctx.emitCall("double", "@llvm.trunc.f64", `double ${right}`);
const rightIsInt = this.ctx.nextTemp();
this.ctx.emit(`${rightIsInt} = fcmp oeq double ${right}, ${rightTrunc}`);
bothInt = this.ctx.nextTemp();
this.ctx.emit(`${bothInt} = and i1 ${leftIsInt}, ${rightIsInt}`);
}
const intModLabel = this.ctx.nextLabel("mod_int");
const floatModLabel = this.ctx.nextLabel("mod_float");
const mergeLabel = this.ctx.nextLabel("mod_merge");
this.ctx.emitBrCond(bothInt, intModLabel, floatModLabel);
this.ctx.emitLabel(intModLabel);
const leftInt = this.ctx.nextTemp();
this.ctx.emit(`${leftInt} = fptosi double ${left} to i64`);
const rightInt = this.ctx.nextTemp();
this.ctx.emit(`${rightInt} = fptosi double ${right} to i64`);
const dynIsZero = this.ctx.emitIcmp("eq", "i64", rightInt, "0");
const dynSremLabel = this.ctx.nextLabel("mod_dyn_srem");
const dynNanLabel = this.ctx.nextLabel("mod_dyn_nan");
this.ctx.emitBrCond(dynIsZero, dynNanLabel, dynSremLabel);
this.ctx.emitLabel(dynSremLabel);
const sremResult = this.ctx.nextTemp();
this.ctx.emit(`${sremResult} = srem i64 ${leftInt}, ${rightInt}`);
const intResult = this.ctx.nextTemp();
this.ctx.emit(`${intResult} = sitofp i64 ${sremResult} to double`);
const intBranchEnd = this.ctx.getCurrentLabel();
this.ctx.emitBr(mergeLabel);
this.ctx.emitLabel(dynNanLabel);
const dynNanEnd = this.ctx.getCurrentLabel();
this.ctx.emitBr(mergeLabel);
this.ctx.emitLabel(floatModLabel);
const fremResult = this.ctx.nextTemp();
this.ctx.emit(`${fremResult} = frem nsz arcp contract reassoc afn double ${left}, ${right}`);
const floatBranchEnd = this.ctx.getCurrentLabel();
this.ctx.emitBr(mergeLabel);
this.ctx.emitLabel(mergeLabel);
const result = this.ctx.nextTemp();
this.ctx.emit(
`${result} = phi double [ ${intResult}, %${intBranchEnd} ], [ 0x7FF8000000000000, %${dynNanEnd} ], [ ${fremResult}, %${floatBranchEnd} ]`,
);
this.ctx.setVariableType(result, "double");
return result;
}
private generateBitwise(_op: string, llvmOp: string, left: string, right: string): string {
const leftType = this.ctx.getVariableType(left);
const rightType = this.ctx.getVariableType(right);
let leftInt: string;
if (leftType === "i64") {
leftInt = left;
} else if (leftType === "i8*" || (leftType && leftType.indexOf("*") !== -1)) {
leftInt = this.ctx.nextTemp();
this.ctx.emit(`${leftInt} = ptrtoint ${leftType} ${left} to i64`);
} else {
leftInt = this.ctx.nextTemp();
this.ctx.emit(`${leftInt} = fptosi double ${left} to i64`);
}
let rightInt: string;
if (rightType === "i64") {
rightInt = right;
} else if (rightType === "i8*" || (rightType && rightType.indexOf("*") !== -1)) {
rightInt = this.ctx.nextTemp();
this.ctx.emit(`${rightInt} = ptrtoint ${rightType} ${right} to i64`);
} else {
rightInt = this.ctx.nextTemp();
this.ctx.emit(`${rightInt} = fptosi double ${right} to i64`);
}
const resultInt = this.ctx.nextTemp();
this.ctx.emit(`${resultInt} = ${llvmOp} i64 ${leftInt}, ${rightInt}`);
this.ctx.setVariableType(resultInt, "i64");
return resultInt;
}
private generateExponentiation(left: string, right: string): string {
const dblLeft = this.ctx.ensureDouble(left);
const dblRight = this.ctx.ensureDouble(right);
const result = this.ctx.nextTemp();
this.ctx.emit(`${result} = call double @llvm.pow.f64(double ${dblLeft}, double ${dblRight})`);
this.ctx.setVariableType(result, "double");
return result;
}
private generateComparison(
op: string,
cond: string,
leftValue: string,
rightValue: string,
leftExpr: Expression,
rightExpr: Expression,
): string {
const leftIsNullish = leftExpr.type === "null" || leftExpr.type === "undefined";
const rightIsNullish = rightExpr.type === "null" || rightExpr.type === "undefined";
if (leftIsNullish || rightIsNullish) {
return this.generatePointerNullComparison(op, leftValue, rightValue);
}
const leftIsString = this.ctx.isStringExpression(leftExpr);
const rightIsString = this.ctx.isStringExpression(rightExpr);
const leftType = this.ctx.getVariableType(leftValue) || "double";
const rightType = this.ctx.getVariableType(rightValue) || "double";
const leftIsStringType = leftType === "i8*" || leftValue.startsWith("@.str");
const rightIsStringType = rightType === "i8*" || rightValue.startsWith("@.str");
const leftIsJSONi32 = leftType === "i32";
const rightIsJSONi32 = rightType === "i32";
const isStringOp =
((leftIsString || leftIsStringType) && (rightIsString || rightIsStringType)) ||
(leftIsJSONi32 && (rightIsString || rightIsStringType)) ||
((leftIsString || leftIsStringType) && rightIsJSONi32) ||
(leftIsJSONi32 && rightIsJSONi32);
if (isStringOp) {
const singleCharResult = this.tryOptimizeSingleCharLiteralComparison(
op,
leftValue,
rightValue,
leftExpr,
rightExpr,
);
if (singleCharResult !== "") return singleCharResult;
return this.generateStringComparison(op, leftValue, rightValue);
}
return this.generateNumericComparison(cond, leftValue, rightValue);
}
private generateStringComparison(op: string, left: string, right: string): string {
const leftType = this.ctx.getVariableType(left);
const rightType = this.ctx.getVariableType(right);
let leftPtr = left;
let rightPtr = right;
if (leftType === "i32") {
const temp = this.ctx.nextTemp();
this.ctx.emit(`${temp} = inttoptr i32 ${left} to i8*`);
leftPtr = temp;
}
if (rightType === "i32") {
const temp = this.ctx.nextTemp();
this.ctx.emit(`${temp} = inttoptr i32 ${right} to i8*`);
rightPtr = temp;
}
const leftNull = this.ctx.emitIcmp("eq", "i8*", leftPtr, "null");
const rightNull = this.ctx.emitIcmp("eq", "i8*", rightPtr, "null");
const eitherNull = this.ctx.nextTemp();
this.ctx.emit(`${eitherNull} = or i1 ${leftNull}, ${rightNull}`);
const nullCheckLabel = this.ctx.nextLabel("strcmp_null_check");
const strcmpLabel = this.ctx.nextLabel("strcmp_call");
const mergeLabel = this.ctx.nextLabel("strcmp_merge");
this.ctx.emitBrCond(eitherNull, nullCheckLabel, strcmpLabel);
this.ctx.emitLabel(nullCheckLabel);
const bothNull = this.ctx.nextTemp();
this.ctx.emit(`${bothNull} = and i1 ${leftNull}, ${rightNull}`);
let nullResult: string;
if (op === "==" || op === "===") {
nullResult = bothNull;
} else if (op === "!=" || op === "!==") {
const notBothNull = this.ctx.nextTemp();
this.ctx.emit(`${notBothNull} = xor i1 ${bothNull}, true`);
nullResult = notBothNull;
} else {
const falseVal = this.ctx.emitIcmp("eq", "i32", "0", "1");
nullResult = falseVal;
}
const nullResultInt = this.ctx.nextTemp();
this.ctx.emit(`${nullResultInt} = zext i1 ${nullResult} to i32`);
const nullBranchEnd = this.ctx.getCurrentLabel();
this.ctx.emitBr(mergeLabel);
this.ctx.emitLabel(strcmpLabel);
const strcmpResult = this.ctx.emitCall("i32", "@strcmp", `i8* ${leftPtr}, i8* ${rightPtr}`);
let cmpResult: string;
if (op === "==" || op === "===") {
cmpResult = this.ctx.emitIcmp("eq", "i32", strcmpResult, "0");
} else if (op === "!=" || op === "!==") {
cmpResult = this.ctx.emitIcmp("ne", "i32", strcmpResult, "0");
} else if (op === "<") {
cmpResult = this.ctx.emitIcmp("slt", "i32", strcmpResult, "0");
} else if (op === ">") {
cmpResult = this.ctx.emitIcmp("sgt", "i32", strcmpResult, "0");
} else if (op === "<=") {
cmpResult = this.ctx.emitIcmp("sle", "i32", strcmpResult, "0");
} else if (op === ">=") {
cmpResult = this.ctx.emitIcmp("sge", "i32", strcmpResult, "0");
} else {
cmpResult = this.ctx.emitIcmp("eq", "i32", strcmpResult, "0");
}
const strcmpResultInt = this.ctx.nextTemp();
this.ctx.emit(`${strcmpResultInt} = zext i1 ${cmpResult} to i32`);
const strcmpBranchEnd = this.ctx.getCurrentLabel();
this.ctx.emitBr(mergeLabel);
this.ctx.emitLabel(mergeLabel);
const i32Result = this.ctx.nextTemp();
this.ctx.emit(
`${i32Result} = phi i32 [ ${nullResultInt}, %${nullBranchEnd} ], [ ${strcmpResultInt}, %${strcmpBranchEnd} ]`,
);
const extResult = this.ctx.nextTemp();
this.ctx.emit(`${extResult} = sitofp i32 ${i32Result} to double`);
this.ctx.setVariableType(extResult, "double");
return extResult;
}
private generateNumericComparison(cond: string, left: string, right: string): string {
const leftType = this.ctx.getVariableType(left);
const rightType = this.ctx.getVariableType(right);
if (leftType === "i64" && rightType === "i64") {
const icmpCond =
cond === "olt"
? "slt"
: cond === "ogt"
? "sgt"
: cond === "ole"
? "sle"
: cond === "oge"
? "sge"
: cond === "oeq"
? "eq"
: "ne";
const cmpResult = this.ctx.emitIcmp(icmpCond, "i64", left, right);
if (getWantsI1()) {
setWantsI1(false);
return cmpResult;
}
const i64Result = this.ctx.nextTemp();
this.ctx.emit(`${i64Result} = zext i1 ${cmpResult} to i64`);
this.ctx.setVariableType(i64Result, "i64");
return i64Result;
}
let leftDouble = left;
let rightDouble = right;
if (leftType === "i32") {
const temp = this.ctx.nextTemp();
this.ctx.emit(`${temp} = sitofp i32 ${left} to double`);
leftDouble = temp;
} else if (leftType === "i8*" || (leftType && leftType.indexOf("*") !== -1)) {
const asInt = this.ctx.nextTemp();
this.ctx.emit(`${asInt} = ptrtoint ${leftType} ${left} to i64`);
const temp = this.ctx.nextTemp();
this.ctx.emit(`${temp} = sitofp i64 ${asInt} to double`);
leftDouble = temp;
} else {
leftDouble = this.ctx.ensureDouble(left);
}
if (rightType === "i32") {
const temp = this.ctx.nextTemp();
this.ctx.emit(`${temp} = sitofp i32 ${right} to double`);
rightDouble = temp;
} else if (rightType === "i8*" || (rightType && rightType.indexOf("*") !== -1)) {
const asInt = this.ctx.nextTemp();
this.ctx.emit(`${asInt} = ptrtoint ${rightType} ${right} to i64`);
const temp = this.ctx.nextTemp();
this.ctx.emit(`${temp} = sitofp i64 ${asInt} to double`);
rightDouble = temp;
} else {
rightDouble = this.ctx.ensureDouble(right);
}
const cmpResult = this.ctx.nextTemp();
this.ctx.emit(`${cmpResult} = fcmp ${cond} double ${leftDouble}, ${rightDouble}`);
if (getWantsI1()) {
setWantsI1(false);
this.ctx.setVariableType(cmpResult, "i1");
return cmpResult;
}
const i32Result = this.ctx.nextTemp();
this.ctx.emit(`${i32Result} = zext i1 ${cmpResult} to i32`);
const doubleResult = this.ctx.nextTemp();
this.ctx.emit(`${doubleResult} = sitofp i32 ${i32Result} to double`);
this.ctx.setVariableType(doubleResult, "double");
return doubleResult;
}
private generatePointerNullComparison(op: string, left: string, right: string): string {
const leftType = this.ctx.getVariableType(left) || "i8*";
const rightType = this.ctx.getVariableType(right) || "i8*";
let leftPtr = left;
let rightPtr = right;
if (leftType !== "i8*" && leftType.indexOf("*") !== -1) {
leftPtr = this.ctx.emitBitcast(left, leftType, "i8*");
} else if (leftType === "double") {
const asInt = this.ctx.nextTemp();
this.ctx.emit(`${asInt} = bitcast double ${left} to i64`);
const temp = this.ctx.nextTemp();
this.ctx.emit(`${temp} = inttoptr i64 ${asInt} to i8*`);
leftPtr = temp;
} else if (leftType === "i64") {
const temp = this.ctx.nextTemp();
this.ctx.emit(`${temp} = inttoptr i64 ${left} to i8*`);
leftPtr = temp;
}
if (rightType !== "i8*" && rightType.indexOf("*") !== -1) {
rightPtr = this.ctx.emitBitcast(right, rightType, "i8*");
} else if (rightType === "double") {
const asInt = this.ctx.nextTemp();
this.ctx.emit(`${asInt} = bitcast double ${right} to i64`);
const temp = this.ctx.nextTemp();
this.ctx.emit(`${temp} = inttoptr i64 ${asInt} to i8*`);
rightPtr = temp;
} else if (rightType === "i64") {
const temp = this.ctx.nextTemp();
this.ctx.emit(`${temp} = inttoptr i64 ${right} to i8*`);
rightPtr = temp;
}
let cond = "";
if (op === "==" || op === "===") {
cond = "eq";
} else if (op === "!=" || op === "!==") {
cond = "ne";
} else {
cond = "eq";
}
const cmpResult = this.ctx.emitIcmp(cond, "i8*", leftPtr, rightPtr);
const i32Result = this.ctx.nextTemp();
this.ctx.emit(`${i32Result} = zext i1 ${cmpResult} to i32`);
const doubleResult = this.ctx.nextTemp();
this.ctx.emit(`${doubleResult} = sitofp i32 ${i32Result} to double`);
this.ctx.setVariableType(doubleResult, "double");
return doubleResult;
}
private getSingleCharCode(expr: Expression): number {
if (expr.type !== "string") return -1;
const s = (expr as StringNode).value;
if (s.length !== 1) return -1;
return s.charCodeAt(0);
}
private isCharAtCall(expr: Expression): boolean {
if (expr.type !== "method_call") return false;
const mc = expr as MethodCallNode;
return mc.method === "charAt" && mc.args.length === 1;
}
private tryOptimizeCharAtComparison(
op: string,
left: Expression,
right: Expression,
params: string[],
): string {
let charAtExpr: MethodCallNode;
let charCode: number;
if (this.isCharAtCall(left) && this.getSingleCharCode(right) >= 0) {
charAtExpr = left as MethodCallNode;
charCode = this.getSingleCharCode(right);
} else if (this.isCharAtCall(right) && this.getSingleCharCode(left) >= 0) {
charAtExpr = right as MethodCallNode;
charCode = this.getSingleCharCode(left);
} else {
return "";
}
const strPtr = this.ctx.generateExpression(charAtExpr.object, params);
const indexValue = this.ctx.generateExpression(charAtExpr.args[0], params);
const indexType = this.ctx.getVariableType(indexValue);
let indexI64: string;
if (indexType === "i64") {
indexI64 = indexValue;
} else if (indexType === "double" || !indexType) {
indexI64 = this.ctx.nextTemp();
this.ctx.emit(`${indexI64} = fptosi double ${indexValue} to i64`);
} else if (indexType === "i32") {
indexI64 = this.ctx.nextTemp();
this.ctx.emit(`${indexI64} = sext i32 ${indexValue} to i64`);
} else {
indexI64 = indexValue;
}
const isNeg = this.ctx.emitIcmp("slt", "i64", indexI64, "0");
const loadLabel = this.ctx.nextLabel("charat_cmp_load");
const negLabel = this.ctx.nextLabel("charat_cmp_neg");
const endLabel = this.ctx.nextLabel("charat_cmp_end");
this.ctx.emitBrCond(isNeg, negLabel, loadLabel);
this.ctx.emitLabel(loadLabel);
const strLen = this.ctx.emitCall("i64", "@cs_cached_strlen", `i8* ${strPtr}`);
const inBounds = this.ctx.emitIcmp("slt", "i64", indexI64, strLen);
const cmpLabel = this.ctx.nextLabel("charat_cmp_do");
const oobLabel = this.ctx.nextLabel("charat_cmp_oob");
this.ctx.emitBrCond(inBounds, cmpLabel, oobLabel);
this.ctx.emitLabel(cmpLabel);
const charPtr = this.ctx.nextTemp();
this.ctx.emit(`${charPtr} = getelementptr inbounds i8, i8* ${strPtr}, i64 ${indexI64}`);
const charByte = this.ctx.emitLoad("i8", charPtr);
const isEq = op === "===" || op === "==";
const cmpPred = isEq ? "eq" : "ne";
const validCmp = this.ctx.emitIcmp(cmpPred, "i8", charByte, `${charCode}`);
const validI32 = this.ctx.nextTemp();
this.ctx.emit(`${validI32} = zext i1 ${validCmp} to i32`);
this.ctx.emitBr(endLabel);
this.ctx.emitLabel(oobLabel);
const oobVal = isEq ? "0" : "1";
this.ctx.emitBr(endLabel);
this.ctx.emitLabel(negLabel);
const negVal = isEq ? "0" : "1";
this.ctx.emitBr(endLabel);
this.ctx.emitLabel(endLabel);
const resultI32 = this.ctx.nextTemp();
this.ctx.emit(
`${resultI32} = phi i32 [${validI32}, %${cmpLabel}], [${oobVal}, %${oobLabel}], [${negVal}, %${negLabel}]`,
);
const result = this.ctx.nextTemp();
this.ctx.emit(`${result} = sitofp i32 ${resultI32} to double`);
this.ctx.setVariableType(result, "double");
return result;
}
private isUint8ArrayIndexAccess(expr: Expression): boolean {
if (expr.type !== "index_access") return false;
const ia = expr as IndexAccessNode;
return this.ctx.isUint8ArrayExpression(ia.object);
}
private getSmallIntLiteral(expr: Expression): number {
if (expr.type !== "number") return -1;
const val = (expr as NumberNode).value;
if (val < 0 || val > 255 || Math.floor(val) !== val) return -1;
return val;
}
private tryOptimizeUint8ArrayComparison(
op: string,
left: Expression,
right: Expression,
params: string[],
): string {
let u8Expr: IndexAccessNode;
let literalVal: number;
if (this.isUint8ArrayIndexAccess(left) && this.getSmallIntLiteral(right) >= 0) {
u8Expr = left as IndexAccessNode;
literalVal = this.getSmallIntLiteral(right);
} else if (this.isUint8ArrayIndexAccess(right) && this.getSmallIntLiteral(left) >= 0) {
u8Expr = right as IndexAccessNode;
literalVal = this.getSmallIntLiteral(left);
} else {
return "";
}
const arrayPtr = this.ctx.generateExpression(u8Expr.object, params);
const indexDouble = this.ctx.generateExpression(u8Expr.index, params);
const indexType = this.ctx.getVariableType(indexDouble);
let index = indexDouble;
if (indexType === "double" || !indexType) {
index = this.ctx.nextTemp();
this.ctx.emit(`${index} = fptosi double ${indexDouble} to i32`);
} else if (indexType === "i64") {
index = this.ctx.nextTemp();
this.ctx.emit(`${index} = trunc i64 ${indexDouble} to i32`);
}
const dataFieldPtr = this.ctx.nextTemp();
this.ctx.emit(
`${dataFieldPtr} = getelementptr inbounds %Uint8Array, %Uint8Array* ${arrayPtr}, i32 0, i32 0`,
);
const dataPtr = this.ctx.emitLoad("i8*", dataFieldPtr);
const elemPtr = this.ctx.nextTemp();
this.ctx.emit(`${elemPtr} = getelementptr inbounds i8, i8* ${dataPtr}, i32 ${index}`);
const byteVal = this.ctx.emitLoad("i8", elemPtr);
const icmpPred =
op === "===" || op === "=="
? "eq"
: op === "!==" || op === "!="
? "ne"
: op === "<"
? "ult"
: op === ">"
? "ugt"
: op === "<="
? "ule"
: "uge";
const swapped = this.isUint8ArrayIndexAccess(right) && this.getSmallIntLiteral(left) >= 0;
const lhs = swapped ? `${literalVal}` : byteVal;
const rhs = swapped ? byteVal : `${literalVal}`;
const cmpResult = this.ctx.emitIcmp(icmpPred, "i8", lhs, rhs);
const i32Result = this.ctx.nextTemp();
this.ctx.emit(`${i32Result} = zext i1 ${cmpResult} to i32`);
const result = this.ctx.nextTemp();
this.ctx.emit(`${result} = sitofp i32 ${i32Result} to double`);
this.ctx.setVariableType(result, "double");
return result;
}
private tryOptimizeSingleCharLiteralComparison(
op: string,
leftValue: string,
rightValue: string,
leftExpr: Expression,
rightExpr: Expression,
): string {
let strValue: string;
let charCode: number;
const leftCode = this.getSingleCharCode(leftExpr);
const rightCode = this.getSingleCharCode(rightExpr);
if (leftCode >= 0 && rightCode < 0) {
charCode = leftCode;
strValue = rightValue;
} else if (rightCode >= 0 && leftCode < 0) {
charCode = rightCode;
strValue = leftValue;
} else {
return "";
}
const strType = this.ctx.getVariableType(strValue);
if (strType !== "i8*" && !strValue.startsWith("@.str")) return "";
const byte = this.ctx.emitLoad("i8", strValue);
const secondPtr = this.ctx.nextTemp();
this.ctx.emit(`${secondPtr} = getelementptr inbounds i8, i8* ${strValue}, i64 1`);
const secondByte = this.ctx.emitLoad("i8", secondPtr);
const byteMatch = this.ctx.emitIcmp("eq", "i8", byte, `${charCode}`);
const isNull = this.ctx.emitIcmp("eq", "i8", secondByte, "0");
const isSingleChar = this.ctx.nextTemp();
this.ctx.emit(`${isSingleChar} = and i1 ${byteMatch}, ${isNull}`);
const isEq = op === "===" || op === "==";
const isNe = op === "!==" || op === "!=";
if (!isEq && !isNe) return "";
let cmpBool: string;
if (isEq) {
cmpBool = isSingleChar;
} else {
cmpBool = this.ctx.nextTemp();
this.ctx.emit(`${cmpBool} = xor i1 ${isSingleChar}, true`);
}
const i32Result = this.ctx.nextTemp();
this.ctx.emit(`${i32Result} = zext i1 ${cmpBool} to i32`);
const result = this.ctx.nextTemp();
this.ctx.emit(`${result} = sitofp i32 ${i32Result} to double`);
this.ctx.setVariableType(result, "double");
return result;
}
}