forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodegenwasm.cpp
More file actions
1849 lines (1626 loc) · 55.1 KB
/
codegenwasm.cpp
File metadata and controls
1849 lines (1626 loc) · 55.1 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
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#include "jitpch.h"
#ifdef _MSC_VER
#pragma hdrstop
#endif
#include "codegen.h"
#include "fgwasm.h"
#ifdef TARGET_64BIT
static const instruction INS_I_const = INS_i64_const;
static const instruction INS_I_add = INS_i64_add;
static const instruction INS_I_sub = INS_i64_sub;
static const instruction INS_I_le_u = INS_i64_le_u;
static const instruction INS_I_gt_u = INS_i64_gt_u;
#else // !TARGET_64BIT
static const instruction INS_I_const = INS_i32_const;
static const instruction INS_I_add = INS_i32_add;
static const instruction INS_I_sub = INS_i32_sub;
static const instruction INS_I_le_u = INS_i32_le_u;
static const instruction INS_I_gt_u = INS_i32_gt_u;
#endif // !TARGET_64BIT
void CodeGen::genMarkLabelsForCodegen()
{
// No work needed here for now.
// We mark labels as needed in genEmitStartBlock.
}
//------------------------------------------------------------------------
// genBeginFnProlog: generate wasm local declarations
//
// TODO-WASM: pre-declare all "register" locals
void CodeGen::genBeginFnProlog()
{
// TODO-WASM: proper local count, local declarations, and shadow stack maintenance
GetEmitter()->emitIns_I(INS_local_cnt, EA_8BYTE, 0);
}
//------------------------------------------------------------------------
// genPushCalleeSavedRegisters: no-op since we don't need to save anything.
//
void CodeGen::genPushCalleeSavedRegisters()
{
}
//------------------------------------------------------------------------
// genAllocLclFrame: initialize the SP and FP locals.
//
// Arguments:
// frameSize - Size of the frame to establish
// initReg - Unused
// pInitRegZeroed - Unused
// maskArgRegsLiveIn - Unused
//
void CodeGen::genAllocLclFrame(unsigned frameSize, regNumber initReg, bool* pInitRegZeroed, regMaskTP maskArgRegsLiveIn)
{
assert(compiler->compGeneratingProlog);
regNumber spReg = GetStackPointerReg();
if (spReg == REG_NA)
{
assert(!isFramePointerUsed());
return;
}
// TODO-WASM: reverse pinvoke frame allocation
//
if (compiler->lvaWasmSpArg == BAD_VAR_NUM)
{
NYI_WASM("alloc local frame for reverse pinvoke");
}
unsigned initialSPLclIndex =
WasmRegToIndex(compiler->lvaGetParameterABIInfo(compiler->lvaWasmSpArg).Segment(0).GetRegister());
unsigned spLclIndex = WasmRegToIndex(spReg);
assert(initialSPLclIndex == spLclIndex);
if (frameSize != 0)
{
GetEmitter()->emitIns_I(INS_local_get, EA_PTRSIZE, initialSPLclIndex);
GetEmitter()->emitIns_I(INS_I_const, EA_PTRSIZE, frameSize);
GetEmitter()->emitIns(INS_I_sub);
GetEmitter()->emitIns_I(INS_local_set, EA_PTRSIZE, spLclIndex);
}
regNumber fpReg = GetFramePointerReg();
if ((fpReg != REG_NA) && (fpReg != spReg))
{
GetEmitter()->emitIns_I(INS_local_get, EA_PTRSIZE, spLclIndex);
GetEmitter()->emitIns_I(INS_local_set, EA_PTRSIZE, WasmRegToIndex(fpReg));
}
}
//------------------------------------------------------------------------
// genEnregisterOSRArgsAndLocals: enregister OSR args and locals.
//
void CodeGen::genEnregisterOSRArgsAndLocals()
{
unreached(); // OSR not supported on WASM.
}
//------------------------------------------------------------------------
// genHomeRegisterParams: place register arguments into their RA-assigned locations.
//
// For the WASM RA, we have a much simplified (compared to LSRA) contract of:
// - If an argument is live on entry in a set of registers, then the RA will
// assign those registers to that argument on entry.
// This means we never need to do any copying or cycle resolution here.
//
// The main motivation for this (along with the obvious CQ implications) is
// obviating the need to adapt the general "RegGraph"-based algorithm to
// !HAS_FIXED_REGISTER_SET constraints (no reg masks).
//
// Arguments:
// initReg - Unused
// initRegStillZeroed - Unused
//
void CodeGen::genHomeRegisterParams(regNumber initReg, bool* initRegStillZeroed)
{
JITDUMP("*************** In genHomeRegisterParams()\n");
auto spillParam = [this](unsigned lclNum, unsigned offset, unsigned paramLclNum, const ABIPassingSegment& segment) {
assert(segment.IsPassedInRegister());
LclVarDsc* varDsc = compiler->lvaGetDesc(lclNum);
if (varDsc->lvTracked && !VarSetOps::IsMember(compiler, compiler->fgFirstBB->bbLiveIn, varDsc->lvVarIndex))
{
return;
}
if (varDsc->lvOnFrame && (!varDsc->lvIsInReg() || varDsc->lvLiveInOutOfHndlr))
{
LclVarDsc* paramVarDsc = compiler->lvaGetDesc(paramLclNum);
var_types storeType = genParamStackType(paramVarDsc, segment);
if (!varDsc->TypeIs(TYP_STRUCT) && (genTypeSize(genActualType(varDsc)) < genTypeSize(storeType)))
{
// Can happen for struct fields due to padding.
storeType = genActualType(varDsc);
}
GetEmitter()->emitIns_I(INS_local_get, EA_PTRSIZE, WasmRegToIndex(GetFramePointerReg()));
GetEmitter()->emitIns_I(INS_local_get, emitActualTypeSize(storeType),
WasmRegToIndex(segment.GetRegister()));
GetEmitter()->emitIns_S(ins_Store(storeType), emitActualTypeSize(storeType), lclNum, offset);
}
if (varDsc->lvIsInReg())
{
assert(varDsc->GetRegNum() == segment.GetRegister());
}
};
for (unsigned lclNum = 0; lclNum < compiler->info.compArgsCount; lclNum++)
{
LclVarDsc* lclDsc = compiler->lvaGetDesc(lclNum);
const ABIPassingInformation& abiInfo = compiler->lvaGetParameterABIInfo(lclNum);
for (const ABIPassingSegment& segment : abiInfo.Segments())
{
if (!segment.IsPassedInRegister())
{
continue;
}
const ParameterRegisterLocalMapping* mapping =
compiler->FindParameterRegisterLocalMappingByRegister(segment.GetRegister());
bool spillToBaseLocal = true;
if (mapping != nullptr)
{
spillParam(mapping->LclNum, mapping->Offset, lclNum, segment);
// If home is shared with base local, then skip spilling to the base local.
if (lclDsc->lvPromoted)
{
spillToBaseLocal = false;
}
}
if (spillToBaseLocal)
{
spillParam(lclNum, segment.Offset, lclNum, segment);
}
}
}
}
void CodeGen::genFnEpilog(BasicBlock* block)
{
#ifdef DEBUG
if (verbose)
{
printf("*************** In genFnEpilog()\n");
}
#endif // DEBUG
ScopedSetVariable<bool> _setGeneratingEpilog(&compiler->compGeneratingEpilog, true);
#ifdef DEBUG
if (compiler->opts.dspCode)
printf("\n__epilog:\n");
#endif // DEBUG
bool jmpEpilog = block->HasFlag(BBF_HAS_JMP);
if (jmpEpilog)
{
NYI_WASM("genFnEpilog: jmpEpilog");
}
// TODO-WASM: shadow stack maintenance
// TODO-WASM: we need to handle the end-of-function case if we reach the end of a codegen for a function
// and do NOT have an epilog. In those cases we currently will not emit an end instruction.
if (block->IsLast() || compiler->bbIsFuncletBeg(block->Next()))
{
instGen(INS_end);
}
else
{
instGen(INS_return);
}
}
void CodeGen::genCaptureFuncletPrologEpilogInfo()
{
}
void CodeGen::genFuncletProlog(BasicBlock* block)
{
#ifdef DEBUG
if (verbose)
{
printf("*************** In genFuncletProlog()\n");
}
#endif
NYI_WASM("genFuncletProlog");
}
void CodeGen::genFuncletEpilog()
{
#ifdef DEBUG
if (verbose)
{
printf("*************** In genFuncletEpilog()\n");
}
#endif
NYI_WASM("genFuncletEpilog");
}
//------------------------------------------------------------------------
// getBlockIndex: return the index of this block in the linear block
// order
//
// Arguments:
// block - block in question
//
// Returns:
// index of block
//
static unsigned getBlockIndex(BasicBlock* block)
{
return block->bbPreorderNum;
}
//------------------------------------------------------------------------
// findTargetDepth: find the depth of a target block in the wasm control flow stack
//
// Arguments:
// targetBlock - block to branch to
// (implicit) compCurBB -- block to branch from
//
// Returns:
// depth of target block in control stack
//
unsigned CodeGen::findTargetDepth(BasicBlock* targetBlock)
{
BasicBlock* const sourceBlock = compiler->compCurBB;
int const h = wasmControlFlowStack->Height();
const unsigned targetIndex = getBlockIndex(targetBlock);
const unsigned sourceIndex = getBlockIndex(sourceBlock);
const bool isBackedge = targetIndex <= sourceIndex;
for (int i = 0; i < h; i++)
{
WasmInterval* const ii = wasmControlFlowStack->Top(i);
unsigned match = 0;
if (isBackedge)
{
// loops bind to start
match = ii->Start();
}
else
{
// blocks bind to end
match = ii->End();
}
if ((match == targetIndex) && (isBackedge == ii->IsLoop()))
{
return i;
}
}
JITDUMP("Could not find " FMT_BB "[%u]%s in active control stack\n", targetBlock->bbNum, targetIndex,
isBackedge ? " (backedge)" : "");
assert(!"Can't find target in control stack");
return ~0;
}
//------------------------------------------------------------------------
// genEmitStartBlock: prepare for codegen in a block
//
// Arguments:
// block - block to prepare for
//
// Notes:
// Updates the wasm control flow stack
//
void CodeGen::genEmitStartBlock(BasicBlock* block)
{
const unsigned cursor = getBlockIndex(block);
// Pop control flow intervals that end here (at most two, block and/or loop)
// and emit wasm END instructions for them.
//
while (!wasmControlFlowStack->Empty() && (wasmControlFlowStack->Top()->End() == cursor))
{
instGen(INS_end);
WasmInterval* interval = wasmControlFlowStack->Pop();
}
// Push control flow for intervals that start here or earlier, and emit
// Wasm BLOCK or LOOP instruction
//
if (wasmCursor < compiler->fgWasmIntervals->size())
{
WasmInterval* interval = compiler->fgWasmIntervals->at(wasmCursor);
WasmInterval* chain = interval->Chain();
while (chain->Start() <= cursor)
{
if (interval->IsLoop())
{
instGen(INS_loop);
}
else
{
instGen(INS_block);
}
wasmCursor++;
wasmControlFlowStack->Push(interval);
if (interval->IsLoop())
{
if (!block->HasFlag(BBF_HAS_LABEL))
{
block->SetFlags(BBF_HAS_LABEL);
genDefineTempLabel(block);
}
}
else
{
BasicBlock* const endBlock = compiler->fgIndexToBlockMap[interval->End()];
if (!endBlock->HasFlag(BBF_HAS_LABEL))
{
endBlock->SetFlags(BBF_HAS_LABEL);
genDefineTempLabel(endBlock);
}
}
if (wasmCursor >= compiler->fgWasmIntervals->size())
{
break;
}
interval = compiler->fgWasmIntervals->at(wasmCursor);
chain = interval->Chain();
}
}
}
//------------------------------------------------------------------------
// genCodeForTreeNode: codegen for a particular tree node
//
// Arguments:
// treeNode - node to generate code for
//
void CodeGen::genCodeForTreeNode(GenTree* treeNode)
{
#ifdef DEBUG
lastConsumedNode = nullptr;
if (compiler->verbose)
{
compiler->gtDispLIRNode(treeNode, "Generating: ");
}
#endif // DEBUG
assert(!treeNode->IsReuseRegVal()); // TODO-WASM-CQ: enable.
// Contained nodes are part of the parent for codegen purposes.
if (treeNode->isContained())
{
return;
}
switch (treeNode->OperGet())
{
case GT_ADD:
case GT_SUB:
case GT_MUL:
case GT_OR:
case GT_XOR:
case GT_AND:
genCodeForBinary(treeNode->AsOp());
break;
case GT_DIV:
case GT_MOD:
case GT_UDIV:
case GT_UMOD:
genCodeForDivMod(treeNode->AsOp());
break;
case GT_LSH:
case GT_RSH:
case GT_RSZ:
case GT_ROL:
case GT_ROR:
genCodeForShift(treeNode);
break;
case GT_EQ:
case GT_NE:
case GT_LT:
case GT_LE:
case GT_GE:
case GT_GT:
genCodeForCompare(treeNode->AsOp());
break;
case GT_LCL_ADDR:
genCodeForLclAddr(treeNode->AsLclFld());
break;
case GT_LCL_FLD:
genCodeForLclFld(treeNode->AsLclFld());
break;
case GT_LCL_VAR:
genCodeForLclVar(treeNode->AsLclVar());
break;
case GT_STORE_LCL_VAR:
genCodeForStoreLclVar(treeNode->AsLclVar());
break;
case GT_JTRUE:
genCodeForJTrue(treeNode->AsOp());
break;
case GT_SWITCH:
genTableBasedSwitch(treeNode);
break;
case GT_RETURN:
genReturn(treeNode);
break;
case GT_IL_OFFSET:
// Do nothing; this node is a marker for debug info.
break;
case GT_NOP:
break;
case GT_NO_OP:
instGen(INS_nop);
break;
case GT_CNS_INT:
case GT_CNS_LNG:
case GT_CNS_DBL:
genCodeForConstant(treeNode);
break;
case GT_CAST:
genCodeForCast(treeNode->AsOp());
break;
case GT_NEG:
case GT_NOT:
genCodeForNegNot(treeNode->AsOp());
break;
case GT_NULLCHECK:
genCodeForNullCheck(treeNode->AsIndir());
break;
case GT_IND:
genCodeForIndir(treeNode->AsIndir());
break;
case GT_STOREIND:
genCodeForStoreInd(treeNode->AsStoreInd());
break;
case GT_CALL:
genCall(treeNode->AsCall());
break;
default:
#ifdef DEBUG
NYIRAW(GenTree::OpName(treeNode->OperGet()));
#else
NYI_WASM("Opcode not implemented");
#endif
break;
}
}
//------------------------------------------------------------------------
// genCodeForJTrue: emit Wasm br_if
//
// Arguments:
// treeNode - predicate value
//
void CodeGen::genCodeForJTrue(GenTreeOp* jtrue)
{
BasicBlock* const block = compiler->compCurBB;
assert(block->KindIs(BBJ_COND));
genConsumeOperands(jtrue);
BasicBlock* const trueTarget = block->GetTrueTarget();
BasicBlock* const falseTarget = block->GetFalseTarget();
// We don't expect degenerate BBJ_COND
//
assert(trueTarget != falseTarget);
// We don't expect the true target to be the next block.
//
assert(trueTarget != block->Next());
// br_if for true target
//
inst_JMP(EJ_jmpif, trueTarget);
// br for false target, if not fallthrough
//
if (falseTarget != block->Next())
{
inst_JMP(EJ_jmp, falseTarget);
}
}
//------------------------------------------------------------------------
// genTableBasedSwitch: emit Wasm br_table
//
// Arguments:
// treeNode - value to switch on
//
void CodeGen::genTableBasedSwitch(GenTree* treeNode)
{
BasicBlock* const block = compiler->compCurBB;
assert(block->KindIs(BBJ_SWITCH));
genConsumeOperands(treeNode->AsOp());
BBswtDesc* const desc = block->GetSwitchTargets();
unsigned const caseCount = desc->GetCaseCount();
// We don't expect degenerate or default-less switches
//
assert(caseCount > 0);
assert(desc->HasDefaultCase());
// br_table list (labelidx*) labelidx
// list is prefixed with length, which is caseCount - 1
//
GetEmitter()->emitIns_I(INS_br_table, EA_4BYTE, caseCount - 1);
// Emit the list case targets, then default case target
// (which is always the last case in the desc).
//
for (unsigned caseNum = 0; caseNum < caseCount; caseNum++)
{
BasicBlock* const caseTarget = desc->GetCase(caseNum)->getDestinationBlock();
unsigned depth = findTargetDepth(caseTarget);
GetEmitter()->emitIns_J(INS_label, EA_4BYTE, depth, caseTarget);
}
}
//------------------------------------------------------------------------
// PackOperAndType: Pack a genTreeOps and var_types into a uint32_t
//
// Arguments:
// oper - a genTreeOps to pack
// type - a var_types to pack
//
// Return Value:
// oper and type packed into an integer that can be used as a switch value/case
//
static constexpr uint32_t PackOperAndType(genTreeOps oper, var_types type)
{
if ((type == TYP_BYREF) || (type == TYP_REF))
{
type = TYP_I_IMPL;
}
const int shift1 = ConstLog2<TYP_COUNT>::value + 1;
return ((uint32_t)oper << shift1) | ((uint32_t)type);
}
// ------------------------------------------------------------------------
// PackTypes: Pack two var_types together into a uint32_t
// Arguments:
// toType - a var_types to pack
// fromType - a var_types to pack
//
// Return Value:
// The two types packed together into an integer that can be used as a switch/value,
// the primary use case being the handling of operations with two-type variants such
// as casts.
//
static constexpr uint32_t PackTypes(var_types toType, var_types fromType)
{
if (toType == TYP_BYREF || toType == TYP_REF)
{
toType = TYP_I_IMPL;
}
if (fromType == TYP_BYREF || fromType == TYP_REF)
{
fromType = TYP_I_IMPL;
}
const int shift1 = ConstLog2<TYP_COUNT>::value + 1;
return ((uint32_t)toType) | ((uint32_t)fromType << shift1);
}
//------------------------------------------------------------------------
// genIntToIntCast: Generate code for an integer to integer cast
//
// Arguments:
// cast - The GT_CAST node for the integer cast operation
//
// Notes:
// Handles casts to and from small int, int, and long types
// including proper sign extension and truncation as needed.
//
void CodeGen::genIntToIntCast(GenTreeCast* cast)
{
if (cast->gtOverflow())
{
NYI_WASM("Overflow checks");
}
GenIntCastDesc desc(cast);
var_types toType = genActualType(cast->CastToType());
var_types fromType = genActualType(cast->CastOp());
int extendSize = desc.ExtendSrcSize();
instruction ins = INS_none;
assert(fromType == TYP_INT || fromType == TYP_LONG);
genConsumeOperands(cast);
// TODO-WASM: Handle load containment GenIntCastDesc::LOAD_* cases once we mark containment for loads
switch (desc.ExtendKind())
{
case GenIntCastDesc::COPY:
{
if (toType == TYP_INT && fromType == TYP_LONG)
{
ins = INS_i32_wrap_i64;
}
else
{
assert(toType == fromType);
ins = INS_none;
}
break;
}
case GenIntCastDesc::ZERO_EXTEND_SMALL_INT:
{
int andAmount = extendSize == 1 ? 255 : 65535;
if (fromType == TYP_LONG)
{
GetEmitter()->emitIns(INS_i32_wrap_i64);
}
GetEmitter()->emitIns_I(INS_i32_const, EA_4BYTE, andAmount);
ins = INS_i32_and;
break;
}
case GenIntCastDesc::SIGN_EXTEND_SMALL_INT:
{
if (fromType == TYP_LONG)
{
GetEmitter()->emitIns(INS_i32_wrap_i64);
}
ins = (extendSize == 1) ? INS_i32_extend8_s : INS_i32_extend16_s;
break;
}
case GenIntCastDesc::ZERO_EXTEND_INT:
{
ins = INS_i64_extend_u_i32;
break;
}
case GenIntCastDesc::SIGN_EXTEND_INT:
{
ins = INS_i64_extend_s_i32;
break;
}
default:
unreached();
}
if (ins != INS_none)
{
GetEmitter()->emitIns(ins);
}
genProduceReg(cast);
}
//------------------------------------------------------------------------
// genFloatToIntCast: Generate code for a floating point to integer cast
//
// Arguments:
// tree - The GT_CAST node for the float-to-int cast operation
//
// Notes:
// Handles casts from TYP_FLOAT/TYP_DOUBLE to TYP_INT/TYP_LONG.
// Uses saturating truncation instructions (trunc_sat) which clamp
// out-of-range values rather than trapping.
//
void CodeGen::genFloatToIntCast(GenTree* tree)
{
if (tree->gtOverflow())
{
NYI_WASM("Overflow checks");
}
var_types toType = tree->TypeGet();
var_types fromType = tree->AsCast()->CastOp()->TypeGet();
bool isUnsigned = varTypeIsUnsigned(tree->AsCast()->CastToType());
instruction ins = INS_none;
assert(varTypeIsFloating(fromType) && (toType == TYP_INT || toType == TYP_LONG));
genConsumeOperands(tree->AsCast());
switch (PackTypes(fromType, toType))
{
case PackTypes(TYP_FLOAT, TYP_INT):
ins = isUnsigned ? INS_i32_trunc_sat_f32_u : INS_i32_trunc_sat_f32_s;
break;
case PackTypes(TYP_DOUBLE, TYP_INT):
ins = isUnsigned ? INS_i32_trunc_sat_f64_u : INS_i32_trunc_sat_f64_s;
break;
case PackTypes(TYP_FLOAT, TYP_LONG):
ins = isUnsigned ? INS_i64_trunc_sat_f32_u : INS_i64_trunc_sat_f32_s;
break;
case PackTypes(TYP_DOUBLE, TYP_LONG):
ins = isUnsigned ? INS_i64_trunc_sat_f64_u : INS_i64_trunc_sat_f64_s;
break;
default:
unreached();
}
GetEmitter()->emitIns(ins);
genProduceReg(tree);
}
//------------------------------------------------------------------------
// genIntToFloatCast: Generate code for an integer to floating point cast
//
// Arguments:
// tree - The GT_CAST node for the int-to-float cast operation
//
// Notes:
// Handles casts from TYP_INT/TYP_LONG to TYP_FLOAT/TYP_DOUBLE.
// Currently not implemented (NYI_WASM).
//
void CodeGen::genIntToFloatCast(GenTree* tree)
{
NYI_WASM("genIntToFloatCast");
}
//------------------------------------------------------------------------
// genFloatToFloatCast: Generate code for a float to float cast
//
// Arguments:
// tree - The GT_CAST node for the float-to-float cast operation
//
void CodeGen::genFloatToFloatCast(GenTree* tree)
{
var_types toType = tree->TypeGet();
var_types fromType = tree->AsCast()->CastOp()->TypeGet();
instruction ins = INS_none;
genConsumeOperands(tree->AsCast());
switch (PackTypes(toType, fromType))
{
case PackTypes(TYP_FLOAT, TYP_DOUBLE):
ins = INS_f32_demote_f64;
break;
case PackTypes(TYP_DOUBLE, TYP_FLOAT):
ins = INS_f64_promote_f32;
break;
case PackTypes(TYP_FLOAT, TYP_FLOAT):
case PackTypes(TYP_DOUBLE, TYP_DOUBLE):
ins = INS_none;
break;
default:
unreached();
}
if (ins != INS_none)
{
GetEmitter()->emitIns(ins);
}
genProduceReg(tree);
}
//------------------------------------------------------------------------
// genCodeForBinary: Generate code for a binary arithmetic operator
//
// Arguments:
// treeNode - The binary operation for which we are generating code.
//
void CodeGen::genCodeForBinary(GenTreeOp* treeNode)
{
genConsumeOperands(treeNode);
instruction ins;
switch (PackOperAndType(treeNode->OperGet(), treeNode->TypeGet()))
{
case PackOperAndType(GT_ADD, TYP_INT):
if (treeNode->gtOverflow())
NYI_WASM("Overflow checks");
ins = INS_i32_add;
break;
case PackOperAndType(GT_ADD, TYP_LONG):
if (treeNode->gtOverflow())
NYI_WASM("Overflow checks");
ins = INS_i64_add;
break;
case PackOperAndType(GT_ADD, TYP_FLOAT):
ins = INS_f32_add;
break;
case PackOperAndType(GT_ADD, TYP_DOUBLE):
ins = INS_f64_add;
break;
case PackOperAndType(GT_SUB, TYP_INT):
if (treeNode->gtOverflow())
NYI_WASM("Overflow checks");
ins = INS_i32_sub;
break;
case PackOperAndType(GT_SUB, TYP_LONG):
if (treeNode->gtOverflow())
NYI_WASM("Overflow checks");
ins = INS_i64_sub;
break;
case PackOperAndType(GT_SUB, TYP_FLOAT):
ins = INS_f32_sub;
break;
case PackOperAndType(GT_SUB, TYP_DOUBLE):
ins = INS_f64_sub;
break;
case PackOperAndType(GT_MUL, TYP_INT):
if (treeNode->gtOverflow())
NYI_WASM("Overflow checks");
ins = INS_i32_mul;
break;
case PackOperAndType(GT_MUL, TYP_LONG):
if (treeNode->gtOverflow())
NYI_WASM("Overflow checks");
ins = INS_i64_mul;
break;
case PackOperAndType(GT_MUL, TYP_FLOAT):
ins = INS_f32_mul;
break;
case PackOperAndType(GT_MUL, TYP_DOUBLE):
ins = INS_f64_mul;
break;
case PackOperAndType(GT_AND, TYP_INT):
ins = INS_i32_and;
break;
case PackOperAndType(GT_AND, TYP_LONG):
ins = INS_i64_and;
break;
case PackOperAndType(GT_OR, TYP_INT):
ins = INS_i32_or;
break;
case PackOperAndType(GT_OR, TYP_LONG):
ins = INS_i64_or;
break;
case PackOperAndType(GT_XOR, TYP_INT):
ins = INS_i32_xor;
break;
case PackOperAndType(GT_XOR, TYP_LONG):
ins = INS_i64_xor;
break;
default:
ins = INS_none;
NYI_WASM("genCodeForBinary");
break;
}
GetEmitter()->emitIns(ins);
genProduceReg(treeNode);
}
//------------------------------------------------------------------------
// genCodeForDivMod: Generate code for a division or modulus operator
//
// Arguments:
// treeNode - The division or modulus operation for which we are generating code.
//
void CodeGen::genCodeForDivMod(GenTreeOp* treeNode)
{
genConsumeOperands(treeNode);
// wasm stack is
// divisor (top)
// dividend (next)
// ...
// TODO-WASM: To check for exception, we will have to spill these to
// internal registers along the way, like so:
//
// ... push dividend
// tee.local $temp1
// ... push divisor
// tee.local $temp2
// ... exception checks (using $temp1 and $temp2; will introduce flow)
// div/mod op
if (!varTypeIsFloating(treeNode->TypeGet()))
{
ExceptionSetFlags exSetFlags = treeNode->OperExceptions(compiler);
// TODO-WASM:(AnyVal / 0) => DivideByZeroException
//
if ((exSetFlags & ExceptionSetFlags::DivideByZeroException) != ExceptionSetFlags::None)
{
}
// TODO-WASM: (MinInt / -1) => ArithmeticException
//
if ((exSetFlags & ExceptionSetFlags::ArithmeticException) != ExceptionSetFlags::None)
{
}
}
instruction ins;
switch (PackOperAndType(treeNode->OperGet(), treeNode->TypeGet()))
{
case PackOperAndType(GT_DIV, TYP_INT):
ins = INS_i32_div_s;
break;
case PackOperAndType(GT_DIV, TYP_LONG):
ins = INS_i64_div_s;
break;
case PackOperAndType(GT_DIV, TYP_FLOAT):
ins = INS_f32_div;
break;
case PackOperAndType(GT_DIV, TYP_DOUBLE):
ins = INS_f64_div;
break;
case PackOperAndType(GT_UDIV, TYP_INT):
ins = INS_i32_div_u;
break;
case PackOperAndType(GT_UDIV, TYP_LONG):
ins = INS_i64_div_u;
break;
case PackOperAndType(GT_MOD, TYP_INT):
ins = INS_i32_rem_s;
break;
case PackOperAndType(GT_MOD, TYP_LONG):
ins = INS_i64_rem_s;
break;
case PackOperAndType(GT_UMOD, TYP_INT):
ins = INS_i32_rem_u;
break;