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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
//! Auto-generated module
//!
//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)
use super::functions::*;
use std::collections::HashMap;
/// An instruction together with its optional result SSA value.
#[derive(Debug, Clone, PartialEq)]
pub struct CraneliftInstResult {
/// The result SSA value produced by this instruction (None for void/side-effect-only)
pub result: Option<CraneliftValue>,
/// The instruction
pub instr: CraneliftInstr,
}
impl CraneliftInstResult {
/// Create an instruction with a result.
pub fn with_result(result: CraneliftValue, instr: CraneliftInstr) -> Self {
CraneliftInstResult {
result: Some(result),
instr,
}
}
/// Create an instruction with no result.
pub fn no_result(instr: CraneliftInstr) -> Self {
CraneliftInstResult {
result: None,
instr,
}
}
/// Emit this instruction as a textual IR string.
pub fn emit(&self) -> String {
let instr_str = emit_instr(&self.instr);
if let Some(ref v) = self.result {
format!("{} = {}", v, instr_str)
} else {
instr_str
}
}
}
/// Function signature.
#[derive(Debug, Clone, PartialEq)]
pub struct Signature {
/// Calling convention
pub call_conv: CallConv,
/// Parameter types
pub params: Vec<CraneliftType>,
/// Return types (Cranelift supports multiple returns)
pub returns: Vec<CraneliftType>,
}
impl Signature {
/// Create a new signature.
pub fn new(
call_conv: CallConv,
params: Vec<CraneliftType>,
returns: Vec<CraneliftType>,
) -> Self {
Signature {
call_conv,
params,
returns,
}
}
/// Create a simple C-like signature.
pub fn c_like(params: Vec<CraneliftType>, returns: Vec<CraneliftType>) -> Self {
Signature::new(CallConv::SystemV, params, returns)
}
}
/// Helpers for generating type conversion instructions.
#[allow(dead_code)]
pub struct CraneliftTypeCoerce;
impl CraneliftTypeCoerce {
/// Generate the instruction to narrow `val` from `src_ty` to `dst_ty` (truncation).
#[allow(dead_code)]
pub fn narrow(
src_ty: &CraneliftType,
dst_ty: &CraneliftType,
val: CraneliftValue,
) -> Option<CraneliftInstr> {
match (src_ty, dst_ty) {
(CraneliftType::I64, CraneliftType::I32)
| (CraneliftType::I64, CraneliftType::I16)
| (CraneliftType::I64, CraneliftType::I8)
| (CraneliftType::I32, CraneliftType::I16)
| (CraneliftType::I32, CraneliftType::I8)
| (CraneliftType::I16, CraneliftType::I8) => {
Some(CraneliftInstr::Ireduce(dst_ty.clone(), val))
}
(CraneliftType::F64, CraneliftType::F32) => {
Some(CraneliftInstr::Fdemote(CraneliftType::F32, val))
}
_ => None,
}
}
/// Generate the instruction to widen `val` from `src_ty` to `dst_ty` (extension).
#[allow(dead_code)]
pub fn widen_signed(
_src_ty: &CraneliftType,
dst_ty: &CraneliftType,
val: CraneliftValue,
) -> Option<CraneliftInstr> {
match dst_ty {
CraneliftType::I16 | CraneliftType::I32 | CraneliftType::I64 | CraneliftType::I128 => {
Some(CraneliftInstr::Sextend(dst_ty.clone(), val))
}
CraneliftType::F64 => Some(CraneliftInstr::Fpromote(CraneliftType::F64, val)),
_ => None,
}
}
/// Generate the instruction to zero-extend `val` to `dst_ty`.
#[allow(dead_code)]
pub fn widen_unsigned(
_src_ty: &CraneliftType,
dst_ty: &CraneliftType,
val: CraneliftValue,
) -> Option<CraneliftInstr> {
match dst_ty {
CraneliftType::I16 | CraneliftType::I32 | CraneliftType::I64 | CraneliftType::I128 => {
Some(CraneliftInstr::Uextend(dst_ty.clone(), val))
}
_ => None,
}
}
}
/// How a global value is defined.
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub enum CraneliftGlobalValueDef {
/// A symbol external to the module.
Symbol { name: String, colocated: bool },
/// The value of another global value plus an offset.
IAddImm { base: u32, offset: i64 },
/// Loaded from memory (for GOT entries, etc.).
Load {
base: u32,
offset: i32,
global_type: CraneliftType,
readonly: bool,
},
}
/// Fluent builder for constructing CraneliftModule objects.
#[derive(Debug)]
#[allow(dead_code)]
pub struct CraneliftModuleBuilder {
pub(super) module: CraneliftModule,
}
impl CraneliftModuleBuilder {
/// Start building a new module.
#[allow(dead_code)]
pub fn new(name: impl Into<String>) -> Self {
CraneliftModuleBuilder {
module: CraneliftModule::new(name),
}
}
/// Set the target triple.
#[allow(dead_code)]
pub fn target(mut self, triple: impl Into<String>) -> Self {
self.module.target = triple.into();
self
}
/// Declare an external function.
#[allow(dead_code)]
pub fn extern_func(mut self, name: impl Into<String>, sig: Signature) -> Self {
self.module.add_func_decl(name, sig);
self
}
/// Add a data object.
#[allow(dead_code)]
pub fn data(mut self, obj: CraneliftDataObject) -> Self {
self.module.add_data_object(obj);
self
}
/// Add a function definition.
#[allow(dead_code)]
pub fn func(mut self, f: CraneliftFunction) -> Self {
self.module.add_function(f);
self
}
/// Consume and return the completed module.
#[allow(dead_code)]
pub fn build(self) -> CraneliftModule {
self.module
}
}
/// Fluent builder for constructing CraneliftFunction objects.
#[derive(Debug)]
#[allow(dead_code)]
pub struct CraneliftFunctionBuilder {
pub(super) func: CraneliftFunction,
pub(super) current_block: Option<u32>,
}
impl CraneliftFunctionBuilder {
/// Create a new builder for the named function with given signature.
#[allow(dead_code)]
pub fn new(name: impl Into<String>, sig: Signature) -> Self {
let func = CraneliftFunction::new(name, sig);
CraneliftFunctionBuilder {
func,
current_block: None,
}
}
/// Create an entry block and set it as current.
#[allow(dead_code)]
pub fn create_entry_block(mut self) -> Self {
let b = self.func.new_block();
self.current_block = Some(b);
self
}
/// Create a new block and set it as current.
#[allow(dead_code)]
pub fn create_block(mut self) -> (Self, u32) {
let b = self.func.new_block();
self.current_block = Some(b);
(self, b)
}
/// Switch to an existing block.
#[allow(dead_code)]
pub fn switch_to_block(mut self, block: u32) -> Self {
self.current_block = Some(block);
self
}
/// Emit a value-producing instruction in the current block.
#[allow(dead_code)]
pub fn ins_result(
mut self,
ty: CraneliftType,
instr: CraneliftInstr,
) -> (Self, CraneliftValue) {
let val = self.func.fresh_value(ty);
if let Some(b_idx) = self.current_block {
if let Some(b) = self.func.block_mut(b_idx) {
b.push_with_result(val.clone(), instr);
}
}
(self, val)
}
/// Emit a void (side-effecting) instruction in the current block.
#[allow(dead_code)]
pub fn ins_void(mut self, instr: CraneliftInstr) -> Self {
if let Some(b_idx) = self.current_block {
if let Some(b) = self.func.block_mut(b_idx) {
b.push_void(instr);
}
}
self
}
/// Add a block parameter to the current block.
#[allow(dead_code)]
pub fn block_param(mut self, ty: CraneliftType) -> (Self, CraneliftValue) {
let val = self.func.fresh_value(ty);
if let Some(b_idx) = self.current_block {
if let Some(b) = self.func.block_mut(b_idx) {
b.params.push(val.clone());
}
}
(self, val)
}
/// Consume the builder and return the completed function.
#[allow(dead_code)]
pub fn finish(self) -> CraneliftFunction {
self.func
}
}
/// Cranelift IR type representation.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum CraneliftType {
/// 1-bit boolean / integer
B1,
/// 8-bit integer
I8,
/// 16-bit integer
I16,
/// 32-bit integer
I32,
/// 64-bit integer
I64,
/// 128-bit integer
I128,
/// 32-bit float (IEEE 754 single-precision)
F32,
/// 64-bit float (IEEE 754 double-precision)
F64,
/// 32-bit reference (GC-managed pointer)
R32,
/// 64-bit reference (GC-managed pointer)
R64,
/// SIMD vector: `i32x4`, `f64x2`, etc.
Vector(Box<CraneliftType>, u32),
/// No type (used for void results / side-effecting instructions)
Void,
}
impl CraneliftType {
/// Return the byte width of this type (None for non-scalar or void).
pub fn byte_width(&self) -> Option<u32> {
match self {
CraneliftType::B1 => Some(1),
CraneliftType::I8 => Some(1),
CraneliftType::I16 => Some(2),
CraneliftType::I32 => Some(4),
CraneliftType::I64 => Some(8),
CraneliftType::I128 => Some(16),
CraneliftType::F32 => Some(4),
CraneliftType::F64 => Some(8),
CraneliftType::R32 => Some(4),
CraneliftType::R64 => Some(8),
CraneliftType::Vector(base, lanes) => base.byte_width().map(|w| w * lanes),
CraneliftType::Void => None,
}
}
/// Return true if this is an integer type.
pub fn is_int(&self) -> bool {
matches!(
self,
CraneliftType::I8
| CraneliftType::I16
| CraneliftType::I32
| CraneliftType::I64
| CraneliftType::I128
| CraneliftType::B1
)
}
/// Return true if this is a floating-point type.
pub fn is_float(&self) -> bool {
matches!(self, CraneliftType::F32 | CraneliftType::F64)
}
}
/// A basic block in Cranelift IR.
#[derive(Debug, Clone, PartialEq)]
pub struct CraneliftBlock {
/// Block ID (e.g. block0, block1)
pub id: u32,
/// Block parameters (like phi nodes in SSA form — Cranelift uses explicit params)
pub params: Vec<CraneliftValue>,
/// Instructions in this block
pub instrs: Vec<CraneliftInstResult>,
}
impl CraneliftBlock {
/// Create a new block.
pub fn new(id: u32) -> Self {
CraneliftBlock {
id,
params: vec![],
instrs: vec![],
}
}
/// Create a block with parameters.
pub fn with_params(id: u32, params: Vec<CraneliftValue>) -> Self {
CraneliftBlock {
id,
params,
instrs: vec![],
}
}
/// Return the block reference for this block.
pub fn block_ref(&self) -> BlockRef {
BlockRef::new(self.id)
}
/// Append an instruction with a result value.
pub fn push_with_result(&mut self, result: CraneliftValue, instr: CraneliftInstr) {
self.instrs
.push(CraneliftInstResult::with_result(result, instr));
}
/// Append a void instruction.
pub fn push_void(&mut self, instr: CraneliftInstr) {
self.instrs.push(CraneliftInstResult::no_result(instr));
}
/// Return true if this block ends with a terminator instruction.
pub fn is_terminated(&self) -> bool {
self.instrs.last().is_some_and(|ir| {
matches!(
ir.instr,
CraneliftInstr::Jump(_, _)
| CraneliftInstr::Brif(_, _, _, _, _)
| CraneliftInstr::BrTable(_, _, _)
| CraneliftInstr::Return(_)
| CraneliftInstr::Trap(_)
| CraneliftInstr::Unreachable
| CraneliftInstr::ReturnCall(_, _)
)
})
}
/// Emit this block as textual IR.
pub fn emit(&self) -> String {
let mut s = String::new();
if self.params.is_empty() {
s.push_str(&format!("block{}:\n", self.id));
} else {
let params = self
.params
.iter()
.map(|v| format!("{}: {}", v, v.ty))
.collect::<Vec<_>>()
.join(", ");
s.push_str(&format!("block{}({}):\n", self.id, params));
}
for ir in &self.instrs {
s.push_str(&format!(" {}\n", ir.emit()));
}
s
}
}
/// Helpers for recognizing common Cranelift instruction patterns.
#[allow(dead_code)]
pub struct CraneliftInstPattern;
impl CraneliftInstPattern {
/// Return true if `instr` is a pure arithmetic (no side effects, no memory) instruction.
#[allow(dead_code)]
pub fn is_pure_arith(instr: &CraneliftInstr) -> bool {
matches!(
instr,
CraneliftInstr::Iconst(..)
| CraneliftInstr::F32Const(..)
| CraneliftInstr::F64Const(..)
| CraneliftInstr::Iadd(..)
| CraneliftInstr::Isub(..)
| CraneliftInstr::Imul(..)
| CraneliftInstr::Sdiv(..)
| CraneliftInstr::Udiv(..)
| CraneliftInstr::Srem(..)
| CraneliftInstr::Urem(..)
| CraneliftInstr::Ineg(..)
| CraneliftInstr::Iabs(..)
| CraneliftInstr::IaddImm(..)
| CraneliftInstr::ImulImm(..)
| CraneliftInstr::Band(..)
| CraneliftInstr::Bor(..)
| CraneliftInstr::Bxor(..)
| CraneliftInstr::Bnot(..)
| CraneliftInstr::Ishl(..)
| CraneliftInstr::Sshr(..)
| CraneliftInstr::Ushr(..)
| CraneliftInstr::Rotl(..)
| CraneliftInstr::Rotr(..)
| CraneliftInstr::Clz(..)
| CraneliftInstr::Ctz(..)
| CraneliftInstr::Popcnt(..)
| CraneliftInstr::Fadd(..)
| CraneliftInstr::Fsub(..)
| CraneliftInstr::Fmul(..)
| CraneliftInstr::Fdiv(..)
| CraneliftInstr::Fneg(..)
| CraneliftInstr::Fabs(..)
| CraneliftInstr::Sqrt(..)
| CraneliftInstr::Ceil(..)
| CraneliftInstr::Floor(..)
| CraneliftInstr::FTrunc(..)
| CraneliftInstr::Nearest(..)
| CraneliftInstr::Fmin(..)
| CraneliftInstr::Fmax(..)
| CraneliftInstr::Icmp(..)
| CraneliftInstr::Fcmp(..)
)
}
/// Return true if `instr` is a terminator (ends a block).
#[allow(dead_code)]
pub fn is_terminator(instr: &CraneliftInstr) -> bool {
matches!(
instr,
CraneliftInstr::Return(..)
| CraneliftInstr::Jump(..)
| CraneliftInstr::Brif(..)
| CraneliftInstr::BrTable(..)
| CraneliftInstr::Trap(..)
)
}
/// Return true if `instr` has side effects (memory or control flow).
#[allow(dead_code)]
pub fn has_side_effects(instr: &CraneliftInstr) -> bool {
matches!(
instr,
CraneliftInstr::Store(..)
| CraneliftInstr::Return(..)
| CraneliftInstr::Jump(..)
| CraneliftInstr::Brif(..)
| CraneliftInstr::BrTable(..)
| CraneliftInstr::Trap(..)
| CraneliftInstr::Call(..)
)
}
/// Try to extract the constant value from an `Iconst` instruction.
#[allow(dead_code)]
pub fn iconst_value(instr: &CraneliftInstr) -> Option<i64> {
match instr {
CraneliftInstr::Iconst(_, n) => Some(*n),
_ => None,
}
}
}
/// Cranelift IR code generation backend.
pub struct CraneliftBackend {
/// The module being built
pub module: CraneliftModule,
/// Current function being constructed (if any)
pub(super) current_func: Option<CraneliftFunction>,
/// Current block id being populated (if any)
pub(super) current_block: Option<u32>,
}
impl CraneliftBackend {
/// Create a new Cranelift backend.
pub fn new(module_name: impl Into<String>) -> Self {
CraneliftBackend {
module: CraneliftModule::new(module_name),
current_func: None,
current_block: None,
}
}
/// Start building a new function.
pub fn begin_function(&mut self, name: impl Into<String>, sig: Signature) {
let mut func = CraneliftFunction::new(name, sig);
let entry_id = func.new_block();
for ty in func.sig.params.clone() {
let v = func.fresh_value(ty.clone());
if let Some(block) = func.blocks.iter_mut().find(|b| b.id == entry_id) {
block.params.push(v);
}
}
self.current_func = Some(func);
self.current_block = Some(entry_id);
}
/// End the current function and add it to the module.
pub fn end_function(&mut self) {
if let Some(func) = self.current_func.take() {
self.module.add_function(func);
}
self.current_block = None;
}
/// Switch to an existing block.
pub fn switch_to_block(&mut self, block_id: u32) {
self.current_block = Some(block_id);
}
/// Allocate a fresh SSA value in the current function.
pub fn fresh_value(&mut self, ty: CraneliftType) -> Option<CraneliftValue> {
self.current_func.as_mut().map(|f| f.fresh_value(ty))
}
/// Create a new block in the current function.
pub fn new_block(&mut self) -> Option<u32> {
self.current_func.as_mut().map(|f| f.new_block())
}
/// Emit an instruction with a result into the current block.
pub fn emit_with_result(
&mut self,
ty: CraneliftType,
instr: CraneliftInstr,
) -> Option<CraneliftValue> {
let v = self.fresh_value(ty)?;
let block_id = self.current_block?;
let func = self.current_func.as_mut()?;
if let Some(block) = func.block_mut(block_id) {
block.push_with_result(v.clone(), instr);
}
Some(v)
}
/// Emit a void instruction into the current block.
pub fn emit_void(&mut self, instr: CraneliftInstr) {
let block_id = match self.current_block {
Some(id) => id,
None => return,
};
if let Some(func) = self.current_func.as_mut() {
if let Some(block) = func.block_mut(block_id) {
block.push_void(instr);
}
}
}
/// Emit an `iconst` instruction.
pub fn iconst(&mut self, ty: CraneliftType, val: i64) -> Option<CraneliftValue> {
self.emit_with_result(ty.clone(), CraneliftInstr::Iconst(ty, val))
}
/// Emit an `iadd` instruction.
pub fn iadd(&mut self, a: CraneliftValue, b: CraneliftValue) -> Option<CraneliftValue> {
let ty = a.ty.clone();
self.emit_with_result(ty, CraneliftInstr::Iadd(a, b))
}
/// Emit an `isub` instruction.
pub fn isub(&mut self, a: CraneliftValue, b: CraneliftValue) -> Option<CraneliftValue> {
let ty = a.ty.clone();
self.emit_with_result(ty, CraneliftInstr::Isub(a, b))
}
/// Emit an `imul` instruction.
pub fn imul(&mut self, a: CraneliftValue, b: CraneliftValue) -> Option<CraneliftValue> {
let ty = a.ty.clone();
self.emit_with_result(ty, CraneliftInstr::Imul(a, b))
}
/// Emit an `sdiv` instruction.
pub fn sdiv(&mut self, a: CraneliftValue, b: CraneliftValue) -> Option<CraneliftValue> {
let ty = a.ty.clone();
self.emit_with_result(ty, CraneliftInstr::Sdiv(a, b))
}
/// Emit an `icmp` instruction.
pub fn icmp(
&mut self,
cc: IntCC,
a: CraneliftValue,
b: CraneliftValue,
) -> Option<CraneliftValue> {
self.emit_with_result(CraneliftType::B1, CraneliftInstr::Icmp(cc, a, b))
}
/// Emit an `fcmp` instruction.
pub fn fcmp(
&mut self,
cc: FloatCC,
a: CraneliftValue,
b: CraneliftValue,
) -> Option<CraneliftValue> {
self.emit_with_result(CraneliftType::B1, CraneliftInstr::Fcmp(cc, a, b))
}
/// Emit a `load` instruction.
pub fn load(
&mut self,
ty: CraneliftType,
flags: MemFlags,
addr: CraneliftValue,
offset: i32,
) -> Option<CraneliftValue> {
self.emit_with_result(ty.clone(), CraneliftInstr::Load(ty, flags, addr, offset))
}
/// Emit a `store` instruction.
pub fn store(
&mut self,
flags: MemFlags,
val: CraneliftValue,
addr: CraneliftValue,
offset: i32,
) {
self.emit_void(CraneliftInstr::Store(flags, val, addr, offset));
}
/// Emit a `call` instruction returning a single value.
pub fn call_single(
&mut self,
ret_ty: CraneliftType,
func: impl Into<String>,
args: Vec<CraneliftValue>,
) -> Option<CraneliftValue> {
self.emit_with_result(ret_ty, CraneliftInstr::Call(func.into(), args))
}
/// Emit a `jump` terminator.
pub fn jump(&mut self, target: BlockRef, args: Vec<CraneliftValue>) {
self.emit_void(CraneliftInstr::Jump(target, args));
}
/// Emit a `brif` terminator.
pub fn brif(
&mut self,
cond: CraneliftValue,
t: BlockRef,
t_args: Vec<CraneliftValue>,
f: BlockRef,
f_args: Vec<CraneliftValue>,
) {
self.emit_void(CraneliftInstr::Brif(cond, t, t_args, f, f_args));
}
/// Emit a `return` terminator.
pub fn emit_return(&mut self, vals: Vec<CraneliftValue>) {
self.emit_void(CraneliftInstr::Return(vals));
}
/// Emit the full module as textual IR.
pub fn emit_module(&self) -> String {
self.module.emit()
}
}
/// Stack slot allocator for a function.
#[derive(Debug, Default)]
#[allow(dead_code)]
pub struct CraneliftStackAllocator {
pub(super) slots: Vec<CraneliftStackSlot>,
pub(super) next_id: u32,
pub(super) frame_size: u32,
}
impl CraneliftStackAllocator {
/// Create a new stack allocator.
#[allow(dead_code)]
pub fn new() -> Self {
CraneliftStackAllocator::default()
}
/// Allocate a new stack slot with the given size and alignment.
#[allow(dead_code)]
pub fn alloc(&mut self, size: u32, align: u32) -> &CraneliftStackSlot {
let aligned = (self.frame_size + align - 1) & !(align - 1);
self.frame_size = aligned + size;
let slot = CraneliftStackSlot::new(self.next_id, size, align);
self.next_id += 1;
self.slots.push(slot);
self.slots
.last()
.expect("slots is non-empty after push; invariant guaranteed by alloc")
}
/// Allocate a named stack slot.
#[allow(dead_code)]
pub fn alloc_named(
&mut self,
size: u32,
align: u32,
name: impl Into<String>,
) -> &CraneliftStackSlot {
let aligned = (self.frame_size + align - 1) & !(align - 1);
self.frame_size = aligned + size;
let slot = CraneliftStackSlot::named(self.next_id, size, align, name);
self.next_id += 1;
self.slots.push(slot);
self.slots
.last()
.expect("slots is non-empty after push; invariant guaranteed by alloc_named")
}
/// Return all allocated slots.
#[allow(dead_code)]
pub fn slots(&self) -> &[CraneliftStackSlot] {
&self.slots
}
/// Return the total frame size.
#[allow(dead_code)]
pub fn frame_size(&self) -> u32 {
self.frame_size
}
/// Emit all slot declarations.
#[allow(dead_code)]
pub fn emit_decls(&self) -> String {
self.slots
.iter()
.map(|s| s.emit())
.collect::<Vec<_>>()
.join("\n")
}
}
/// A data object in the Cranelift module (global variable / constant).
#[derive(Debug, Clone, PartialEq)]
pub struct CraneliftDataObject {
/// Symbol name
pub name: String,
/// Whether the data is read-only
pub is_readonly: bool,
/// Data contents (raw bytes)
pub data: Vec<u8>,
/// Alignment in bytes
pub align: u32,
}
impl CraneliftDataObject {
/// Create a new read-only data object.
pub fn readonly(name: impl Into<String>, data: Vec<u8>, align: u32) -> Self {
CraneliftDataObject {
name: name.into(),
is_readonly: true,
data,
align,
}
}
/// Create a writable data object.
pub fn writable(name: impl Into<String>, data: Vec<u8>, align: u32) -> Self {
CraneliftDataObject {
name: name.into(),
is_readonly: false,
data,
align,
}
}
/// Emit this data object as textual IR.
pub fn emit(&self) -> String {
let rw = if self.is_readonly { "rodata" } else { "data" };
let hex: String = self.data.iter().map(|b| format!("\\{:02x}", b)).collect();
format!(
"{} %{} align={} {{\n ascii \"{}\"\n}}\n",
rw, self.name, self.align, hex
)
}
}
/// ABI parameter classification.
#[derive(Debug, Clone, PartialEq, Eq)]
#[allow(dead_code)]
pub enum CraneliftABIClass {
/// Passed in integer register.
Integer,
/// Passed in floating-point register.
Float,
/// Passed in SSE register (packed integers).
SSE,
/// Passed on the stack.
Memory,
/// Not passed (zero-size or erased).
NoValue,
}
/// ABI layout for a function parameter or return value.
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct CraneliftABIParam {
/// The IR type.
pub ty: CraneliftType,
/// How this parameter is classified.
pub class: CraneliftABIClass,
/// Stack offset (if passed on stack), or register index.
pub location: i32,
}
impl CraneliftABIParam {
/// Create a new ABI parameter.
#[allow(dead_code)]
pub fn new(ty: CraneliftType, class: CraneliftABIClass, location: i32) -> Self {
CraneliftABIParam {
ty,
class,
location,
}
}
/// Return true if this parameter is register-allocated.
#[allow(dead_code)]
pub fn is_register(&self) -> bool {
!matches!(
self.class,
CraneliftABIClass::Memory | CraneliftABIClass::NoValue
)
}
}
/// Which optimization passes to run on Cranelift IR.
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct CraneliftPassConfig {
/// Enable constant folding.
pub const_folding: bool,
/// Enable dead code elimination.
pub dce: bool,
/// Enable instruction combining (e.g., add + mul → madd).
pub inst_combine: bool,
/// Enable branch optimization.
pub branch_opt: bool,
/// Enable loop-invariant code motion.
pub licm: bool,
/// Enable register coalescing.
pub reg_coalescing: bool,
/// Enable redundant load elimination.
pub load_elim: bool,
/// Enable tail call optimization.
pub tail_call_opt: bool,
/// Maximum inlining depth.
pub inline_depth: u32,
/// Whether to emit debug information.
pub debug_info: bool,
}
impl CraneliftPassConfig {
/// Return a no-optimization configuration.
#[allow(dead_code)]
pub fn no_opt() -> Self {
CraneliftPassConfig {
const_folding: false,
dce: false,
inst_combine: false,
branch_opt: false,
licm: false,
reg_coalescing: false,
load_elim: false,
tail_call_opt: false,
inline_depth: 0,
debug_info: false,
}
}
/// Return a maximum optimization configuration.
#[allow(dead_code)]
pub fn max_opt() -> Self {
CraneliftPassConfig {
const_folding: true,
dce: true,
inst_combine: true,
branch_opt: true,
licm: true,
reg_coalescing: true,
load_elim: true,
tail_call_opt: true,
inline_depth: 10,
debug_info: false,
}
}
}
/// A block reference — corresponds to `block0`, `block1`, etc.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct BlockRef {
/// Numeric ID for this block
pub id: u32,
}
impl BlockRef {
/// Create a new block reference.
pub fn new(id: u32) -> Self {
BlockRef { id }
}
}
/// Memory access flags for load/store instructions.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MemFlags {
/// True if the access is aligned (UB if not)
pub aligned: bool,
/// True if the access is volatile (not reordered)
pub notrap: bool,
/// True if the access does not alias any other access
pub readonly: bool,
}
impl MemFlags {
/// Create default (safe) memory flags.
pub fn new() -> Self {
MemFlags {
aligned: false,
notrap: false,
readonly: false,
}
}
/// Create trusted (notrap) memory flags.
pub fn trusted() -> Self {
MemFlags {
aligned: false,
notrap: true,
readonly: false,
}
}
/// Create aligned, notrap flags.
pub fn aligned_notrap() -> Self {
MemFlags {
aligned: true,
notrap: true,
readonly: false,
}
}
}
/// Known calling conventions for Cranelift.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[allow(dead_code)]
pub enum CraneliftCallingConvention {
/// System V AMD64 ABI (Linux, macOS x86-64).
SystemV,
/// Windows x64 calling convention.
WindowsFastcall,
/// Wasmtime calling convention (WebAssembly).
WasmtimeSystem,
/// Cold (rarely-called) function convention.
Cold,
/// Tail-call optimized convention.
Tail,
/// Fast (internal) convention (no caller-saves).
Fast,
}
/// Debug information attached to a Cranelift function.
#[derive(Debug, Clone, Default)]
#[allow(dead_code)]
pub struct CraneliftDebugInfo {
/// Source file name.
pub source_file: Option<String>,
/// Source function name (may differ from IR name).
pub source_function: Option<String>,
/// Mapping from instruction offset to (line, column) in source.
pub location_map: Vec<(usize, u32, u32)>,
/// Local variable names: (ir_value, source_name).
pub var_names: HashMap<String, String>,
}
impl CraneliftDebugInfo {
/// Create a new debug info record.
#[allow(dead_code)]
pub fn new(source_file: impl Into<String>, source_function: impl Into<String>) -> Self {
CraneliftDebugInfo {
source_file: Some(source_file.into()),
source_function: Some(source_function.into()),
location_map: Vec::new(),
var_names: HashMap::new(),
}
}
/// Record a source location for an instruction.
#[allow(dead_code)]
pub fn add_location(&mut self, offset: usize, line: u32, column: u32) {
self.location_map.push((offset, line, column));
}
/// Record a variable name mapping.
#[allow(dead_code)]
pub fn add_var_name(&mut self, ir_name: impl Into<String>, source_name: impl Into<String>) {
self.var_names.insert(ir_name.into(), source_name.into());
}
/// Emit debug info as comments.
#[allow(dead_code)]
pub fn emit_comments(&self) -> String {
let mut out = String::new();
if let Some(ref f) = self.source_file {
out.push_str(&format!("; source_file: {}\n", f));
}
if let Some(ref f) = self.source_function {
out.push_str(&format!("; source_function: {}\n", f));
}
for (offset, line, col) in &self.location_map {
out.push_str(&format!("; @{}: {}:{}\n", offset, line, col));
}
for (ir, src) in &self.var_names {
out.push_str(&format!("; var {} => {}\n", ir, src));
}
out
}
}
/// A Cranelift global value declaration.
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct CraneliftGlobalValue {
/// Global value ID.
pub id: u32,
/// The global value type.
pub ty: CraneliftType,
/// How the value is obtained.
pub def: CraneliftGlobalValueDef,
}
impl CraneliftGlobalValue {
/// Create a symbol global value.
#[allow(dead_code)]
pub fn symbol(id: u32, name: impl Into<String>, colocated: bool) -> Self {
CraneliftGlobalValue {
id,
ty: CraneliftType::I64,
def: CraneliftGlobalValueDef::Symbol {
name: name.into(),
colocated,
},
}
}
/// Emit the global value declaration.
#[allow(dead_code)]
pub fn emit(&self) -> String {
match &self.def {
CraneliftGlobalValueDef::Symbol { name, colocated } => {
format!(
"gv{} = symbol {}{}",
self.id,
if *colocated { "colocated " } else { "" },
name
)
}
CraneliftGlobalValueDef::IAddImm { base, offset } => {
format!("gv{} = iadd_imm gv{}, {}", self.id, base, offset)
}
CraneliftGlobalValueDef::Load {
base,
offset,
global_type,
readonly,
} => {
format!(
"gv{} = load.{} {}[{}]{}",
self.id,
global_type,
base,
offset,
if *readonly { " readonly" } else { "" }
)
}
}
}
}
/// WebAssembly-style heap (linear memory) configuration for Cranelift.
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct CraneliftHeapConfig {
/// Base global value (pointer to start of heap).
pub base: u32,
/// Minimum heap size in bytes.
pub min_size: u64,
/// Maximum heap size (None = unlimited).
pub max_size: Option<u64>,
/// Page size (typically 65536 for Wasm).
pub page_size: u64,
/// Whether bounds checking is needed.
pub needs_bounds_check: bool,
/// Guard size in bytes (0 = no guard pages).
pub guard_size: u64,
}
impl CraneliftHeapConfig {
/// Create a 4 GiB static heap (no guard needed in 64-bit mode).
#[allow(dead_code)]
pub fn static_4gib() -> Self {
CraneliftHeapConfig {
min_size: 65536,
max_size: Some(4 * 1024 * 1024 * 1024),
guard_size: 2 * 1024 * 1024 * 1024,
needs_bounds_check: false,
..Default::default()
}
}
/// Emit a comment describing the heap configuration.
#[allow(dead_code)]
pub fn emit_comment(&self) -> String {
format!(
"; heap base=gv{} min={} max={} page={} guard={} bounds_check={}",
self.base,
self.min_size,
self.max_size
.map(|m| m.to_string())
.unwrap_or_else(|| "unlimited".to_string()),
self.page_size,
self.guard_size,
self.needs_bounds_check,
)
}
}
/// A stack slot in a Cranelift function frame.
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct CraneliftStackSlot {
/// Slot identifier.
pub id: u32,
/// Size in bytes.
pub size: u32,
/// Alignment in bytes.
pub align: u32,
/// Optional debug name.
pub name: Option<String>,
}
impl CraneliftStackSlot {
/// Create a new stack slot.
#[allow(dead_code)]
pub fn new(id: u32, size: u32, align: u32) -> Self {
CraneliftStackSlot {
id,
size,
align,
name: None,
}
}
/// Create a named stack slot.
#[allow(dead_code)]
pub fn named(id: u32, size: u32, align: u32, name: impl Into<String>) -> Self {
CraneliftStackSlot {
id,
size,
align,
name: Some(name.into()),
}
}
/// Emit the stack slot declaration.
#[allow(dead_code)]
pub fn emit(&self) -> String {
let name_comment = self
.name
.as_ref()
.map(|n| format!(" ; {}", n))
.unwrap_or_default();
format!(
"ss{} = explicit_slot {}, align = {}{}",
self.id, self.size, self.align, name_comment
)
}
/// Generate a `stack_addr` expression for this slot at the given offset.
#[allow(dead_code)]
pub fn addr_expr(&self, offset: i32) -> String {
format!("stack_addr.i64 ss{}, {}", self.id, offset)
}
}
/// A Cranelift IR instruction.
#[derive(Debug, Clone, PartialEq)]
pub enum CraneliftInstr {
/// Integer constant: `iconst.i64 42`
Iconst(CraneliftType, i64),
/// Boolean constant: `bconst.b1 true`
Bconst(bool),
/// 32-bit float constant: `f32const 3.14`
F32Const(f32),
/// 64-bit float constant: `f64const 3.14`
F64Const(f64),
/// Add: `iadd v1, v2`
Iadd(CraneliftValue, CraneliftValue),
/// Subtract: `isub v1, v2`
Isub(CraneliftValue, CraneliftValue),
/// Multiply: `imul v1, v2`
Imul(CraneliftValue, CraneliftValue),
/// Signed divide: `sdiv v1, v2`
Sdiv(CraneliftValue, CraneliftValue),
/// Unsigned divide: `udiv v1, v2`
Udiv(CraneliftValue, CraneliftValue),
/// Signed remainder: `srem v1, v2`
Srem(CraneliftValue, CraneliftValue),
/// Unsigned remainder: `urem v1, v2`
Urem(CraneliftValue, CraneliftValue),
/// Negate: `ineg v1`
Ineg(CraneliftValue),
/// Absolute value: `iabs v1`
Iabs(CraneliftValue),
/// Add with immediate: `iadd_imm v1, 5`
IaddImm(CraneliftValue, i64),
/// Multiply with immediate: `imul_imm v1, 5`
ImulImm(CraneliftValue, i64),
/// Bitwise AND: `band v1, v2`
Band(CraneliftValue, CraneliftValue),
/// Bitwise OR: `bor v1, v2`
Bor(CraneliftValue, CraneliftValue),
/// Bitwise XOR: `bxor v1, v2`
Bxor(CraneliftValue, CraneliftValue),
/// Bitwise NOT: `bnot v1`
Bnot(CraneliftValue),
/// Shift left: `ishl v1, v2`
Ishl(CraneliftValue, CraneliftValue),
/// Arithmetic shift right: `sshr v1, v2`
Sshr(CraneliftValue, CraneliftValue),
/// Logical shift right: `ushr v1, v2`
Ushr(CraneliftValue, CraneliftValue),
/// Rotate left: `rotl v1, v2`
Rotl(CraneliftValue, CraneliftValue),
/// Rotate right: `rotr v1, v2`
Rotr(CraneliftValue, CraneliftValue),
/// Count leading zeros: `clz v1`
Clz(CraneliftValue),
/// Count trailing zeros: `ctz v1`
Ctz(CraneliftValue),
/// Population count: `popcnt v1`
Popcnt(CraneliftValue),
/// Float add: `fadd v1, v2`
Fadd(CraneliftValue, CraneliftValue),
/// Float subtract: `fsub v1, v2`
Fsub(CraneliftValue, CraneliftValue),
/// Float multiply: `fmul v1, v2`
Fmul(CraneliftValue, CraneliftValue),
/// Float divide: `fdiv v1, v2`
Fdiv(CraneliftValue, CraneliftValue),
/// Float negate: `fneg v1`
Fneg(CraneliftValue),
/// Float absolute value: `fabs v1`
Fabs(CraneliftValue),
/// Float square root: `sqrt v1`
Sqrt(CraneliftValue),
/// Fused multiply-add: `fma v1, v2, v3`
Fma(CraneliftValue, CraneliftValue, CraneliftValue),
/// Float minimum: `fmin v1, v2`
Fmin(CraneliftValue, CraneliftValue),
/// Float maximum: `fmax v1, v2`
Fmax(CraneliftValue, CraneliftValue),
/// Float floor: `floor v1`
Floor(CraneliftValue),
/// Float ceiling: `ceil v1`
Ceil(CraneliftValue),
/// Float truncate toward zero: `trunc v1`
FTrunc(CraneliftValue),
/// Float round to nearest: `nearest v1`
Nearest(CraneliftValue),
/// Integer compare: `icmp eq v1, v2`
Icmp(IntCC, CraneliftValue, CraneliftValue),
/// Float compare: `fcmp lt v1, v2`
Fcmp(FloatCC, CraneliftValue, CraneliftValue),
/// Select: `select v_cond, v_true, v_false`
Select(CraneliftValue, CraneliftValue, CraneliftValue),
/// Sign-extend: `sextend.i64 v1`
Sextend(CraneliftType, CraneliftValue),
/// Zero-extend: `uextend.i64 v1`
Uextend(CraneliftType, CraneliftValue),
/// Truncate: `ireduce.i32 v1`
Ireduce(CraneliftType, CraneliftValue),
/// Float convert: `fpromote.f64 v1`
Fpromote(CraneliftType, CraneliftValue),
/// Float demote: `fdemote.f32 v1`
Fdemote(CraneliftType, CraneliftValue),
/// Float to int (trunc): `fcvt_to_sint.i64 v1`
FcvtToSint(CraneliftType, CraneliftValue),
/// Float to unsigned int (trunc): `fcvt_to_uint.i64 v1`
FcvtToUint(CraneliftType, CraneliftValue),
/// Signed int to float: `fcvt_from_sint.f64 v1`
FcvtFromSint(CraneliftType, CraneliftValue),
/// Unsigned int to float: `fcvt_from_uint.f64 v1`
FcvtFromUint(CraneliftType, CraneliftValue),
/// Bitcast: `bitcast.f64 v1`
Bitcast(CraneliftType, CraneliftValue),
/// Load: `load.i64 notrap aligned v_addr+offset`
Load(CraneliftType, MemFlags, CraneliftValue, i32),
/// Store: `store notrap aligned v_val, v_addr+offset`
Store(MemFlags, CraneliftValue, CraneliftValue, i32),
/// Stack slot address: `stack_addr.i64 ss0`
StackAddr(CraneliftType, u32),
/// Global value address: `global_value.i64 gv0`
GlobalValue(CraneliftType, u32),
/// Unconditional jump: `jump block1(v1, v2)`
Jump(BlockRef, Vec<CraneliftValue>),
/// Conditional branch: `brif v_cond, block1(args), block2(args)`
Brif(
CraneliftValue,
BlockRef,
Vec<CraneliftValue>,
BlockRef,
Vec<CraneliftValue>,
),
/// Branch table (indirect jump): `br_table v, block_default, jt0`
BrTable(CraneliftValue, BlockRef, Vec<BlockRef>),
/// Return: `return v1, v2`
Return(Vec<CraneliftValue>),
/// Trap: `trap user1`
Trap(String),
/// Trap if condition: `trapif eq v1, user1`
Trapif(IntCC, CraneliftValue, String),
/// Unreachable trap
Unreachable,
/// Direct call: `call func(args)`
Call(String, Vec<CraneliftValue>),
/// Indirect call: `call_indirect sig0, v_callee(args)`
CallIndirect(u32, CraneliftValue, Vec<CraneliftValue>),
/// Return call (tail call): `return_call func(args)`
ReturnCall(String, Vec<CraneliftValue>),
/// Function argument: `func_addr.i64 func_name`
FuncAddr(CraneliftType, String),
/// Null reference constant: `null.r64`
Null(CraneliftType),
/// Vector splat: `splat.i32x4 v1`
Splat(CraneliftType, CraneliftValue),
/// Vector extract lane: `extractlane v1, 0`
ExtractLane(CraneliftValue, u8),
/// Vector insert lane: `insertlane v1, 0, v2`
InsertLane(CraneliftValue, u8, CraneliftValue),
/// Copy / identity: `copy v1` (used for renames)
Copy(CraneliftValue),
/// Nop
Nop,
}
/// Float comparison condition codes for `fcmp`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FloatCC {
/// Ordered and equal
Equal,
/// Ordered and not equal
NotEqual,
/// Ordered and less than
LessThan,
/// Ordered and less than or equal
LessThanOrEqual,
/// Ordered and greater than
GreaterThan,
/// Ordered and greater than or equal
GreaterThanOrEqual,
/// Ordered (neither is NaN)
Ordered,
/// Unordered (at least one is NaN)
Unordered,
/// Unordered or equal
UnorderedOrEqual,
/// Unordered or less than
UnorderedOrLessThan,
/// Unordered or greater than
UnorderedOrGreaterThan,
}
/// Integer comparison condition codes for `icmp`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum IntCC {
/// Equal (`==`)
Equal,
/// Not equal (`!=`)
NotEqual,
/// Signed less than (`<`)
SignedLessThan,
/// Signed less than or equal (`<=`)
SignedLessThanOrEqual,
/// Signed greater than (`>`)
SignedGreaterThan,
/// Signed greater than or equal (`>=`)
SignedGreaterThanOrEqual,
/// Unsigned less than
UnsignedLessThan,
/// Unsigned less than or equal
UnsignedLessThanOrEqual,
/// Unsigned greater than
UnsignedGreaterThan,
/// Unsigned greater than or equal
UnsignedGreaterThanOrEqual,
/// Overflow: signed add overflows
Overflow,
/// No overflow
NotOverflow,
}
/// An SSA value reference in Cranelift IR — corresponds to `v0`, `v1`, etc.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CraneliftValue {
/// Numeric ID for this SSA value
pub id: u32,
/// The type of this value
pub ty: CraneliftType,
}
impl CraneliftValue {
/// Create a new SSA value.
pub fn new(id: u32, ty: CraneliftType) -> Self {
CraneliftValue { id, ty }
}
}
/// Calling convention.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CallConv {
/// Fast calling convention (Cranelift internal)
Fast,
/// Cold calling convention (infrequently called)
Cold,
/// System V AMD64 ABI
SystemV,
/// Windows x64 ABI
WindowsFastcall,
/// WebAssembly calling convention
WasmtimeSystemV,
}
/// Static metrics about a Cranelift function.
#[derive(Debug, Clone, Default)]
#[allow(dead_code)]
pub struct CraneliftCodeMetrics {
/// Number of basic blocks.
pub num_blocks: usize,
/// Total number of instructions.
pub total_instructions: usize,
/// Number of value-producing instructions.
pub num_value_instructions: usize,
/// Number of void (side-effecting) instructions.
pub num_void_instructions: usize,
/// Number of block parameters across all blocks.
pub total_block_params: usize,
/// Number of call instructions.
pub num_calls: usize,
/// Number of load instructions.
pub num_loads: usize,
/// Number of store instructions.
pub num_stores: usize,
/// Number of branch instructions.
pub num_branches: usize,
}
impl CraneliftCodeMetrics {
/// Compute metrics for a function.
#[allow(dead_code)]
pub fn compute(func: &CraneliftFunction) -> Self {
let mut m = CraneliftCodeMetrics {
num_blocks: func.blocks.len(),
..Default::default()
};
for block in &func.blocks {
m.total_block_params += block.params.len();
for inst in &block.instrs {
m.total_instructions += 1;
if inst.result.is_some() {
m.num_value_instructions += 1;
} else {
m.num_void_instructions += 1;
}
match &inst.instr {
CraneliftInstr::Call(..) => {
m.num_calls += 1;
}
CraneliftInstr::Load(..) => {
m.num_loads += 1;
}
CraneliftInstr::Store(..) => {
m.num_stores += 1;
}
CraneliftInstr::Brif(..)
| CraneliftInstr::BrTable(..)
| CraneliftInstr::Jump(..) => {
m.num_branches += 1;
}
_ => {}
}
}
}
m
}
/// Return a summary string.
#[allow(dead_code)]
pub fn summary(&self) -> String {
format!(
"blocks={} total_instrs={} values={} voids={} params={} calls={} loads={} stores={} branches={}",
self.num_blocks, self.total_instructions, self.num_value_instructions, self
.num_void_instructions, self.total_block_params, self.num_calls, self
.num_loads, self.num_stores, self.num_branches,
)
}
}
/// An inline assembly fragment that can be embedded in a function.
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct CraneliftInlineAsm {
/// The assembly template string.
pub template: String,
/// Input operands: (constraint, value).
pub inputs: Vec<(String, CraneliftValue)>,
/// Output operands: (constraint, result_value).
pub outputs: Vec<(String, CraneliftValue)>,
/// Whether the assembly has side effects (prevents reordering).
pub volatile: bool,
/// Whether the assembly can trap.
pub can_trap: bool,
}
impl CraneliftInlineAsm {
/// Create a new inline assembly fragment.
#[allow(dead_code)]
pub fn new(template: impl Into<String>) -> Self {
CraneliftInlineAsm {
template: template.into(),
inputs: Vec::new(),
outputs: Vec::new(),
volatile: true,
can_trap: false,
}
}
/// Add an input operand.
#[allow(dead_code)]
pub fn add_input(&mut self, constraint: impl Into<String>, val: CraneliftValue) {
self.inputs.push((constraint.into(), val));
}
/// Add an output operand.
#[allow(dead_code)]
pub fn add_output(&mut self, constraint: impl Into<String>, val: CraneliftValue) {
self.outputs.push((constraint.into(), val));
}
/// Emit a comment describing this inline asm fragment.
#[allow(dead_code)]
pub fn emit_comment(&self) -> String {
format!(
"; inline_asm template={:?} inputs={} outputs={} volatile={}",
self.template,
self.inputs.len(),
self.outputs.len(),
self.volatile,
)
}
}
/// A Cranelift function definition.
#[derive(Debug, Clone, PartialEq)]
pub struct CraneliftFunction {
/// Function name
pub name: String,
/// Function signature
pub sig: Signature,
/// Stack slots: (slot_id, size_bytes)
pub stack_slots: Vec<(u32, u32)>,
/// Global values referenced: (gv_id, name)
pub global_values: Vec<(u32, String)>,
/// Signature references for indirect calls: (sig_id, signature)
pub sig_refs: Vec<(u32, Signature)>,
/// Function references for direct calls: (fn_ref_id, name, sig_id)
pub func_refs: Vec<(u32, String, u32)>,
/// Basic blocks (first block is the entry block)
pub blocks: Vec<CraneliftBlock>,
/// Next available SSA value id
pub(super) next_value: u32,
/// Next available block id
pub(super) next_block: u32,
}
impl CraneliftFunction {
/// Create a new function.
pub fn new(name: impl Into<String>, sig: Signature) -> Self {
CraneliftFunction {
name: name.into(),
sig,
stack_slots: vec![],
global_values: vec![],
sig_refs: vec![],
func_refs: vec![],
blocks: vec![],
next_value: 0,
next_block: 0,
}
}
/// Allocate a fresh SSA value with the given type.
pub fn fresh_value(&mut self, ty: CraneliftType) -> CraneliftValue {
let v = CraneliftValue::new(self.next_value, ty);
self.next_value += 1;
v
}
/// Create a new block and return its block reference.
pub fn new_block(&mut self) -> u32 {
let id = self.next_block;
self.next_block += 1;
self.blocks.push(CraneliftBlock::new(id));
id
}
/// Create a new block with parameters.
pub fn new_block_with_params(&mut self, param_types: &[CraneliftType]) -> u32 {
let id = self.next_block;
self.next_block += 1;
let params = param_types
.iter()
.map(|ty| {
let v = CraneliftValue::new(self.next_value, ty.clone());
self.next_value += 1;
v
})
.collect();
self.blocks.push(CraneliftBlock::with_params(id, params));
id
}
/// Get a mutable reference to a block by id.
pub fn block_mut(&mut self, id: u32) -> Option<&mut CraneliftBlock> {
self.blocks.iter_mut().find(|b| b.id == id)
}
/// Add a stack slot.
pub fn add_stack_slot(&mut self, size_bytes: u32) -> u32 {
let id = self.stack_slots.len() as u32;
self.stack_slots.push((id, size_bytes));
id
}
/// Emit this function as textual IR.
pub fn emit(&self) -> String {
let mut s = String::new();
s.push_str(&format!("function %{}(", self.name));
for (i, ty) in self.sig.params.iter().enumerate() {
if i > 0 {
s.push_str(", ");
}
s.push_str(&ty.to_string());
}
s.push_str(") -> ");
for (i, ty) in self.sig.returns.iter().enumerate() {
if i > 0 {
s.push_str(", ");
}
s.push_str(&ty.to_string());
}
s.push_str(" system_v {\n");
for (id, size) in &self.stack_slots {
s.push_str(&format!(" ss{} = explicit_slot {}\n", id, size));
}
for (id, name) in &self.global_values {
s.push_str(&format!(" gv{} = symbol colocated %{}\n", id, name));
}
for (id, sig) in &self.sig_refs {
s.push_str(&format!(" sig{} = {}\n", id, sig));
}
for (id, name, sig_id) in &self.func_refs {
s.push_str(&format!(
" fn{} = colocated %{} sig{}\n",
id, name, sig_id
));
}
if !self.stack_slots.is_empty()
|| !self.global_values.is_empty()
|| !self.func_refs.is_empty()
{
s.push('\n');
}
for block in &self.blocks {
s.push_str(&block.emit());
}
s.push_str("}\n");
s
}
}
/// A Cranelift module — the top-level compilation unit.
#[derive(Debug, Clone)]
pub struct CraneliftModule {
/// Module name (for display purposes)
pub name: String,
/// Target triple (e.g. "x86_64-unknown-linux-gnu")
pub target: String,
/// Function definitions
pub functions: Vec<CraneliftFunction>,
/// Function declarations (external references)
pub func_decls: Vec<(String, Signature)>,
/// Data objects
pub data_objects: Vec<CraneliftDataObject>,
/// Global value map: name → gv_id
pub global_values: HashMap<String, u32>,
}
impl CraneliftModule {
/// Create a new module.
pub fn new(name: impl Into<String>) -> Self {
CraneliftModule {
name: name.into(),
target: "x86_64-unknown-linux-gnu".to_string(),
functions: vec![],
func_decls: vec![],
data_objects: vec![],
global_values: HashMap::new(),
}
}
/// Add a function definition.
pub fn add_function(&mut self, func: CraneliftFunction) {
self.functions.push(func);
}
/// Add a function declaration (external).
pub fn add_func_decl(&mut self, name: impl Into<String>, sig: Signature) {
self.func_decls.push((name.into(), sig));
}
/// Add a data object.
pub fn add_data_object(&mut self, obj: CraneliftDataObject) {
self.data_objects.push(obj);
}
/// Emit the full module as textual IR.
pub fn emit(&self) -> String {
let mut s = String::new();
s.push_str(&format!("; target = \"{}\"\n\n", self.target));
for (name, sig) in &self.func_decls {
s.push_str(&format!("declare %{}{};\n", name, sig));
}
if !self.func_decls.is_empty() {
s.push('\n');
}
for obj in &self.data_objects {
s.push_str(&obj.emit());
s.push('\n');
}
for func in &self.functions {
s.push_str(&func.emit());
s.push('\n');
}
s
}
}