-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinary.lean
More file actions
5250 lines (4960 loc) · 241 KB
/
Copy pathBinary.lean
File metadata and controls
5250 lines (4960 loc) · 241 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
import LeanExe.Core
import LeanExe.IR.Core
import LeanExe.Wasm.Annotations
import LeanExe.Wasm.Image.Emit
import LeanExe.Wasm.Instr
import LeanExe.Wasm.Leb
import LeanExe.Wasm.ScalarDescriptor
namespace LeanExe.Wasm.Binary
def byte (n : Nat) : UInt8 :=
UInt8.ofNat n
def ofNats (bytes : List Nat) : List UInt8 :=
bytes.map byte
def u32leb (n : Nat) : List UInt8 :=
(Leb.u32lebU64 (UInt64.ofNat n)).toList
def intBits (n : Int) : UInt64 :=
if 0 ≤ n then UInt64.ofNat n.toNat else 0 - UInt64.ofNat (-n).toNat
def s64lebInt (n : Int) : List UInt8 :=
(Leb.s64lebU64 (intBits n)).toList
def byteVec (bytes : List UInt8) : List UInt8 :=
(Leb.byteVecBytes (ByteArray.mk bytes.toArray)).toList
def concatBytes : List (List UInt8) → List UInt8
| [] => []
| item :: rest => item ++ concatBytes rest
def u32Vec (values : List Nat) : List UInt8 :=
(Leb.u32VecBytes (values.map UInt64.ofNat).toArray).toList
def vec (items : List (List UInt8)) : List UInt8 :=
(Leb.vecBytes (items.map (fun item => ByteArray.mk item.toArray)).toArray).toList
def name (s : String) : List UInt8 :=
byteVec s.toUTF8.data.toList
def wasmSection (id : Nat) (payload : List UInt8) : List UInt8 :=
(Leb.sectionBytes (UInt64.ofNat id) (ByteArray.mk payload.toArray)).toList
def funcType (params results : List UInt8) : List UInt8 :=
byte 96 :: byteVec params ++ byteVec results
def i32 : UInt8 :=
byte 127
def i64 : UInt8 :=
byte 126
def typeSection : List UInt8 :=
wasmSection 1 <| vec [
funcType [i32] [i32],
funcType [] [],
funcType [i32, i32] [i32]
]
def functionSection : List UInt8 :=
wasmSection 3 <| byteVec (ofNats [0, 1, 2])
def memorySection : List UInt8 :=
wasmSection 5 <| vec [
ofNats [0] ++ u32leb 1
]
def globalSection : List UInt8 :=
wasmSection 6 <| vec [
ofNats [127, 1, 65] ++ u32leb 4096 ++ ofNats [11]
]
def exportEntry (entryName : String) (kind index : Nat) : List UInt8 :=
name entryName ++ ofNats [kind] ++ u32leb index
def exportSection : List UInt8 :=
wasmSection 7 <| vec [
exportEntry "memory" 2 0,
exportEntry "alloc" 0 0,
exportEntry "reset" 0 1,
exportEntry "validate" 0 2
]
def body (locals code : List UInt8) : List UInt8 :=
byteVec (locals ++ code ++ ofNats [11])
def allocBody : List UInt8 :=
body
(ofNats [0])
(ofNats [
35, 0,
35, 0,
32, 0,
106,
36, 0
])
def resetBody : List UInt8 :=
body
(ofNats [0])
(ofNats [65] ++ u32leb 4096 ++ ofNats [36, 0])
def validateBody (validator : LeanExe.Core.LoweredValidator) : List UInt8 :=
body
(ofNats [1, 2, 127])
(ofNats [
65, 0,
33, 2,
2, 64,
2, 64,
3, 64,
32, 2,
32, 1,
79,
13, 1,
32, 0,
32, 2,
106,
45, 0, 0,
33, 3,
32, 3,
65
] ++ u32leb validator.min ++ ofNats [
73,
13, 2,
32, 3,
65
] ++ u32leb validator.max ++ ofNats [
75,
13, 2,
32, 2,
65, 1,
106,
33, 2,
12, 0,
11,
11,
65, 1,
15,
11,
65, 0
])
def codeSection (validator : LeanExe.Core.LoweredValidator) : List UInt8 :=
wasmSection 10 <| vec [
allocBody,
resetBody,
validateBody validator
]
def moduleBytes
(validator : LeanExe.Core.LoweredValidator :=
LeanExe.Core.lower LeanExe.Core.asciiDigits) : ByteArray :=
ByteArray.mk <| (ofNats [0, 97, 115, 109, 1, 0, 0, 0]
++ typeSection
++ functionSection
++ memorySection
++ globalSection
++ exportSection
++ codeSection validator).toArray
def i64Const (n : Nat) : List UInt8 :=
let bits := n % (2 ^ 64)
let signed :=
if bits < 2 ^ 63 then
Int.ofNat bits
else
Int.ofNat bits - Int.ofNat (2 ^ 64)
byte 66 :: s64lebInt signed
def i32Const (n : Nat) : List UInt8 :=
byte 65 :: u32leb n
def localGet (index : Nat) : List UInt8 :=
ofNats [32] ++ u32leb index
def localSet (index : Nat) : List UInt8 :=
ofNats [33] ++ u32leb index
def localTee (index : Nat) : List UInt8 :=
ofNats [34] ++ u32leb index
def call (index : Nat) : List UInt8 :=
ofNats [16] ++ u32leb index
def globalGet (index : Nat) : List UInt8 :=
ofNats [35] ++ u32leb index
def globalSet (index : Nat) : List UInt8 :=
ofNats [36] ++ u32leb index
namespace CoreWasm
abbrev Expr := LeanExe.IR.Expr
abbrev Cond := LeanExe.IR.Cond
abbrev LocalLet := LeanExe.IR.LocalLet
abbrev Stmt := LeanExe.IR.Stmt
abbrev Func := LeanExe.IR.Func
abbrev Module := LeanExe.IR.Module
abbrev Instr := LeanExe.Wasm.Instr
mutual
/-- Compatibility wrapper over the image emitter's authoritative opcode
encoding for byte-list consumers that have not yet moved to `ByteArray`. -/
def encodeInstr : Instr → List UInt8
| instr => LeanExe.Wasm.Image.emitInstr instr |>.toList
def encodeInstrs : List Instr → List UInt8
| [] => []
| instr :: rest => encodeInstr instr ++ encodeInstrs rest
end
/-! Instruction-building atoms. These shadow the byte-level helpers of the
same names in the outer namespace, so the emitters below build structured
instructions while the encoder above remains the only byte producer. -/
def i64Const (n : Nat) : List Instr :=
[.constI64 n]
def i32Const (n : Nat) : List Instr :=
[.constI32 n]
def localGet (index : Nat) : List Instr :=
[.localGet index]
def localSet (index : Nat) : List Instr :=
[.localSet index]
def localTee (index : Nat) : List Instr :=
[.localTee index]
def call (index : Nat) : List Instr :=
[.call index]
def globalGet (index : Nat) : List Instr :=
[.globalGet index]
def globalSet (index : Nat) : List Instr :=
[.globalSet index]
mutual
partial def shiftExprCalls (offset : Nat) : Expr → Expr
| .local index => .local index
| .trap => .trap
| .u64 value => .u64 value
| .f64SqrtBits value => .f64SqrtBits (shiftExprCalls offset value)
| .floatUnary op value => .floatUnary op (shiftExprCalls offset value)
| .u64Bin op left right =>
.u64Bin op (shiftExprCalls offset left) (shiftExprCalls offset right)
| .ite cond thenValue elseValue =>
.ite (shiftCondCalls offset cond)
(shiftExprCalls offset thenValue)
(shiftExprCalls offset elseValue)
| .letE slot value body =>
.letE slot (shiftExprCalls offset value) (shiftExprCalls offset body)
| .letCall slots index args body =>
.letCall slots (index + offset) (args.map (shiftExprCalls offset))
(shiftExprCalls offset body)
| .letLets lets body =>
.letLets (lets.map (shiftLocalLetCalls offset)) (shiftExprCalls offset body)
| .runtimeStat stat => .runtimeStat stat
| .release ptr => .release (shiftExprCalls offset ptr)
| .arrayAllocSlots width childMask cells =>
.arrayAllocSlots width childMask (shiftExprCalls offset cells)
| .heapAllocSlots childMask ownedMask values =>
.heapAllocSlots childMask ownedMask (values.map (shiftExprCalls offset))
| .heapLoadSlot ptr slot =>
.heapLoadSlot (shiftExprCalls offset ptr) slot
| .arrayLiteralSlots width childMask elements =>
.arrayLiteralSlots width childMask
(elements.map fun element =>
(element.fst, element.snd.map (shiftExprCalls offset)))
| .arrayReplicateSlots width childMask ownedMask cells values =>
.arrayReplicateSlots width childMask ownedMask (shiftExprCalls offset cells)
(values.map (shiftExprCalls offset))
| .arraySize array =>
.arraySize (shiftExprCalls offset array)
| .arrayGetSlot width slot array index =>
.arrayGetSlot width slot (shiftExprCalls offset array) (shiftExprCalls offset index)
| .arraySetSlots width childMask ownedMask array index values =>
.arraySetSlots width childMask ownedMask (shiftExprCalls offset array)
(shiftExprCalls offset index)
(values.map (shiftExprCalls offset))
| .arrayPushSlots width childMask ownedMask array values =>
.arrayPushSlots width childMask ownedMask (shiftExprCalls offset array)
(values.map (shiftExprCalls offset))
| .arrayPopSlots width childMask array =>
.arrayPopSlots width childMask (shiftExprCalls offset array)
| .arrayAppendSlots width childMask left right =>
.arrayAppendSlots width childMask (shiftExprCalls offset left) (shiftExprCalls offset right)
| .arrayExtractSlots width childMask array start stop =>
.arrayExtractSlots width childMask (shiftExprCalls offset array) (shiftExprCalls offset start)
(shiftExprCalls offset stop)
| .arrayMapSlots sourceWidth resultWidth childMask ownedMask array itemStart bodyValues bodyLets =>
.arrayMapSlots sourceWidth resultWidth childMask ownedMask (shiftExprCalls offset array) itemStart
(bodyValues.map (shiftExprCalls offset)) (bodyLets.map (shiftLocalLetCalls offset))
| .arrayFoldMultiSlot sourceWidth resultWidth reverse array start stop initValues accStart
itemStart bodyValues bodyLets bodyDone releaseOffsets resultSlot =>
.arrayFoldMultiSlot sourceWidth resultWidth reverse (shiftExprCalls offset array)
(shiftExprCalls offset start) (shiftExprCalls offset stop)
(initValues.map (shiftExprCalls offset)) accStart itemStart
(bodyValues.map (shiftExprCalls offset)) (bodyLets.map (shiftLocalLetCalls offset))
(shiftExprCalls offset bodyDone) releaseOffsets resultSlot
| .arrayFindIdxSlots sourceWidth array itemStart predicate returnPayload =>
.arrayFindIdxSlots sourceWidth (shiftExprCalls offset array) itemStart
(shiftExprCalls offset predicate) returnPayload
| .arrayFindSlot sourceWidth array itemStart predicate slot =>
.arrayFindSlot sourceWidth (shiftExprCalls offset array) itemStart
(shiftExprCalls offset predicate) slot
| .arrayEqSlots width left right leftStart rightStart predicate =>
.arrayEqSlots width (shiftExprCalls offset left) (shiftExprCalls offset right)
leftStart rightStart (shiftExprCalls offset predicate)
| .arrayAnySlots sourceWidth array start stop itemStart predicate forAll =>
.arrayAnySlots sourceWidth (shiftExprCalls offset array) (shiftExprCalls offset start)
(shiftExprCalls offset stop) itemStart (shiftExprCalls offset predicate) forAll
| .arrayFilterSlots sourceWidth childMask array start stop itemStart predicate =>
.arrayFilterSlots sourceWidth childMask (shiftExprCalls offset array) (shiftExprCalls offset start)
(shiftExprCalls offset stop) itemStart (shiftExprCalls offset predicate)
| .arrayInsertIfInBoundsSlots width childMask ownedMask array index values =>
.arrayInsertIfInBoundsSlots width childMask ownedMask (shiftExprCalls offset array)
(shiftExprCalls offset index) (values.map (shiftExprCalls offset))
| .arrayEraseIfInBoundsSlots width childMask array index =>
.arrayEraseIfInBoundsSlots width childMask (shiftExprCalls offset array) (shiftExprCalls offset index)
| .arraySwapIfInBoundsSlots width childMask array left right =>
.arraySwapIfInBoundsSlots width childMask (shiftExprCalls offset array)
(shiftExprCalls offset left) (shiftExprCalls offset right)
| .arrayReverseSlots width childMask array =>
.arrayReverseSlots width childMask (shiftExprCalls offset array)
| .byteArrayGet ptr len index =>
.byteArrayGet (shiftExprCalls offset ptr) (shiftExprCalls offset len)
(shiftExprCalls offset index)
| .byteArrayLoad32 ptr len index =>
.byteArrayLoad32 (shiftExprCalls offset ptr) (shiftExprCalls offset len)
(shiftExprCalls offset index)
| .byteArrayGenerate32Ptr len indexSlot body =>
.byteArrayGenerate32Ptr (shiftExprCalls offset len) indexSlot (shiftExprCalls offset body)
| .byteArrayPushPtr ptr len value =>
.byteArrayPushPtr (shiftExprCalls offset ptr) (shiftExprCalls offset len)
(shiftExprCalls offset value)
| .byteArrayAppendPtr leftPtr leftLen rightPtr rightLen =>
.byteArrayAppendPtr (shiftExprCalls offset leftPtr) (shiftExprCalls offset leftLen)
(shiftExprCalls offset rightPtr) (shiftExprCalls offset rightLen)
| .byteArraySetPtr ptr len index value =>
.byteArraySetPtr (shiftExprCalls offset ptr) (shiftExprCalls offset len)
(shiftExprCalls offset index) (shiftExprCalls offset value)
| .byteArrayFromArrayPtr array =>
.byteArrayFromArrayPtr (shiftExprCalls offset array)
| .byteArrayCopySlicePtr srcPtr srcLen srcOff destPtr destLen destOff copyLen =>
.byteArrayCopySlicePtr (shiftExprCalls offset srcPtr) (shiftExprCalls offset srcLen)
(shiftExprCalls offset srcOff) (shiftExprCalls offset destPtr)
(shiftExprCalls offset destLen) (shiftExprCalls offset destOff)
(shiftExprCalls offset copyLen)
| .byteArrayEq leftPtr leftLen rightPtr rightLen =>
.byteArrayEq (shiftExprCalls offset leftPtr) (shiftExprCalls offset leftLen)
(shiftExprCalls offset rightPtr) (shiftExprCalls offset rightLen)
| .byteArrayFindIdx ptr len start byteSlot predicate returnPayload =>
.byteArrayFindIdx (shiftExprCalls offset ptr) (shiftExprCalls offset len)
(shiftExprCalls offset start) byteSlot (shiftExprCalls offset predicate) returnPayload
| .byteArrayFoldMultiSlot resultWidth ptr len start stop initValues accStart byteSlot
bodyValues bodyLets bodyDone releaseOffsets resultSlot =>
.byteArrayFoldMultiSlot resultWidth (shiftExprCalls offset ptr)
(shiftExprCalls offset len) (shiftExprCalls offset start) (shiftExprCalls offset stop)
(initValues.map (shiftExprCalls offset)) accStart byteSlot
(bodyValues.map (shiftExprCalls offset)) (bodyLets.map (shiftLocalLetCalls offset))
(shiftExprCalls offset bodyDone) releaseOffsets resultSlot
| .rangeFoldMultiSlot resultWidth start stop step initValues accStart itemSlot bodyValues
bodyLets bodyDone releaseOffsets resultSlot =>
.rangeFoldMultiSlot resultWidth (shiftExprCalls offset start) (shiftExprCalls offset stop)
(shiftExprCalls offset step) (initValues.map (shiftExprCalls offset)) accStart
itemSlot (bodyValues.map (shiftExprCalls offset)) (bodyLets.map (shiftLocalLetCalls offset))
(shiftExprCalls offset bodyDone) releaseOffsets resultSlot
| .loopFoldMultiSlot resultWidth initValues accStart bodyValues bodyLets bodyDone
releaseOffsets resultSlot =>
.loopFoldMultiSlot resultWidth (initValues.map (shiftExprCalls offset)) accStart
(bodyValues.map (shiftExprCalls offset)) (bodyLets.map (shiftLocalLetCalls offset))
(shiftExprCalls offset bodyDone) releaseOffsets resultSlot
| .heapLinearPredicate ptr continueTag fieldSlotCount recursiveFieldOffset fieldStart
predicate stopWhenTrue terminalValue =>
.heapLinearPredicate (shiftExprCalls offset ptr) continueTag fieldSlotCount
recursiveFieldOffset fieldStart (shiftExprCalls offset predicate) stopWhenTrue
terminalValue
| .call index args =>
.call (index + offset) (args.map (shiftExprCalls offset))
partial def shiftCondCalls (offset : Nat) : Cond → Cond
| .true => .true
| .false => .false
| .eqU64 left right =>
.eqU64 (shiftExprCalls offset left) (shiftExprCalls offset right)
| .ltU64 left right =>
.ltU64 (shiftExprCalls offset left) (shiftExprCalls offset right)
| .leU64 left right =>
.leU64 (shiftExprCalls offset left) (shiftExprCalls offset right)
| .not cond => .not (shiftCondCalls offset cond)
| .and left right => .and (shiftCondCalls offset left) (shiftCondCalls offset right)
| .or left right => .or (shiftCondCalls offset left) (shiftCondCalls offset right)
partial def shiftLocalLetCalls (offset : Nat) : LocalLet → LocalLet
| .expr slot value => .expr slot (shiftExprCalls offset value)
| .call slots index args => .call slots (index + offset) (args.map (shiftExprCalls offset))
| .slots slots values => .slots slots (values.map (shiftExprCalls offset))
| .branch cond thenLets elseLets =>
.branch (shiftCondCalls offset cond)
(thenLets.map (shiftLocalLetCalls offset))
(elseLets.map (shiftLocalLetCalls offset))
partial def shiftStmtCalls (offset : Nat) : Stmt → Stmt
| .skip => .skip
| .assign index value => .assign index (shiftExprCalls offset value)
| .call slots index args => .call slots (index + offset) (args.map (shiftExprCalls offset))
| .release ptr => .release (shiftExprCalls offset ptr)
| .arrayFoldMultiSlotAssign sourceWidth resultWidth reverse array start stop initValues accStart
itemStart bodyValues bodyLets bodyDone releaseOffsets targets =>
.arrayFoldMultiSlotAssign sourceWidth resultWidth reverse (shiftExprCalls offset array)
(shiftExprCalls offset start) (shiftExprCalls offset stop)
(initValues.map (shiftExprCalls offset)) accStart itemStart
(bodyValues.map (shiftExprCalls offset)) (bodyLets.map (shiftLocalLetCalls offset))
(shiftExprCalls offset bodyDone) releaseOffsets targets
| .byteArrayFoldMultiSlotAssign resultWidth ptr len start stop initValues accStart
byteSlot bodyValues bodyLets bodyDone releaseOffsets targets =>
.byteArrayFoldMultiSlotAssign resultWidth (shiftExprCalls offset ptr)
(shiftExprCalls offset len) (shiftExprCalls offset start) (shiftExprCalls offset stop)
(initValues.map (shiftExprCalls offset)) accStart byteSlot
(bodyValues.map (shiftExprCalls offset)) (bodyLets.map (shiftLocalLetCalls offset))
(shiftExprCalls offset bodyDone) releaseOffsets targets
| .rangeFoldMultiSlotAssign resultWidth start stop step initValues accStart itemSlot
bodyValues bodyLets bodyDone releaseOffsets targets =>
.rangeFoldMultiSlotAssign resultWidth (shiftExprCalls offset start)
(shiftExprCalls offset stop) (shiftExprCalls offset step)
(initValues.map (shiftExprCalls offset)) accStart itemSlot
(bodyValues.map (shiftExprCalls offset)) (bodyLets.map (shiftLocalLetCalls offset))
(shiftExprCalls offset bodyDone) releaseOffsets targets
| .loopFoldMultiSlotAssign resultWidth initValues accStart bodyValues bodyLets bodyDone
releaseOffsets targets =>
.loopFoldMultiSlotAssign resultWidth (initValues.map (shiftExprCalls offset)) accStart
(bodyValues.map (shiftExprCalls offset)) (bodyLets.map (shiftLocalLetCalls offset))
(shiftExprCalls offset bodyDone) releaseOffsets targets
| .ite cond thenStmt elseStmt =>
.ite (shiftCondCalls offset cond) (shiftStmtCalls offset thenStmt)
(shiftStmtCalls offset elseStmt)
| .seq first second => .seq (shiftStmtCalls offset first) (shiftStmtCalls offset second)
| .while cond body => .while (shiftCondCalls offset cond) (shiftStmtCalls offset body)
end
def shiftFuncCalls (offset : Nat) (func : Func) : Func :=
{ func with
body := shiftStmtCalls offset func.body,
results := func.results.map (shiftExprCalls offset) }
def shiftModuleCalls (offset : Nat) (module_ : Module) : Module :=
{ funcs := module_.funcs.map (shiftFuncCalls offset) }
def emitU64Op : LeanExe.IR.U64Op → List Instr
| .add => [Instr.addI64]
| .natAdd => [Instr.addI64]
| .sub => [Instr.subI64]
| .natSub => [Instr.subI64]
| .mul => [Instr.mulI64]
| .natMul => [Instr.mulI64]
| .divU => [Instr.divUI64]
| .modU => [Instr.remUI64]
| .bitAnd => [Instr.andI64]
| .bitOr => [Instr.orI64]
| .bitXor => [Instr.xorI64]
| .shiftLeft => [Instr.shlI64]
| .shiftRight => [Instr.shrUI64]
| .f64AddBits => [Instr.addF64]
| .f64MulBits => [Instr.mulF64]
| .f64SubBits => [Instr.subF64]
| .f64DivBits => [Instr.divF64]
| .f32AddBits => [Instr.addF32]
| .f32SubBits => [Instr.subF32]
| .f32MulBits => [Instr.mulF32]
| .f32DivBits => [Instr.divF32]
def emitFloatUnary : LeanExe.IR.FloatUnaryOp → List Instr
| .f32SqrtBits =>
[.wrapI64, .f32ReinterpretI32, .sqrtF32, .i32ReinterpretF32, .extendUI32]
| .f32ToF64Bits =>
[.wrapI64, .f32ReinterpretI32, .f64PromoteF32, .i64ReinterpretF64]
| .f64ToF32Bits =>
[.f64ReinterpretI64, .f32DemoteF64, .i32ReinterpretF32, .extendUI32]
def isF32Binary : LeanExe.IR.U64Op → Bool
| .f32AddBits | .f32SubBits | .f32MulBits | .f32DivBits => true
| _ => false
def coreGlobalSection : List UInt8 :=
wasmSection 6 <| vec [
ofNats [126, 1] ++ LeanExe.Wasm.Binary.i64Const 4096 ++ ofNats [11],
ofNats [126, 1] ++ LeanExe.Wasm.Binary.i64Const 0 ++ ofNats [11],
ofNats [126, 1] ++ LeanExe.Wasm.Binary.i64Const 0 ++ ofNats [11],
ofNats [126, 1] ++ LeanExe.Wasm.Binary.i64Const 0 ++ ofNats [11],
ofNats [126, 1] ++ LeanExe.Wasm.Binary.i64Const 0 ++ ofNats [11],
ofNats [126, 1] ++ LeanExe.Wasm.Binary.i64Const 0 ++ ofNats [11]
]
def coreMemorySection : List UInt8 :=
wasmSection 5 <| vec [
ofNats [0] ++ u32leb 16
]
def i32WrapI64 : List Instr :=
[Instr.wrapI64]
def i64Load : List Instr :=
[Instr.load64]
def i32Load : List Instr :=
[Instr.load32]
def i64Eq : List Instr :=
[Instr.eqI64]
def i64Store : List Instr :=
[Instr.store64]
def i32Store : List Instr :=
[Instr.store32]
def i32Load8U : List Instr :=
[Instr.load8U]
def i32Store8 : List Instr :=
[Instr.store8]
def i32Eq : List Instr :=
[Instr.eqI32]
def i32ConstNegOne : List Instr :=
[Instr.constI32NegOne]
def i64ExtendI32U : List Instr :=
[Instr.extendUI32]
def i64LtU : List Instr :=
[Instr.ltUI64]
def i64Ne : List Instr :=
[Instr.neI64]
def i64LeU : List Instr :=
[Instr.leUI64]
def i64GeU : List Instr :=
[Instr.geUI64]
def i64And : List Instr :=
[Instr.andI64]
def i64ShrU : List Instr :=
[Instr.shrUI64]
def i64Eqz : List Instr :=
[Instr.eqzI64]
def unreachable : List Instr :=
[Instr.unreachable]
def returnOp : List Instr :=
[Instr.ret]
def memorySize : List Instr :=
[Instr.memorySize]
def memoryGrow : List Instr :=
[Instr.memoryGrow]
def i64Align8 (value : List Instr) : List Instr :=
value ++ i64Const 7 ++ [Instr.addI64] ++ i64Const 8 ++ [Instr.divUI64] ++
i64Const 8 ++ [Instr.mulI64]
def rcHeaderBytes : Nat :=
48
def rcMagic : Nat :=
5501223100278326855
def rcKindRaw : Nat :=
0
def rcKindSlots : Nat :=
1
def rcKindArray : Nat :=
2
def runtimeStatGlobal : LeanExe.IR.RuntimeStat → Nat
| .allocs => 2
| .retains => 3
| .releases => 4
| .frees => 5
def incGlobal (index : Nat) : List Instr :=
globalGet index ++ i64Const 1 ++ [Instr.addI64] ++ globalSet index
def rcHeaderAddress (ptr : List Instr) (offset : Nat) : List Instr :=
ptr ++ i64Const offset ++ [Instr.subI64] ++ i32WrapI64
def rcHeaderLoad (ptr : List Instr) (offset : Nat) : List Instr :=
rcHeaderAddress ptr offset ++ i64Load
def rcHeaderStore (ptr : List Instr) (offset : Nat) (value : List Instr) : List Instr :=
rcHeaderAddress ptr offset ++ value ++ i64Store
def rcInitHeader
(ptr capacity kind aux1 aux2 : List Instr) :
List Instr :=
rcHeaderStore ptr 48 (i64Const rcMagic) ++
rcHeaderStore ptr 40 (i64Const 1) ++
rcHeaderStore ptr 32 capacity ++
rcHeaderStore ptr 24 kind ++
rcHeaderStore ptr 16 aux1 ++
rcHeaderStore ptr 8 aux2
def rcAllocPayload
(scratch : Nat)
(payloadBytes kind aux1 aux2 : List Instr) :
List Instr :=
let alignedLocal := scratch
let prevLocal := scratch + 1
let currLocal := scratch + 2
let sizeLocal := scratch + 3
let nextLocal := scratch + 4
let ptrLocal := scratch + 5
let endLocal := sizeLocal
let requiredPagesLocal := nextLocal
let unlinkCurrent :=
localGet prevLocal ++ i64Const 0 ++ i64Eq ++
([Instr.iff false (localGet nextLocal ++ globalSet 1) (some (rcHeaderStore (localGet prevLocal) 8 (localGet nextLocal)))])
let takeCurrent :=
unlinkCurrent ++
rcInitHeader (localGet currLocal) (localGet sizeLocal) kind aux1 aux2 ++
localGet currLocal ++ localSet ptrLocal
let searchLoop :=
([Instr.block [Instr.loop (localGet currLocal ++ i64Const 0 ++ i64Eq ++ [Instr.brIf 1] ++
localGet ptrLocal ++ i64Const 0 ++ i64Ne ++ [Instr.brIf 1] ++
rcHeaderLoad (localGet currLocal) 32 ++ localSet sizeLocal ++
rcHeaderLoad (localGet currLocal) 8 ++ localSet nextLocal ++
localGet sizeLocal ++ localGet alignedLocal ++ i64GeU ++
([Instr.iff false (takeCurrent) (some (localGet currLocal ++ localSet prevLocal ++
localGet nextLocal ++ localSet currLocal))]) ++
[Instr.br 0])]])
let bumpAllocate :=
globalGet 0 ++ i64Const rcHeaderBytes ++ [Instr.addI64] ++ localGet alignedLocal ++
[Instr.addI64] ++ localTee endLocal ++
globalGet 0 ++ i64LtU ++
([Instr.iff false (unreachable) none]) ++
localGet endLocal ++ i64Const 1 ++ [Instr.subI64] ++ i64Const 65536 ++
[Instr.divUI64] ++ i64Const 1 ++ [Instr.addI64] ++ localSet requiredPagesLocal ++
memorySize ++ i64ExtendI32U ++ localGet requiredPagesLocal ++ i64LtU ++
([Instr.iff false (localGet requiredPagesLocal ++ memorySize ++ i64ExtendI32U ++ [Instr.subI64] ++
i32WrapI64 ++ memoryGrow ++ i32ConstNegOne ++ i32Eq ++
([Instr.iff false (unreachable) none])) none]) ++
globalGet 0 ++ i64Const rcHeaderBytes ++ [Instr.addI64] ++ localSet ptrLocal ++
localGet endLocal ++ globalSet 0 ++
rcInitHeader (localGet ptrLocal) (localGet alignedLocal) kind aux1 aux2
i64Align8 payloadBytes ++ localSet alignedLocal ++
localGet alignedLocal ++ i64Const 8 ++ i64LtU ++
([Instr.iff false (i64Const 8 ++ localSet alignedLocal) none]) ++
i64Const 0 ++ localSet ptrLocal ++
i64Const 0 ++ localSet prevLocal ++
globalGet 1 ++ localSet currLocal ++
searchLoop ++
localGet ptrLocal ++ i64Const 0 ++ i64Eq ++
([Instr.iff false (bumpAllocate) none]) ++
incGlobal (runtimeStatGlobal .allocs) ++
localGet ptrLocal
def rcArrayPayloadBytes (width : Nat) (len : List Instr) : List Instr :=
i64Const 8 ++ len ++ i64Const width ++ [Instr.mulI64] ++ i64Const 8 ++ [Instr.mulI64, Instr.addI64]
def rcAllocArrayObject (scratch width childMask : Nat) (len : List Instr) : List Instr :=
rcAllocPayload scratch
(rcArrayPayloadBytes width len)
(i64Const rcKindArray)
(i64Const width)
(i64Const childMask)
def rcAllocSlotObject (scratch slots childMask : Nat) : List Instr :=
rcAllocPayload scratch
(i64Const (slots * 8))
(i64Const rcKindSlots)
(i64Const slots)
(i64Const childMask)
def rcAllocRawObject (scratch : Nat) (len : List Instr) : List Instr :=
rcAllocPayload scratch (len) (i64Const rcKindRaw) (i64Const 0) (i64Const 0)
def arrayCellAddress (base index : List Instr) : List Instr :=
base ++ index ++ i64Const 1 ++ [Instr.addI64] ++ i64Const 8 ++ [Instr.mulI64, Instr.addI64] ++
i32WrapI64
def arraySlotAddress (width slot : Nat) (base index : List Instr) : List Instr :=
base ++ index ++ i64Const width ++ [Instr.mulI64] ++ i64Const (slot + 1) ++
[Instr.addI64] ++ i64Const 8 ++ [Instr.mulI64, Instr.addI64] ++ i32WrapI64
def enumerateAux {α : Type} : List α → Nat → List (Nat × α)
| [], _ => []
| item :: rest, index => (index, item) :: enumerateAux rest (index + 1)
def enumerate {α : Type} (items : List α) : List (Nat × α) :=
enumerateAux items 0
def maskBitSet (mask slot : Nat) : Bool :=
(mask / (2 ^ slot)) % 2 == 1
def emitRetainLocal (ptrLocal rcLocal : Nat) : List Instr :=
localGet ptrLocal ++ i64Const 0 ++ i64Ne ++
([Instr.iff false (rcHeaderLoad (localGet ptrLocal) 48 ++ i64Const rcMagic ++ i64Ne ++
([Instr.iff false (unreachable) none]) ++
rcHeaderLoad (localGet ptrLocal) 40 ++ localSet rcLocal ++
localGet rcLocal ++ i64Const 0 ++ i64Eq ++
([Instr.iff false (unreachable) none]) ++
incGlobal (runtimeStatGlobal .retains) ++
rcHeaderStore (localGet ptrLocal) 40 (localGet rcLocal ++ i64Const 1 ++ [Instr.addI64])) none])
def emitRetainArraySlotsAtIndex
(width childMask skipMask childLocal rcLocal : Nat)
(base index : List Instr) :
List Instr :=
(List.range width).flatMap fun slot =>
if maskBitSet childMask slot && !maskBitSet skipMask slot then
arraySlotAddress width slot base index ++ i64Load ++ localSet childLocal ++
emitRetainLocal childLocal rcLocal
else
[]
def emitRetainArrayRange
(width childMask loopLocal childLocal rcLocal : Nat)
(base start len : List Instr) :
List Instr :=
if childMask == 0 then
[]
else
let index := start ++ localGet loopLocal ++ [Instr.addI64]
i64Const 0 ++ localSet loopLocal ++
([Instr.block [Instr.loop (localGet loopLocal ++ len ++ i64GeU ++ [Instr.brIf 1] ++
emitRetainArraySlotsAtIndex width childMask 0 childLocal rcLocal base index ++
localGet loopLocal ++ i64Const 1 ++ [Instr.addI64] ++ localSet loopLocal ++
[Instr.br 0])]])
def emitRetainArrayRangeWithSpecial
(width childMask skipMask loopLocal childLocal rcLocal : Nat)
(base start len specialIndex : List Instr) :
List Instr :=
if childMask == 0 then
[]
else
let index := start ++ localGet loopLocal ++ [Instr.addI64]
i64Const 0 ++ localSet loopLocal ++
([Instr.block [Instr.loop (localGet loopLocal ++ len ++ i64GeU ++ [Instr.brIf 1] ++
index ++ specialIndex ++ i64Eq ++
([Instr.iff false (emitRetainArraySlotsAtIndex width childMask skipMask childLocal rcLocal base index) (some (emitRetainArraySlotsAtIndex width childMask 0 childLocal rcLocal base index))]) ++
localGet loopLocal ++ i64Const 1 ++ [Instr.addI64] ++ localSet loopLocal ++
[Instr.br 0])]])
mutual
partial def exprScratch : Expr → Nat
| .local _ => 0
| .trap => 0
| .u64 _ => 0
| .u64Bin .natAdd left right => 3 + max (exprScratch left) (exprScratch right)
| .u64Bin .natSub left right => 2 + max (exprScratch left) (exprScratch right)
| .u64Bin .natMul left right => 2 + max (exprScratch left) (exprScratch right)
| .u64Bin .divU left right => 2 + max (exprScratch left) (exprScratch right)
| .u64Bin .modU left right => 2 + max (exprScratch left) (exprScratch right)
| .f64SqrtBits value => exprScratch value
| .floatUnary _ value => exprScratch value
| .u64Bin _ left right => max (exprScratch left) (exprScratch right)
| .ite cond thenValue elseValue =>
max (condScratch cond) (max (exprScratch thenValue) (exprScratch elseValue))
| .letE _ value body => max (exprScratch value) (exprScratch body)
| .runtimeStat _ => 0
| .release ptr => exprScratch ptr
| .arrayAllocSlots _ _ cells => 8 + exprScratch cells
| .heapAllocSlots _ _ values =>
3 + values.length +
max 6 (values.foldl (fun n value => max n (exprScratch value)) 0)
| .heapLoadSlot ptr _ => 1 + exprScratch ptr
| .arrayLiteralSlots width _ elements =>
3 + width +
max 6
(elements.foldl
(fun n element =>
element.snd.foldl (fun m value => max m (exprScratch value)) n)
0)
| .arrayReplicateSlots _ _ _ cells values =>
5 + values.length +
max 6
(max (exprScratch cells) (values.foldl (fun n value => max n (exprScratch value)) 0))
| .arraySize array => 1 + exprScratch array
| .arrayGetSlot _ _ array index => 2 + max (exprScratch array) (exprScratch index)
| .arraySetSlots _ _ _ array index values =>
8 + values.length +
max 6
(max (exprScratch array)
(max (exprScratch index)
(values.foldl (fun n value => max n (exprScratch value)) 0)))
| .arrayPushSlots _ _ _ array values =>
8 + values.length +
max 6
(max (exprScratch array) (values.foldl (fun n value => max n (exprScratch value)) 0))
| .arrayPopSlots _ _ array => 8 + max 6 (exprScratch array)
| .arrayAppendSlots _ _ left right => 11 + max 6 (max (exprScratch left) (exprScratch right))
| .arrayExtractSlots _ _ array start stop =>
12 + max 6 (max (exprScratch array) (max (exprScratch start) (exprScratch stop)))
| .arrayMapSlots _ _ _ _ array _ bodyValues bodyLets =>
6 + max 6
(max (exprScratch array)
(max (bodyLets.foldl (fun n item => max n (localLetScratch item)) 0)
(bodyValues.foldl (fun n value => max n (exprScratch value)) 0)))
| .arrayFoldMultiSlot sourceWidth resultWidth _reverse array start stop initValues _ _ bodyValues
bodyLets bodyDone _ _ =>
let initScratch := initValues.foldl (fun n value => max n (exprScratch value)) 0
let letScratch := bodyLets.foldl (fun n item => max n (localLetScratch item)) 0
let bodyScratch :=
max letScratch <|
max (exprScratch bodyDone) (bodyValues.foldl (fun n value => max n (exprScratch value)) 0)
5 + sourceWidth + resultWidth + 2 +
max
(max (exprScratch array) (max (exprScratch start) (exprScratch stop)))
(max initScratch bodyScratch)
| .arrayFindIdxSlots sourceWidth array _ predicate _ =>
4 + sourceWidth + max (exprScratch array) (exprScratch predicate)
| .arrayFindSlot sourceWidth array _ predicate _ =>
4 + sourceWidth + max (exprScratch array) (exprScratch predicate)
| .arrayEqSlots width left right _ _ predicate =>
5 + max
(max (exprScratch left) (exprScratch right))
(max (exprScratch predicate) width)
| .arrayAnySlots sourceWidth array start stop _ predicate _ =>
6 + sourceWidth +
max
(max (exprScratch array) (max (exprScratch start) (exprScratch stop)))
(exprScratch predicate)
| .arrayFilterSlots sourceWidth _ array start stop _ predicate =>
10 + sourceWidth +
max 6
(max
(max (exprScratch array) (max (exprScratch start) (exprScratch stop)))
(exprScratch predicate))
| .arrayInsertIfInBoundsSlots _ _ _ array index values =>
10 + values.length +
max 6
(max (exprScratch array)
(max (exprScratch index)
(values.foldl (fun n value => max n (exprScratch value)) 0)))
| .arrayEraseIfInBoundsSlots _ _ array index =>
10 + max 6 (max (exprScratch array) (exprScratch index))
| .arraySwapIfInBoundsSlots _ _ array left right =>
9 + max 6 (max (exprScratch array) (max (exprScratch left) (exprScratch right)))
| .arrayReverseSlots _ _ array => 6 + max 6 (exprScratch array)
| .byteArrayGet ptr len index =>
3 + max (exprScratch ptr) (max (exprScratch len) (exprScratch index))
| .byteArrayLoad32 ptr len index =>
3 + max (exprScratch ptr) (max (exprScratch len) (exprScratch index))
| .byteArrayGenerate32Ptr len _ body =>
2 + max 6 (max (exprScratch len) (exprScratch body))
| .byteArrayPushPtr ptr len value =>
6 + max 6 (max (exprScratch ptr) (max (exprScratch len) (exprScratch value)))
| .byteArrayAppendPtr leftPtr leftLen rightPtr rightLen =>
7 + max 6
(max
(max (exprScratch leftPtr) (exprScratch leftLen))
(max (exprScratch rightPtr) (exprScratch rightLen)))
| .byteArraySetPtr ptr len index value =>
6 + max 6
(max
(max (exprScratch ptr) (exprScratch len))
(max (exprScratch index) (exprScratch value)))
| .byteArrayFromArrayPtr array => 4 + max 6 (exprScratch array)
| .byteArrayCopySlicePtr srcPtr srcLen srcOff destPtr destLen destOff copyLen =>
15 + max 6
(max
(max (exprScratch srcPtr) (max (exprScratch srcLen) (exprScratch srcOff)))
(max
(max (exprScratch destPtr) (max (exprScratch destLen) (exprScratch destOff)))
(exprScratch copyLen)))
| .byteArrayEq leftPtr leftLen rightPtr rightLen =>
6 + max
(max (exprScratch leftPtr) (exprScratch leftLen))
(max (exprScratch rightPtr) (exprScratch rightLen))
| .byteArrayFindIdx ptr len start _ predicate _ =>
4 + max
(max (exprScratch ptr) (max (exprScratch len) (exprScratch start)))
(exprScratch predicate)
| .byteArrayFoldMultiSlot resultWidth ptr len start stop initValues _ _ bodyValues
bodyLets bodyDone _ _ =>
let initScratch := initValues.foldl (fun n value => max n (exprScratch value)) 0
let letScratch := bodyLets.foldl (fun n item => max n (localLetScratch item)) 0
let bodyScratch :=
max letScratch <|
max (exprScratch bodyDone) (bodyValues.foldl (fun n value => max n (exprScratch value)) 0)
5 + resultWidth + 2 +
max
(max (exprScratch ptr) (exprScratch len))
(max (max (exprScratch start) (exprScratch stop))
(max initScratch bodyScratch))
| .rangeFoldMultiSlot resultWidth start stop step initValues _ _ bodyValues bodyLets bodyDone _ _ =>
let initScratch := initValues.foldl (fun n value => max n (exprScratch value)) 0
let letScratch := bodyLets.foldl (fun n item => max n (localLetScratch item)) 0
let bodyScratch :=
max letScratch <|
max (exprScratch bodyDone) (bodyValues.foldl (fun n value => max n (exprScratch value)) 0)
4 +
max
(max (exprScratch start) (max (exprScratch stop) (exprScratch step)))
(max (max initScratch bodyScratch) (max (bodyScratch + resultWidth + 1) 3))
| .loopFoldMultiSlot resultWidth initValues _ bodyValues bodyLets bodyDone _ _ =>
let initScratch := initValues.foldl (fun n value => max n (exprScratch value)) 0
let letScratch := bodyLets.foldl (fun n item => max n (localLetScratch item)) 0
let bodyScratch :=
max letScratch <|
max (exprScratch bodyDone) (bodyValues.foldl (fun n value => max n (exprScratch value)) 0)
2 + resultWidth + max initScratch bodyScratch
| .heapLinearPredicate ptr _ _ _ _ predicate _ _ =>
2 + max (exprScratch ptr) (exprScratch predicate)
| .call _ args => args.foldl (fun count arg => max count (exprScratch arg)) 0
| .letCall _ _ args body =>
max (args.foldl (fun count arg => max count (exprScratch arg)) 0) (exprScratch body)
| .letLets lets body =>
max
(lets.foldl (fun count item => max count (localLetScratch item)) 0)
(exprScratch body)
partial def localLetScratch : LocalLet → Nat
| .expr _ value => exprScratch value
| .call _ _ args => args.foldl (fun count arg => max count (exprScratch arg)) 0
| .slots _ values => values.foldl (fun count value => max count (exprScratch value)) 0
| .branch cond thenLets elseLets =>
max (condScratch cond)
(max
(thenLets.foldl (fun count item => max count (localLetScratch item)) 0)
(elseLets.foldl (fun count item => max count (localLetScratch item)) 0))
partial def condScratch : Cond → Nat
| .true => 0
| .false => 0
| .eqU64 left right => max (exprScratch left) (exprScratch right)
| .ltU64 left right => max (exprScratch left) (exprScratch right)
| .leU64 left right => max (exprScratch left) (exprScratch right)
| .not cond => condScratch cond
| .and left right => max (condScratch left) (condScratch right)
| .or left right => max (condScratch left) (condScratch right)
end
partial def stmtScratch : Stmt → Nat
| .skip => 0
| .assign _ value => exprScratch value
| .call _ _ args => args.foldl (fun count arg => max count (exprScratch arg)) 0
| .release ptr => exprScratch ptr
| .arrayFoldMultiSlotAssign sourceWidth resultWidth _reverse array start stop initValues _ _ bodyValues
bodyLets bodyDone _ _ =>
let initScratch := initValues.foldl (fun n value => max n (exprScratch value)) 0
let letScratch := bodyLets.foldl (fun n item => max n (localLetScratch item)) 0
let bodyScratch :=
max letScratch <|
max (exprScratch bodyDone) (bodyValues.foldl (fun n value => max n (exprScratch value)) 0)
5 + sourceWidth + resultWidth + 2 +
max
(max (exprScratch array) (max (exprScratch start) (exprScratch stop)))
(max initScratch bodyScratch)
| .byteArrayFoldMultiSlotAssign resultWidth ptr len start stop initValues _ _ bodyValues
bodyLets bodyDone _ _ =>
let initScratch := initValues.foldl (fun n value => max n (exprScratch value)) 0
let letScratch := bodyLets.foldl (fun n item => max n (localLetScratch item)) 0
let bodyScratch :=
max letScratch <|
max (exprScratch bodyDone) (bodyValues.foldl (fun n value => max n (exprScratch value)) 0)
5 + resultWidth + 2 +
max
(max (exprScratch ptr) (exprScratch len))
(max (max (exprScratch start) (exprScratch stop))
(max initScratch bodyScratch))
| .rangeFoldMultiSlotAssign resultWidth start stop step initValues _ _ bodyValues bodyLets bodyDone _ _ =>
let initScratch := initValues.foldl (fun n value => max n (exprScratch value)) 0
let letScratch := bodyLets.foldl (fun n item => max n (localLetScratch item)) 0
let bodyScratch :=
max letScratch <|
max (exprScratch bodyDone) (bodyValues.foldl (fun n value => max n (exprScratch value)) 0)
4 +
max
(max (exprScratch start) (max (exprScratch stop) (exprScratch step)))
(max (max initScratch bodyScratch) (max (bodyScratch + resultWidth + 1) 3))
| .loopFoldMultiSlotAssign resultWidth initValues _ bodyValues bodyLets bodyDone _ _ =>
let initScratch := initValues.foldl (fun n value => max n (exprScratch value)) 0
let letScratch := bodyLets.foldl (fun n item => max n (localLetScratch item)) 0
let bodyScratch :=
max letScratch <|
max (exprScratch bodyDone) (bodyValues.foldl (fun n value => max n (exprScratch value)) 0)
2 + resultWidth + max initScratch bodyScratch
| .ite cond thenStmt elseStmt =>
max (condScratch cond) (max (stmtScratch thenStmt) (stmtScratch elseStmt))
| .seq first second => max (stmtScratch first) (stmtScratch second)
| .while cond body => max (condScratch cond) (stmtScratch body)
def funcScratch (func : Func) : Nat :=