forked from WebKit/WebKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathriscv64.rb
More file actions
1694 lines (1541 loc) · 62.6 KB
/
riscv64.rb
File metadata and controls
1694 lines (1541 loc) · 62.6 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
# Copyright (C) 2021 Igalia S.L.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
# THE POSSIBILITY OF SUCH DAMAGE.
# Naming conventions
#
# x<number> => GPR, used to operate with 32-bit and 64-bit integer values
# f<number> => FPR, used to operate with 32-bit and 64-bit floating-point values
#
# GPR conventions, to match the baseline JIT:
#
# x0 => not used (except where needed for operations) (RISC-V hard-wired zero register)
# x1 => la (through alias ra) (RISC-V return address register)
# x2 => sp (through alias sp) (RISC-V stack pointer register)
# x3 => not used (RISC-V global pointer register)
# x4 => not used (RISC-V thread pointer register)
# x5 => not used
# x6 => ws0
# x7 => ws1
# x8 => cfr (through alias fp) (RISC-V frame pointer register)
# x9 => csr0
# x10 => t0, a0, wa0, r0
# x11 => t1, a1, wa1, r1
# x12 => t2, a2, wa2
# x13 => t3, a3, wa3
# x14 => t4, a4, wa4
# x15 => t5, a5, wa5
# x16 => t6, a6, wa6
# x17 => t7, a7, wa7
# x18 => csr1
# x19 => csr2
# x20 => csr3
# x21 => csr4
# x22 => csr5
# x23 => csr6 (metadataTable)
# x24 => csr7 (PB)
# x25 => csr8 (numberTag)
# x26 => csr9 (notCellMask)
# x27 => csr10
# x28 => scratch register
# x29 => scratch register
# x30 => scratch register
# x31 => scratch register
#
# FPR conventions, to match the baseline JIT:
#
# f0 => ft0
# f1 => ft1
# f2 => ft2
# f3 => ft3
# f4 => ft4
# f5 => ft5
# f6 => not used
# f7 => not used
# f8 => csfr0
# f9 => csfr1
# f10 => fa0, wfa0
# f11 => fa1, wfa1
# f12 => fa2, wfa2
# f13 => fa3, wfa3
# f14 => fa4, wfa4
# f15 => fa5, wfa5
# f16 => fa6, wfa6
# f17 => fa7, wfa7
# f18 => csfr2
# f19 => csfr3
# f20 => csfr4
# f21 => csfr5
# f22 => csfr6
# f23 => csfr7
# f24 => csfr8
# f25 => csfr9
# f26 => csfr10
# f27 => csfr11
# f28 => scratch register
# f29 => scratch register
# f30 => scratch register
# f31 => scratch register
RISCV64_EXTRA_GPRS = [SpecialRegister.new("x28"), SpecialRegister.new("x29"), SpecialRegister.new("x30"), SpecialRegister.new("x31")]
RISCV64_EXTRA_FPRS = [SpecialRegister.new("f28"), SpecialRegister.new("f29"), SpecialRegister.new("f30"), SpecialRegister.new("f31")]
def riscv64OperandTypes(operands)
return operands.map {
|op|
if op.is_a? SpecialRegister
case op.name
when /^x/
RegisterID
when /^f/
FPRegisterID
else
raise "Invalid SpecialRegister operand #{op.name}"
end
elsif op.is_a? Tmp
case op.kind
when :gpr
RegisterID
when :fpr
FPRegisterID
else
raise "Invalid Tmp operand #{op.kind}"
end
else
op.class
end
}
end
def riscv64RaiseMismatchedOperands(operands)
raise "Unable to match operands #{riscv64OperandTypes(operands)}"
end
def riscv64ValidateOperands(operands, *expected)
riscv64RaiseMismatchedOperands(operands) unless expected.include? riscv64OperandTypes(operands)
end
def riscv64ValidateImmediate(validation, value)
case validation
when :i_immediate
(-0x800..0x7ff).include? value
when :any_immediate
true
when :rv32_shift_immediate
(0..31).include? value
when :rv64_shift_immediate
(0..63).include? value
else
raise "Invalid immediate validation #{validation}"
end
end
class RegisterID
def riscv64Operand
case @name
when 't0', 'a0', 'wa0', 'r0'
'x10'
when 't1', 'a1', 'wa1', 'r1'
'x11'
when 't2', 'a2', 'wa2'
'x12'
when 't3', 'a3', 'wa3'
'x13'
when 't4', 'a4', 'wa4'
'x14'
when 't5', 'a5', 'wa5'
'x15'
when 't6', 'a6', 'wa6'
'x16'
when 't7', 'a7', 'wa7'
'x17'
when 'ws0'
'x6'
when 'ws1'
'x7'
when 'csr0'
'x9'
when 'csr1'
'x18'
when 'csr2'
'x19'
when 'csr3'
'x20'
when 'csr4'
'x21'
when 'csr5'
'x22'
when 'csr6'
'x23'
when 'csr7'
'x24'
when 'csr8'
'x25'
when 'csr9'
'x26'
when 'csr10'
'x27'
when 'lr'
'ra'
when 'sp'
'sp'
when 'cfr'
'fp'
else
raise "Bad register name #{@name} at #{codeOriginString}"
end
end
end
class FPRegisterID
def riscv64Operand
case @name
when 'ft0'
'f0'
when 'ft1'
'f1'
when 'ft2'
'f2'
when 'ft3'
'f3'
when 'ft4'
'f4'
when 'ft5'
'f5'
when 'csfr0'
'f8'
when 'csfr1'
'f9'
when 'fa0', 'wfa0'
'f10'
when 'fa1', 'wfa1'
'f11'
when 'fa2', 'wfa2'
'f12'
when 'fa3', 'wfa3'
'f13'
when 'fa4', 'wfa4'
'f14'
when 'fa5', 'wfa5'
'f15'
when 'fa6', 'wfa6'
'f16'
when 'fa7', 'wfa7'
'f17'
when 'csfr2'
'f18'
when 'csfr3'
'f19'
when 'csfr4'
'f20'
when 'csfr5'
'f21'
when 'csfr6'
'f22'
when 'csfr7'
'f23'
when 'csfr8'
'f24'
when 'csfr9'
'f25'
when 'csfr10'
'f26'
when 'csfr11'
'f27'
else
raise "Bad register name #{@name} at #{codeOriginString}"
end
end
end
class SpecialRegister
def riscv64Operand
@name
end
end
class Immediate
def riscv64Operand(validation = :i_immediate)
raise "Invalid immediate value #{value} at #{codeOriginString}" if riscv64RequiresLoad(validation)
"#{value}"
end
def riscv64RequiresLoad(validation = :i_immediate)
not riscv64ValidateImmediate(validation, value)
end
end
class Address
def riscv64Operand
raise "Invalid offset #{offset.value} at #{codeOriginString}" if riscv64RequiresLoad
"#{offset.value}(#{base.riscv64Operand})"
end
def riscv64RequiresLoad
not riscv64ValidateImmediate(:i_immediate, offset.value)
end
end
class RISCV64RoundingMode < NoChildren
def initialize(mode)
@mode = mode
end
def riscv64RoundingMode
case @mode
when :floor
"rdn"
when :ceil
"rup"
when :round
"rne"
when :truncate
"rtz"
else
raise "Invalid rounding mode #{@mode}"
end
end
end
class RISCV64MemoryOrdering < NoChildren
def initialize(ordering)
@ordering = ordering
end
def riscv64MemoryOrdering
case @ordering
when :rw, :iorw
@ordering.to_s
else
raise "Invalid memory ordering #{@ordering}"
end
end
end
def riscv64LowerEmitMask(newList, node, size, source, destination)
case size
when :b, :h, :i
case size
when :b
shiftSize = 56
when :h
shiftSize = 48
when :i
shiftSize = 32
end
newList << Instruction.new(node.codeOrigin, "rv_slli", [source, Immediate.new(node.codeOrigin, shiftSize), destination])
newList << Instruction.new(node.codeOrigin, "rv_srli", [destination, Immediate.new(node.codeOrigin, shiftSize), destination])
when :p, :q
else
raise "Invalid masking size"
end
end
def riscv64LowerEmitSignExtension(newList, node, size, source, destination)
case size
when :b, :h
case size
when :b
shiftSize = 56
when :h
shiftSize = 32
end
newList << Instruction.new(node.codeOrigin, "rv_slli", [source, Immediate.new(node.codeOrigin, shiftSize), destination])
newList << Instruction.new(node.codeOrigin, "rv_srai", [destination, Immediate.new(node.codeOrigin, shiftSize), destination])
when :i
newList << Instruction.new(node.codeOrigin, "rv_sext.w", [source, destination])
when :p, :q
else
raise "Invalid extension size"
end
end
def riscv64LowerOperandIntoRegister(newList, node, operand)
register = operand
if operand.immediate?
register = Tmp.new(node.codeOrigin, :gpr)
newList << Instruction.new(node.codeOrigin, "rv_li", [operand, register])
end
raise "Invalid register type" unless riscv64OperandTypes([register]) == [RegisterID]
register
end
def riscv64LowerOperandIntoRegisterAndSignExtend(newList, node, operand, size, forcedTmp = :none)
source = riscv64LowerOperandIntoRegister(newList, node, operand)
destination = source
if ([:b, :h, :i].include? size or forcedTmp == :forced_tmp) and not destination.is_a? Tmp
destination = Tmp.new(node.codeOrigin, :gpr)
end
riscv64LowerEmitSignExtension(newList, node, size, source, destination)
destination
end
def riscv64LowerMisplacedAddresses(list)
newList = []
list.each {
| node |
if node.is_a? Instruction
case node.opcode
when /^b(add|sub)i(z|nz|s)$/
case riscv64OperandTypes(node.operands)
when [Immediate, Address, LocalLabelReference]
tmp = Tmp.new(node.codeOrigin, :gpr)
newList << Instruction.new(node.codeOrigin, "loadi", [node.operands[1], tmp])
newList << Instruction.new(node.codeOrigin, "#{$1}i", [tmp, node.operands[0], tmp])
newList << Instruction.new(node.codeOrigin, "storei", [tmp, node.operands[1]])
newList << Instruction.new(node.codeOrigin, "bti#{$2}", [tmp, node.operands[2]])
else
newList << node
end
else
newList << node
end
else
newList << node
end
}
newList
end
def riscv64LowerAddressLoads(list)
newList = []
list.each {
| node |
if node.is_a? Instruction
case node.opcode
when "leap", "leaq"
case riscv64OperandTypes(node.operands)
when [Address, RegisterID]
address, dest = node.operands[0], node.operands[1]
raise "Invalid address" if address.riscv64RequiresLoad
newList << Instruction.new(node.codeOrigin, "rv_addi", [address.base, address.offset, dest])
when [BaseIndex, RegisterID]
bi, dest = node.operands[0], node.operands[1]
newList << Instruction.new(node.codeOrigin, "rv_slli", [bi.index, Immediate.new(node.codeOrigin, bi.scaleShift), dest])
newList << Instruction.new(node.codeOrigin, "rv_add", [dest, bi.base, dest])
if bi.offset.value != 0
offset = Immediate.new(node.codeOrigin, bi.offset.value)
if offset.riscv64RequiresLoad
tmp = Tmp.new(node.codeOrigin, :gpr)
newList << Instruction.new(node.codeOrigin, "rv_li", [offset, tmp])
newList << Instruction.new(node.codeOrigin, "rv_add", [dest, tmp, dest])
else
newList << Instruction.new(node.codeOrigin, "rv_addi", [dest, offset, dest])
end
end
when [LabelReference, RegisterID]
label, dest = node.operands[0], node.operands[1]
newList << Instruction.new(node.codeOrigin, "rv_la", [label, dest])
if label.offset != 0
offset = Immediate.new(node.codeOrigin, label.offset)
if offset.riscv64RequiresLoad
tmp = Tmp.new(node.codeOrigin, :gpr)
newList << Instruction.new(node.codeOrigin, "rv_li", [offset, tmp])
newList << Instruction.new(node.codeOrigin, "rv_add", [dest, tmp, dest])
else
newList << Instruction.new(node.codeOrigin, "rv_addi", [dest, offset, dest])
end
end
else
riscv64RaiseMismatchedOperands(node.operands)
end
when "globaladdr"
riscv64ValidateOperands(node.operands, [LabelReference, RegisterID])
newList << Instruction.new(node.codeOrigin, "rv_la", node.operands)
when "pcrtoaddr"
riscv64ValidateOperands(node.operands, [LabelReference, RegisterID])
newList << Instruction.new(node.codeOrigin, "rv_lla", node.operands)
else
newList << node
end
else
newList << node
end
}
newList
end
def riscv64LowerImmediateSubtraction(list)
def emit(newList, node, size, operands)
riscv64ValidateOperands(operands, [RegisterID, Immediate, RegisterID])
nimmediate = Immediate.new(node.codeOrigin, -operands[1].value)
if nimmediate.riscv64RequiresLoad
tmp = Tmp.new(node.codeOrigin, :gpr)
newList << Instruction.new(node.codeOrigin, "rv_li", [operands[1], tmp])
newList << Instruction.new(node.codeOrigin, "rv_sub", [operands[0], tmp, operands[2]])
else
newList << Instruction.new(node.codeOrigin, "rv_addi", [operands[0], nimmediate, operands[2]])
end
riscv64LowerEmitMask(newList, node, size, operands[2], operands[2])
end
newList = []
list.each {
| node |
if node.is_a? Instruction
case node.opcode
when /^sub(i|p|q)$/
case riscv64OperandTypes(node.operands)
when [RegisterID, Immediate, RegisterID]
emit(newList, node, $1.to_sym, node.operands)
when [Immediate, RegisterID]
emit(newList, node, $1.to_sym, [node.operands[1], node.operands[0], node.operands[1]])
else
raise "Invalid immediate subtraction pattern" if riscv64OperandTypes(node.operands).include? Immediate
newList << node
end
else
newList << node
end
else
newList << node
end
}
newList
end
def riscv64LowerOperation(list)
def emitLoadOperation(newList, node, size)
riscv64ValidateOperands(node.operands, [Address, RegisterID])
case size
when :b
suffix = "bu"
when :bsi, :bsq
suffix = "b"
when :h
suffix = "hu"
when :hsi, :hsq
suffix = "h"
when :i
suffix = "wu"
when :is
suffix = "w"
when :p, :q
suffix = "d"
else
raise "Invalid size #{size}"
end
newList << Instruction.new(node.codeOrigin, "rv_l#{suffix}", node.operands)
case size
when :bsi, :hsi
riscv64LowerEmitMask(newList, node, :i, node.operands[1], node.operands[1])
when :bsq, :hsq
# Nothing to do
end
end
def emitStoreOperation(newList, node, size)
riscv64ValidateOperands(node.operands, [RegisterID, Address])
case size
when :b
suffix = "b"
when :h
suffix = "h"
when :i
suffix = "w"
when :p, :q
suffix = "d"
else
raise "Invalid size #{size}"
end
newList << Instruction.new(node.codeOrigin, "rv_s#{suffix}", node.operands)
end
def emitMove(newList, node)
case riscv64OperandTypes(node.operands)
when [RegisterID, RegisterID]
moveOpcode = "mv"
when [Immediate, RegisterID]
moveOpcode = "li"
else
riscv64RaiseMismatchedOperands(node.operands)
end
newList << Instruction.new(node.codeOrigin, "rv_#{moveOpcode}", node.operands)
end
def emitJump(newList, node)
case riscv64OperandTypes(node.operands)
when [RegisterID]
jumpOpcode = "jr"
when [LabelReference], [LocalLabelReference]
jumpOpcode = "tail"
else
riscv64RaiseMismatchedOperands(node.operands)
end
newList << Instruction.new(node.codeOrigin, "rv_#{jumpOpcode}", node.operands)
end
def emitCall(newList, node)
case riscv64OperandTypes(node.operands)
when [RegisterID]
callOpcode = "jalr"
when [LabelReference]
callOpcode = "call"
else
riscv64RaiseMismatchedOperands(node.operands)
end
newList << Instruction.new(node.codeOrigin, "rv_#{callOpcode}", node.operands)
end
def emitPush(newList, node)
sp = RegisterID.forName(node.codeOrigin, 'sp')
size = 8 * node.operands.size
newList << Instruction.new(node.codeOrigin, "rv_addi", [sp, Immediate.new(node.codeOrigin, -size), sp])
node.operands.reverse.each_with_index {
| op, index |
offset = size - 8 * (index + 1)
newList << Instruction.new(node.codeOrigin, "rv_sd", [op, Address.new(node.codeOrigin, sp, Immediate.new(node.codeOrigin, offset))])
}
end
def emitPop(newList, node)
sp = RegisterID.forName(node.codeOrigin, 'sp')
size = 8 * node.operands.size
node.operands.each_with_index {
| op, index |
offset = size - 8 * (index + 1)
newList << Instruction.new(node.codeOrigin, "rv_ld", [Address.new(node.codeOrigin, sp, Immediate.new(node.codeOrigin, offset)), op])
}
newList << Instruction.new(node.codeOrigin, "rv_addi", [sp, Immediate.new(node.codeOrigin, size), sp])
end
def emitAdditionOperation(newList, node, operation, size)
operands = node.operands
if operands.size == 2
operands = [operands[1], operands[0], operands[1]]
end
if riscv64OperandTypes(operands) == [Immediate, RegisterID, RegisterID]
raise "Invalid subtraction pattern" if operation == :sub
operands = [operands[1], operands[0], operands[2]]
end
riscv64ValidateOperands(operands, [RegisterID, RegisterID, RegisterID], [RegisterID, Immediate, RegisterID])
case operation
when :add, :sub
additionOpcode = operation.to_s
else
raise "Invalid operation #{operation}"
end
raise "Invalid subtraction of immediate" if operands[1].is_a? Immediate and operation == :sub
additionOpcode += ((operands[1].is_a? Immediate) ? "i" : "") + (size == :i ? "w" : "")
newList << Instruction.new(node.codeOrigin, "rv_#{additionOpcode}", operands)
riscv64LowerEmitMask(newList, node, size, operands[2], operands[2])
end
def emitMultiplicationOperation(newList, node, operation, size, signedness)
operands = node.operands
if operands.size == 2
operands = [operands[1], operands[0], operands[1]]
end
if riscv64OperandTypes(operands) == [Immediate, RegisterID, RegisterID]
raise "Invalid division/remainder pattern" if [:div, :rem].include? operation
operands = [operands[1], operands[0], operands[2]]
end
riscv64ValidateOperands(operands, [RegisterID, RegisterID, RegisterID], [RegisterID, Immediate, RegisterID])
case operation
when :mul
multiplicationOpcode = "mul"
when :div, :rem
multiplicationOpcode = operation.to_s + (signedness != :s ? "u" : "")
else
raise "Invalid operation #{operation}"
end
multiplicationOpcode += (size == :i ? "w" : "")
newList << Instruction.new(node.codeOrigin, "rv_#{multiplicationOpcode}", operands)
riscv64LowerEmitMask(newList, node, size, operands[2], operands[2])
end
def emitShiftOperation(newList, node, operation, size)
operands = node.operands
if operands.size == 2
operands = [operands[1], operands[0], operands[1]]
end
riscv64ValidateOperands(operands, [RegisterID, RegisterID, RegisterID], [RegisterID, Immediate, RegisterID])
case operation
when :l
shiftOpcode = "sll"
when :r
shiftOpcode = "sra"
when :ur
shiftOpcode = "srl"
else
raise "Invalid operation #{operation}"
end
shiftOpcode += ((operands[1].is_a? Immediate) ? "i" : "") + (size == :i ? "w" : "")
newList << Instruction.new(node.codeOrigin, "rv_#{shiftOpcode}", operands)
riscv64LowerEmitMask(newList, node, size, operands[2], operands[2])
end
def emitRotateOperation(newList, node, direction, size)
riscv64ValidateOperands(node.operands, [RegisterID, RegisterID])
lhs = node.operands[1]
rhs = node.operands[0]
case size
when :i
bits = 32
suffix = "w"
when :q
bits = 64
suffix = ""
end
inverseAmount = Tmp.new(node.codeOrigin, :gpr)
newList << Instruction.new(node.codeOrigin, "rv_li", [Immediate.new(node.codeOrigin, bits), inverseAmount])
realAmount = Tmp.new(node.codeOrigin, :gpr)
newList << Instruction.new(node.codeOrigin, "rv_rem#{suffix}", [rhs, inverseAmount, realAmount])
newList << Instruction.new(node.codeOrigin, "rv_sub#{suffix}", [inverseAmount, realAmount, inverseAmount])
leftRegister = Tmp.new(node.codeOrigin, :gpr)
newList << Instruction.new(node.codeOrigin, "rv_sll#{suffix}", [lhs, direction == :l ? realAmount : inverseAmount, leftRegister])
rightRegister = Tmp.new(node.codeOrigin, :gpr)
newList << Instruction.new(node.codeOrigin, "rv_srl#{suffix}", [lhs, direction == :l ? inverseAmount : realAmount, rightRegister])
newList << Instruction.new(node.codeOrigin, "rv_or", [leftRegister, rightRegister, lhs])
end
def emitLogicalOperation(newList, node, operation, size)
operands = node.operands
if operands.size == 2
operands = [operands[1], operands[0], operands[1]]
end
riscv64ValidateOperands(operands, [RegisterID, RegisterID, RegisterID], [RegisterID, Immediate, RegisterID])
case operation
when :and, :or, :xor
logicalOpcode = operation.to_s
else
raise "Invalid operation #{operation}"
end
if operands[1].is_a? Immediate
logicalOpcode += "i"
end
newList << Instruction.new(node.codeOrigin, "rv_#{logicalOpcode}", operands)
riscv64LowerEmitMask(newList, node, size, operands[2], operands[2])
end
def emitComplementOperation(newList, node, operation, size)
riscv64ValidateOperands(node.operands, [RegisterID])
case operation
when :neg
complementOpcode = size == :i ? "negw" : "neg"
when :not
complementOpcode = "not"
else
raise "Invalid operation #{operation}"
end
newList << Instruction.new(node.codeOrigin, "rv_#{complementOpcode}", [node.operands[0], node.operands[0]])
riscv64LowerEmitMask(newList, node, size, node.operands[0], node.operands[0])
end
def emitBitExtensionOperation(newList, node, extension, fromSize, toSize)
raise "Invalid operand types" unless riscv64OperandTypes(node.operands) == [RegisterID, RegisterID]
if [[:s, :i, :p], [:s, :i, :q]].include? [extension, fromSize, toSize]
newList << Instruction.new(node.codeOrigin, "rv_sext.w", node.operands)
return
end
source = node.operands[0]
dest = node.operands[1]
if [[:z, :i, :p], [:z, :i, :q]].include? [extension, fromSize, toSize]
newList << Instruction.new(node.codeOrigin, "rv_slli", [source, Immediate.new(node.codeOrigin, 32), dest])
newList << Instruction.new(node.codeOrigin, "rv_srli", [dest, Immediate.new(node.codeOrigin, 32), dest])
return
end
raise "Invalid zero extension" unless extension == :s
case [fromSize, toSize]
when [:b, :i]
newList << Instruction.new(node.codeOrigin, "rv_slli", [source, Immediate.new(node.codeOrigin, 56), dest])
newList << Instruction.new(node.codeOrigin, "rv_srai", [dest, Immediate.new(node.codeOrigin, 24), dest])
newList << Instruction.new(node.codeOrigin, "rv_srli", [dest, Immediate.new(node.codeOrigin, 32), dest])
when [:b, :q]
newList << Instruction.new(node.codeOrigin, "rv_slli", [source, Immediate.new(node.codeOrigin, 56), dest])
newList << Instruction.new(node.codeOrigin, "rv_srai", [dest, Immediate.new(node.codeOrigin, 56), dest])
when [:h, :i]
newList << Instruction.new(node.codeOrigin, "rv_slli", [source, Immediate.new(node.codeOrigin, 48), dest])
newList << Instruction.new(node.codeOrigin, "rv_srai", [dest, Immediate.new(node.codeOrigin, 16), dest])
newList << Instruction.new(node.codeOrigin, "rv_srli", [dest, Immediate.new(node.codeOrigin, 32), dest])
when [:h, :q]
newList << Instruction.new(node.codeOrigin, "rv_slli", [source, Immediate.new(node.codeOrigin, 48), dest])
newList << Instruction.new(node.codeOrigin, "rv_srai", [dest, Immediate.new(node.codeOrigin, 48), dest])
else
raise "Invalid bit-extension combination"
end
end
def emitZeroCountOperation(newList, node, side, size)
riscv64ValidateOperands(node.operands, [RegisterID, RegisterID])
from = node.operands[0]
to = node.operands[1]
case size
when :i
bits = 32
suffix = "w"
when :q
bits = 64
suffix = ""
end
count = Tmp.new(node.codeOrigin, :gpr)
newList << Instruction.new(node.codeOrigin, "rv_xor", [count, count, count])
tmp = Tmp.new(node.codeOrigin, :gpr)
newList << Instruction.new(node.codeOrigin, "rv_li", [Immediate.new(node.codeOrigin, side == :t ? bits : bits - 1), tmp])
loopLabel = LocalLabel.unique("begin_count_loop")
newList << loopLabel
check = Tmp.new(node.codeOrigin, :gpr)
newList << Instruction.new(node.codeOrigin, "rv_srl#{suffix}", [from, side == :t ? count : tmp, check])
newList << Instruction.new(node.codeOrigin, "rv_andi", [check, Immediate.new(node.codeOrigin, 1), check])
returnLabel = LocalLabel.unique("return_count")
newList << Instruction.new(node.codeOrigin, "rv_bgtz", [check, LocalLabelReference.new(node.codeOrigin, returnLabel)])
newList << Instruction.new(node.codeOrigin, "rv_addi#{suffix}", [count, Immediate.new(node.codeOrigin, 1), count])
case side
when :t
newList << Instruction.new(node.codeOrigin, "rv_blt", [count, tmp, LocalLabelReference.new(node.codeOrigin, loopLabel)])
when :l
newList << Instruction.new(node.codeOrigin, "rv_addi#{suffix}", [tmp, Immediate.new(node.codeOrigin, -1), tmp])
newList << Instruction.new(node.codeOrigin, "rv_bgez", [tmp, LocalLabelReference.new(node.codeOrigin, loopLabel)])
end
newList << returnLabel
newList << Instruction.new(node.codeOrigin, "rv_mv", [count, to])
end
newList = []
list.each {
| node |
if node.is_a? Instruction
case node.opcode
when /^load(b|bsi|bsq|h||hsi|hsq|i|is|p|q)$/
emitLoadOperation(newList, node, $1.to_sym)
when /^store(b|h|i|p|q)$/
emitStoreOperation(newList, node, $1.to_sym)
when "move"
emitMove(newList, node)
when "jmp"
emitJump(newList, node)
when "call"
emitCall(newList, node)
when "push"
emitPush(newList, node)
when "pop"
emitPop(newList, node)
when /^(add|sub)(i|p|q)$/
emitAdditionOperation(newList, node, $1.to_sym, $2.to_sym)
when /^(mul|div|rem)(i|p|q)(s?)$/
emitMultiplicationOperation(newList, node, $1.to_sym, $2.to_sym, $3.to_sym)
when /^(l|r)rotate(i|q)$/
emitRotateOperation(newList, node, $1.to_sym, $2.to_sym)
when /^(l|r|ur)shift(i|p|q)$/
emitShiftOperation(newList, node, $1.to_sym, $2.to_sym)
when /^(and|or|xor)(h|i|p|q)$/
emitLogicalOperation(newList, node, $1.to_sym, $2.to_sym)
when /^(neg|not)(i|p|q)$/
emitComplementOperation(newList, node, $1.to_sym, $2.to_sym)
when /^(s|z)x(b|h|i)2(i|p|q)$/
emitBitExtensionOperation(newList, node, $1.to_sym, $2.to_sym, $3.to_sym)
when /^(t|l)zcnt(i|q)$/
emitZeroCountOperation(newList, node, $1.to_sym, $2.to_sym)
when "break"
newList << Instruction.new(node.codeOrigin, "rv_ebreak", [])
when "nop", "ret"
newList << Instruction.new(node.codeOrigin, "rv_#{node.opcode}", [])
when "memfence"
newList << Instruction.new(node.codeOrigin, "rv_fence", [RISCV64MemoryOrdering.new(:rw), RISCV64MemoryOrdering.new(:rw)])
when "fence"
newList << Instruction.new(node.codeOrigin, "rv_fence", [RISCV64MemoryOrdering.new(:iorw), RISCV64MemoryOrdering.new(:iorw)])
else
newList << node
end
else
newList << node
end
}
newList
end
def riscv64LowerTest(list)
def branchOpcode(test)
case test
when :s
"bltz"
when :z
"beqz"
when :nz
"bnez"
else
raise "Invalid test-branch opcode"
end
end
def setOpcode(test)
case test
when :s
"sltz"
when :z
"seqz"
when :nz
"snez"
else
raise "Invalid test-set opcode"
end
end
def emit(newList, node, size, opcode)
if node.operands.size == 2
newList << Instruction.new(node.codeOrigin, "rv_#{opcode}", node.operands)
return
end
if node.operands[0].immediate? and node.operands[0].value == -1
newList << Instruction.new(node.codeOrigin, "rv_#{opcode}", [node.operands[1], node.operands[2]])
return
end
if node.operands[1].immediate? and node.operands[1].value == -1
newList << Instruction.new(node.codeOrigin, "rv_#{opcode}", [node.operands[0], node.operands[2]])
return
end
value = node.operands[0]
mask = node.operands[1]
if node.operands[0].immediate?
value = node.operands[1]
mask = node.operands[0]
end
tmp = Tmp.new(node.codeOrigin, :gpr)
if value.register? and mask.register?
newList << Instruction.new(node.codeOrigin, "rv_and", [value, mask, tmp])
else
newList << Instruction.new(node.codeOrigin, "rv_li", [mask, tmp]);
newList << Instruction.new(node.codeOrigin, "rv_and", [tmp, value, tmp]);
end
riscv64LowerEmitSignExtension(newList, node, size, tmp, tmp)
newList << Instruction.new(node.codeOrigin, "rv_#{opcode}", [tmp, node.operands[2]])
end
newList = []
list.each {
| node |
if node.is_a? Instruction
case node.opcode
when /^bt(b|i|p|q)(s|z|nz)$/
emit(newList, node, $1.to_sym, branchOpcode($2.to_sym))
when /^t(b|i|p|q)(s|z|nz)$/
emit(newList, node, $1.to_sym, setOpcode($2.to_sym))
else
newList << node
end
else
newList << node
end
}
newList
end
def riscv64LowerCompare(list)
def emit(newList, node, size, comparison)
lhs = riscv64LowerOperandIntoRegisterAndSignExtend(newList, node, node.operands[0], size)
rhs = riscv64LowerOperandIntoRegisterAndSignExtend(newList, node, node.operands[1], size)
dest = node.operands[2]
case comparison
when :eq
tmp = Tmp.new(node.codeOrigin, :gpr)
newList << Instruction.new(node.codeOrigin, "rv_sub", [lhs, rhs, tmp])
newList << Instruction.new(node.codeOrigin, "rv_seqz", [tmp, dest])
when :neq
tmp = Tmp.new(node.codeOrigin, :gpr)
newList << Instruction.new(node.codeOrigin, "rv_sub", [lhs, rhs, tmp])
newList << Instruction.new(node.codeOrigin, "rv_snez", [tmp, dest])
when :a
newList << Instruction.new(node.codeOrigin, "rv_sltu", [rhs, lhs, dest])
when :aeq
newList << Instruction.new(node.codeOrigin, "rv_sltu", [lhs, rhs, dest])
newList << Instruction.new(node.codeOrigin, "rv_xori", [dest, Immediate.new(node.codeOrigin, 1), dest])
when :b