-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStorage.lean
More file actions
708 lines (657 loc) · 31.2 KB
/
Copy pathStorage.lean
File metadata and controls
708 lines (657 loc) · 31.2 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
import Lean
import LeanExe.Extract.Values
import LeanExe.IR.Core
open Lean
namespace LeanExe.Extract.Core
mutual
partial def heapLoadValueAt (ptr : IRExpr) : Ty → Nat → Except String (ExtractedValue × Nat)
| .unit, slot => .ok (.scalar (.heapLoadSlot ptr slot), slot + 1)
| .bool, slot => .ok (.scalar (.heapLoadSlot ptr slot), slot + 1)
| .u8, slot => .ok (.scalar (.heapLoadSlot ptr slot), slot + 1)
| .u32, slot => .ok (.scalar (.heapLoadSlot ptr slot), slot + 1)
| .u64, slot => .ok (.scalar (.heapLoadSlot ptr slot), slot + 1)
| .nat, slot => .ok (.scalar (.heapLoadSlot ptr slot), slot + 1)
| .array item, slot =>
if supportedArrayElementType item then
.ok
(.array
(.heapLoadSlot ptr slot)
(.heapLoadSlot ptr (slot + 1)),
slot + 2)
else
.error s!"unsupported heap field array type: {reprStr ((.array item : Ty))}"
| .byteArray, slot =>
.ok
(.byteArray
(.heapLoadSlot ptr slot)
(.heapLoadSlot ptr (slot + 1))
(.heapLoadSlot ptr (slot + 2)),
slot + 3)
| .product left right, slot => do
let leftLoaded ← heapLoadValueAt ptr left slot
let rightLoaded ← heapLoadValueAt ptr right leftLoaded.snd
.ok (.product leftLoaded.fst rightLoaded.fst, rightLoaded.snd)
| .sum left right, slot => do
let leftLoaded ← heapLoadValueAt ptr left (slot + 1)
let rightLoaded ← heapLoadValueAt ptr right leftLoaded.snd
.ok (.sum (.heapLoadSlot ptr slot) leftLoaded.fst rightLoaded.fst, rightLoaded.snd)
| .struct name _ fields, slot => do
let loaded ← heapLoadFieldsAt ptr fields slot
.ok (.struct name loaded.fst, loaded.snd)
| .variant name _ ctors, slot => do
let loaded ← heapLoadCtorsAt ptr ctors (slot + 1)
.ok (.variant name (.heapLoadSlot ptr slot) loaded.fst, loaded.snd)
| .recVariant name _, slot => .ok (.heapVariant name (.heapLoadSlot ptr slot), slot + 1)
partial def heapLoadFieldsAt (ptr : IRExpr) :
List Ty → Nat → Except String (List ExtractedValue × Nat)
| [], slot => .ok ([], slot)
| field :: rest, slot => do
let head ← heapLoadValueAt ptr field slot
let tail ← heapLoadFieldsAt ptr rest head.snd
.ok (head.fst :: tail.fst, tail.snd)
partial def heapLoadCtorsAt (ptr : IRExpr) :
List (List Ty) → Nat → Except String (List (List ExtractedValue) × Nat)
| [], slot => .ok ([], slot)
| fields :: rest, slot => do
let head ← heapLoadFieldsAt ptr fields slot
let tail ← heapLoadCtorsAt ptr rest head.snd
.ok (head.fst :: tail.fst, tail.snd)
end
partial def flattenFieldsFromKinds
(typeName : Name)
(fieldKinds : List (Option Ty))
(runtimeFields : List ExtractedValue) :
Except String (List IRExpr) := do
let rec loop :
List (Option Ty) → List ExtractedValue → List (List IRExpr) →
Except String (List IRExpr)
| [], [], acc => .ok acc.reverse.flatten
| [], _ :: _, _ => .error s!"too many runtime fields for {typeName}"
| some ty :: restKinds, field :: restFields, acc => do
let flattened ← flattenInternalValue ty field
loop restKinds restFields (flattened :: acc)
| some _ :: _, [], _ => .error s!"too few runtime fields for {typeName}"
| none :: restKinds, fields, acc =>
loop restKinds fields acc
loop fieldKinds runtimeFields []
partial def defaultCtorSlotValues (ctors : List VariantCtorLayout) :
Except String (List (List IRExpr)) :=
ctors.mapM fun ctor => do
let defaults ← runtimeTypesFromKinds ctor.fields |>.mapM defaultValue
flattenFieldsFromKinds ctor.name ctor.fields defaults
partial def heapRuntimeFieldsFromKinds
(ptr : IRExpr)
(fieldKinds : List (Option Ty))
(slot : Nat) :
Except String (List ExtractedValue × Nat) :=
let rec loop :
List (Option Ty) → Nat → List ExtractedValue →
Except String (List ExtractedValue × Nat)
| [], next, acc => .ok (acc.reverse, next)
| some ty :: rest, next, acc => do
let loaded ← heapLoadValueAt ptr ty next
loop rest loaded.snd (loaded.fst :: acc)
| none :: rest, next, acc =>
loop rest next acc
loop fieldKinds slot []
partial def flattenArrayElementValue
(ty : Ty)
(value : ExtractedValue)
(summaries : Array (List Nat) := #[]) :
Except String (List IRExpr) :=
match ty with
| .unit => scalarValue value |>.map (fun expr => [expr])
| .bool => scalarValue value |>.map (fun expr => [expr])
| .u8 => scalarValue value |>.map (fun expr => [expr])
| .u32 => scalarValue value |>.map (fun expr => [expr])
| .u64 => scalarValue value |>.map (fun expr => [expr])
| .nat => scalarValue value |>.map (fun expr => [expr])
| .byteArray => do
let parts ← byteArrayFullParts value
.ok [parts.owner, parts.ptr, parts.len]
| .array item =>
if supportedArrayElementType item then do
let parts ← arrayFullParts value
.ok [parts.owner, parts.ptr]
else
.error s!"unsupported array element value type: {reprStr ((.array item : Ty))}"
| .recVariant name _ =>
match value with
| .heapVariant actual ptr =>
if actual == name then
.ok [ptr]
else
.error s!"recursive inductive array element shape mismatch: {name}"
| .recursiveVariant actual tag ctors =>
if actual == name then do
let hoisted ← hoistCtorFieldWrappers
(fun ty value sources => flattenInternalValue ty value summaries sources)
summaries ctors []
let values := tag :: hoisted.2.1
let childMask := heapChildMaskForCtors ctors
let ownedMask :=
if hoisted.1.isEmpty then
ownedChildMaskForSlotsWithSummaries summaries childMask values
else
ownedChildMaskForSlotsWithOwnerSourcesForAlloc summaries childMask hoisted.2.2 values
.ok [rewrapValueWrappers hoisted.1 (.heapAllocSlots childMask ownedMask values)]
else
.error s!"recursive inductive array element shape mismatch: {name}"
| .letE slot value body => do
let flattened ← flattenArrayElementValue ty body summaries
.ok (flattened.map (fun expr => .letE slot value expr))
| .letCall slots index args body => do
let flattened ← flattenArrayElementValue ty body summaries
.ok (flattened.map (fun expr => .letCall slots index args expr))
| .ite cond thenValue elseValue => do
combineIteSlots cond
(← flattenArrayElementValue ty thenValue summaries)
(← flattenArrayElementValue ty elseValue summaries)
| _ =>
.error s!"non-recursive value used where recursive inductive array element is required: {name}"
| .product left right =>
match value with
| .product leftValue rightValue => do
let leftSlots ← flattenArrayElementValue left leftValue summaries
let rightSlots ← flattenArrayElementValue right rightValue summaries
.ok (leftSlots ++ rightSlots)
| .letE slot value body => do
let flattened ← flattenArrayElementValue ty body summaries
.ok (flattened.map (fun expr => .letE slot value expr))
| .letCall slots index args body => do
let flattened ← flattenArrayElementValue ty body summaries
.ok (flattened.map (fun expr => .letCall slots index args expr))
| .ite cond thenValue elseValue => do
combineIteSlots cond
(← flattenArrayElementValue ty thenValue summaries)
(← flattenArrayElementValue ty elseValue summaries)
| _ => .error "non-product value used where product array element is required"
| .sum left right =>
match value with
| .sum tag leftValue rightValue => do
let leftSlots ← flattenArrayElementValue left leftValue summaries
let rightSlots ← flattenArrayElementValue right rightValue summaries
.ok (tag :: leftSlots ++ rightSlots)
| .letE slot value body => do
let flattened ← flattenArrayElementValue ty body summaries
.ok (flattened.map (fun expr => .letE slot value expr))
| .letCall slots index args body => do
let flattened ← flattenArrayElementValue ty body summaries
.ok (flattened.map (fun expr => .letCall slots index args expr))
| .ite cond thenValue elseValue => do
combineIteSlots cond
(← flattenArrayElementValue ty thenValue summaries)
(← flattenArrayElementValue ty elseValue summaries)
| _ => .error "non-sum value used where sum array element is required"
| .struct name _ fields =>
match value with
| .struct actual values =>
if actual == name && values.length == fields.length then do
let flattened ← (fields.zip values).mapM fun item =>
flattenArrayElementValue item.fst item.snd summaries
.ok flattened.flatten
else
.error s!"structure array element shape mismatch: {name}"
| .letE slot value body => do
let flattened ← flattenArrayElementValue ty body summaries
.ok (flattened.map (fun expr => .letE slot value expr))
| .letCall slots index args body => do
let flattened ← flattenArrayElementValue ty body summaries
.ok (flattened.map (fun expr => .letCall slots index args expr))
| .ite cond thenValue elseValue => do
combineIteSlots cond
(← flattenArrayElementValue ty thenValue summaries)
(← flattenArrayElementValue ty elseValue summaries)
| _ =>
.error s!"non-structure value used where structure array element is required: {name}"
| .variant name _ ctors =>
match value with
| .variant actual tag values =>
if actual == name && values.length == ctors.length then do
let flattened ← (ctors.zip values).mapM fun ctorPair =>
if ctorPair.fst.length == ctorPair.snd.length then do
let fields ← (ctorPair.fst.zip ctorPair.snd).mapM (fun item =>
flattenArrayElementValue item.fst item.snd summaries)
.ok fields.flatten
else
.error s!"inductive array element payload shape mismatch: {name}"
.ok (tag :: flattened.flatten)
else
.error s!"inductive array element shape mismatch: {name}"
| .letE slot value body => do
let flattened ← flattenArrayElementValue ty body summaries
.ok (flattened.map (fun expr => .letE slot value expr))
| .letCall slots index args body => do
let flattened ← flattenArrayElementValue ty body summaries
.ok (flattened.map (fun expr => .letCall slots index args expr))
| .ite cond thenValue elseValue => do
combineIteSlots cond
(← flattenArrayElementValue ty thenValue summaries)
(← flattenArrayElementValue ty elseValue summaries)
| _ => .error s!"non-inductive value used where inductive array element is required: {name}"
partial def materializeStrictSlotsWith
(flatten : ExtractedValue → Except String (List IRExpr))
(value : ExtractedValue)
(nextLocal : Nat) :
Except String StrictSlots := do
match value with
| .letE slot expr body => do
let result ← materializeStrictSlotsWith flatten body nextLocal
.ok { result with lets := .expr slot expr :: result.lets }
| .letCall slots index args body => do
let result ← materializeStrictSlotsWith flatten body nextLocal
.ok { result with lets := .call slots index args :: result.lets }
| .letLocal lets body => do
let result ← materializeStrictSlotsWith flatten body nextLocal
.ok { result with lets := .locals lets :: result.lets }
| _ =>
.ok { lets := [], slots := ← flatten value, nextLocal := nextLocal }
def materializeStrictInternalSlots
(ty : Ty)
(value : ExtractedValue)
(nextLocal : Nat) :
Except String StrictSlots :=
materializeStrictSlotsWith (flattenInternalValue ty) value nextLocal
def materializeStrictInternalSlotsWithSummaries
(summaries : Array (List Nat))
(ty : Ty)
(value : ExtractedValue)
(nextLocal : Nat) :
Except String StrictSlots :=
materializeStrictSlotsWith (fun value => flattenInternalValue ty value summaries) value nextLocal
def materializeStrictArrayElementSlots
(ty : Ty)
(value : ExtractedValue)
(nextLocal : Nat) :
Except String StrictSlots :=
materializeStrictSlotsWith (flattenArrayElementValue ty) value nextLocal
def materializeStrictArrayElementSlotsWithSummaries
(summaries : Array (List Nat))
(ty : Ty)
(value : ExtractedValue)
(nextLocal : Nat) :
Except String StrictSlots :=
materializeStrictSlotsWith (fun value => flattenArrayElementValue ty value summaries) value
nextLocal
mutual
partial def arrayLoadValueAt
(width : Nat)
(array index : IRExpr) :
Ty → Nat → Except String (ExtractedValue × Nat)
| .unit, slot => .ok (.scalar (.arrayGetSlot width slot array index), slot + 1)
| .bool, slot => .ok (.scalar (.arrayGetSlot width slot array index), slot + 1)
| .u8, slot => .ok (.scalar (.arrayGetSlot width slot array index), slot + 1)
| .u32, slot => .ok (.scalar (.arrayGetSlot width slot array index), slot + 1)
| .u64, slot => .ok (.scalar (.arrayGetSlot width slot array index), slot + 1)
| .nat, slot => .ok (.scalar (.arrayGetSlot width slot array index), slot + 1)
| .byteArray, slot =>
.ok
(.byteArray
(.arrayGetSlot width slot array index)
(.arrayGetSlot width (slot + 1) array index)
(.arrayGetSlot width (slot + 2) array index),
slot + 3)
| .array item, slot =>
if supportedArrayElementType item then
.ok
(.array
(.arrayGetSlot width slot array index)
(.arrayGetSlot width (slot + 1) array index),
slot + 2)
else
.error s!"unsupported array element load type: {reprStr ((.array item : Ty))}"
| .recVariant name _, slot =>
.ok (.heapVariant name (.arrayGetSlot width slot array index), slot + 1)
| .product left right, slot => do
let leftLoaded ← arrayLoadValueAt width array index left slot
let rightLoaded ← arrayLoadValueAt width array index right leftLoaded.snd
.ok (.product leftLoaded.fst rightLoaded.fst, rightLoaded.snd)
| .sum left right, slot => do
let leftLoaded ← arrayLoadValueAt width array index left (slot + 1)
let rightLoaded ← arrayLoadValueAt width array index right leftLoaded.snd
.ok (.sum (.arrayGetSlot width slot array index) leftLoaded.fst rightLoaded.fst,
rightLoaded.snd)
| .struct name _ fields, slot => do
let result ← arrayLoadFieldsAt width array index fields slot
.ok (.struct name result.fst, result.snd)
| .variant name _ ctors, slot => do
let tag := .arrayGetSlot width slot array index
let result ← arrayLoadCtorsAt width array index ctors (slot + 1)
.ok (.variant name tag result.fst, result.snd)
partial def arrayLoadFieldsAt
(width : Nat)
(array index : IRExpr) :
List Ty → Nat → Except String (List ExtractedValue × Nat)
| [], slot => .ok ([], slot)
| field :: rest, slot => do
let head ← arrayLoadValueAt width array index field slot
let tail ← arrayLoadFieldsAt width array index rest head.snd
.ok (head.fst :: tail.fst, tail.snd)
partial def arrayLoadCtorsAt
(width : Nat)
(array index : IRExpr) :
List (List Ty) → Nat → Except String (List (List ExtractedValue) × Nat)
| [], slot => .ok ([], slot)
| fields :: rest, slot => do
let head ← arrayLoadFieldsAt width array index fields slot
let tail ← arrayLoadCtorsAt width array index rest head.snd
.ok (head.fst :: tail.fst, tail.snd)
end
def arrayLoadValue
(itemTy : Ty)
(array index : IRExpr) :
Except String ExtractedValue := do
let width ←
match arrayElementSlots? itemTy with
| some width => .ok width
| none => .error s!"unsupported array element type: {reprStr itemTy}"
let loaded ← arrayLoadValueAt width array index itemTy 0
.ok loaded.fst
mutual
partial def arrayFindValueAt
(width : Nat)
(array : IRExpr)
(itemStart : Nat)
(predicate : IRExpr) :
Ty → Nat → Except String (ExtractedValue × Nat)
| .unit, slot => .ok (.scalar (.arrayFindSlot width array itemStart predicate slot), slot + 1)
| .bool, slot => .ok (.scalar (.arrayFindSlot width array itemStart predicate slot), slot + 1)
| .u8, slot => .ok (.scalar (.arrayFindSlot width array itemStart predicate slot), slot + 1)
| .u32, slot => .ok (.scalar (.arrayFindSlot width array itemStart predicate slot), slot + 1)
| .u64, slot => .ok (.scalar (.arrayFindSlot width array itemStart predicate slot), slot + 1)
| .nat, slot => .ok (.scalar (.arrayFindSlot width array itemStart predicate slot), slot + 1)
| .byteArray, slot =>
.ok
(.byteArray
(.arrayFindSlot width array itemStart predicate slot)
(.arrayFindSlot width array itemStart predicate (slot + 1))
(.arrayFindSlot width array itemStart predicate (slot + 2)),
slot + 3)
| .array item, slot =>
if supportedArrayElementType item then
.ok
(.array
(.arrayFindSlot width array itemStart predicate slot)
(.arrayFindSlot width array itemStart predicate (slot + 1)),
slot + 2)
else
.error s!"unsupported array find element type: {reprStr ((.array item : Ty))}"
| .recVariant name _, slot =>
.ok (.heapVariant name (.arrayFindSlot width array itemStart predicate slot), slot + 1)
| .product left right, slot => do
let leftLoaded ← arrayFindValueAt width array itemStart predicate left slot
let rightLoaded ← arrayFindValueAt width array itemStart predicate right leftLoaded.snd
.ok (.product leftLoaded.fst rightLoaded.fst, rightLoaded.snd)
| .sum left right, slot => do
let leftLoaded ← arrayFindValueAt width array itemStart predicate left (slot + 1)
let rightLoaded ← arrayFindValueAt width array itemStart predicate right leftLoaded.snd
.ok (.sum (.arrayFindSlot width array itemStart predicate slot) leftLoaded.fst
rightLoaded.fst, rightLoaded.snd)
| .struct name _ fields, slot => do
let result ← arrayFindFieldsAt width array itemStart predicate fields slot
.ok (.struct name result.fst, result.snd)
| .variant name _ ctors, slot => do
let tag := .arrayFindSlot width array itemStart predicate slot
let result ← arrayFindCtorsAt width array itemStart predicate ctors (slot + 1)
.ok (.variant name tag result.fst, result.snd)
partial def arrayFindFieldsAt
(width : Nat)
(array : IRExpr)
(itemStart : Nat)
(predicate : IRExpr) :
List Ty → Nat → Except String (List ExtractedValue × Nat)
| [], slot => .ok ([], slot)
| field :: rest, slot => do
let head ← arrayFindValueAt width array itemStart predicate field slot
let tail ← arrayFindFieldsAt width array itemStart predicate rest head.snd
.ok (head.fst :: tail.fst, tail.snd)
partial def arrayFindCtorsAt
(width : Nat)
(array : IRExpr)
(itemStart : Nat)
(predicate : IRExpr) :
List (List Ty) → Nat → Except String (List (List ExtractedValue) × Nat)
| [], slot => .ok ([], slot)
| fields :: rest, slot => do
let head ← arrayFindFieldsAt width array itemStart predicate fields slot
let tail ← arrayFindCtorsAt width array itemStart predicate rest head.snd
.ok (head.fst :: tail.fst, tail.snd)
end
def arrayFindValue
(itemTy : Ty)
(width : Nat)
(array : IRExpr)
(itemStart : Nat)
(predicate : IRExpr) :
Except String ExtractedValue := do
let loaded ← arrayFindValueAt width array itemStart predicate itemTy 0
.ok loaded.fst
mutual
partial def arrayLocalValueAt (start : Nat) :
Ty → Nat → Except String (ExtractedValue × Nat)
| .unit, slot => .ok (.scalar (.local (start + slot)), slot + 1)
| .bool, slot => .ok (.scalar (.local (start + slot)), slot + 1)
| .u8, slot => .ok (.scalar (.local (start + slot)), slot + 1)
| .u32, slot => .ok (.scalar (.local (start + slot)), slot + 1)
| .u64, slot => .ok (.scalar (.local (start + slot)), slot + 1)
| .nat, slot => .ok (.scalar (.local (start + slot)), slot + 1)
| .byteArray, slot =>
.ok
(.byteArray
(.local (start + slot))
(.local (start + slot + 1))
(.local (start + slot + 2)),
slot + 3)
| .array item, slot =>
if supportedArrayElementType item then
.ok (.array (.local (start + slot)) (.local (start + slot + 1)), slot + 2)
else
.error s!"unsupported array local element type: {reprStr ((.array item : Ty))}"
| .recVariant name _, slot => .ok (.heapVariant name (.local (start + slot)), slot + 1)
| .product left right, slot => do
let leftLoaded ← arrayLocalValueAt start left slot
let rightLoaded ← arrayLocalValueAt start right leftLoaded.snd
.ok (.product leftLoaded.fst rightLoaded.fst, rightLoaded.snd)
| .sum left right, slot => do
let leftLoaded ← arrayLocalValueAt start left (slot + 1)
let rightLoaded ← arrayLocalValueAt start right leftLoaded.snd
.ok (.sum (.local (start + slot)) leftLoaded.fst rightLoaded.fst, rightLoaded.snd)
| .struct name _ fields, slot => do
let result ← arrayLocalFieldsAt start fields slot
.ok (.struct name result.fst, result.snd)
| .variant name _ ctors, slot => do
let tag := .local (start + slot)
let result ← arrayLocalCtorsAt start ctors (slot + 1)
.ok (.variant name tag result.fst, result.snd)
partial def arrayLocalFieldsAt (start : Nat) :
List Ty → Nat → Except String (List ExtractedValue × Nat)
| [], slot => .ok ([], slot)
| field :: rest, slot => do
let head ← arrayLocalValueAt start field slot
let tail ← arrayLocalFieldsAt start rest head.snd
.ok (head.fst :: tail.fst, tail.snd)
partial def arrayLocalCtorsAt (start : Nat) :
List (List Ty) → Nat → Except String (List (List ExtractedValue) × Nat)
| [], slot => .ok ([], slot)
| fields :: rest, slot => do
let head ← arrayLocalFieldsAt start fields slot
let tail ← arrayLocalCtorsAt start rest head.snd
.ok (head.fst :: tail.fst, tail.snd)
end
def arrayLocalValue (itemTy : Ty) (start : Nat) : Except String ExtractedValue := do
let loaded ← arrayLocalValueAt start itemTy 0
.ok loaded.fst
mutual
partial def valueFromInternalSlotsAt (slotExpr : Nat → IRExpr) :
Ty → Nat → ExtractedValue × Nat
| .array item, slot =>
if supportedArrayElementType item then
(.array (slotExpr slot) (slotExpr (slot + 1)), slot + 2)
else
(.scalar .trap, slot + 1)
| .byteArray, slot =>
(.byteArray (slotExpr slot) (slotExpr (slot + 1)) (slotExpr (slot + 2)), slot + 3)
| .product left right, slot =>
let leftValue := valueFromInternalSlotsAt slotExpr left slot
let rightValue := valueFromInternalSlotsAt slotExpr right leftValue.snd
(.product leftValue.fst rightValue.fst, rightValue.snd)
| .sum left right, slot =>
let leftValue := valueFromInternalSlotsAt slotExpr left (slot + 1)
let rightValue := valueFromInternalSlotsAt slotExpr right leftValue.snd
(.sum (slotExpr slot) leftValue.fst rightValue.fst, rightValue.snd)
| .struct name _ fields, slot =>
let fieldsValue := valuesFromInternalSlotsAt slotExpr fields slot
(.struct name fieldsValue.fst, fieldsValue.snd)
| .variant name _ ctors, slot =>
let ctorsValue := ctorValuesFromInternalSlotsAt slotExpr ctors (slot + 1)
(.variant name (slotExpr slot) ctorsValue.fst, ctorsValue.snd)
| .recVariant name _, slot =>
(.heapVariant name (slotExpr slot), slot + 1)
| _, slot =>
(.scalar (slotExpr slot), slot + 1)
partial def valuesFromInternalSlotsAt (slotExpr : Nat → IRExpr) :
List Ty → Nat → List ExtractedValue × Nat
| [], slot => ([], slot)
| ty :: rest, slot =>
let head := valueFromInternalSlotsAt slotExpr ty slot
let tail := valuesFromInternalSlotsAt slotExpr rest head.snd
(head.fst :: tail.fst, tail.snd)
partial def ctorValuesFromInternalSlotsAt (slotExpr : Nat → IRExpr) :
List (List Ty) → Nat → List (List ExtractedValue) × Nat
| [], slot => ([], slot)
| fields :: rest, slot =>
let head := valuesFromInternalSlotsAt slotExpr fields slot
let tail := ctorValuesFromInternalSlotsAt slotExpr rest head.snd
(head.fst :: tail.fst, tail.snd)
end
def valueFromInternalSlots (ty : Ty) (slotExpr : Nat → IRExpr) : ExtractedValue :=
(valueFromInternalSlotsAt slotExpr ty 0).fst
def arrayElementWidth (context : String) (itemTy : Ty) : Except String Nat :=
match arrayElementSlots? itemTy with
| some width => .ok width
| none => .error s!"unsupported {context} item type: {reprStr itemTy}"
mutual
partial def extractedValueForParam (slot : Nat) : Ty → ExtractedValue
| .u8 => .scalar (u8WrapExpr (.local slot))
| .u32 => .scalar (u32WrapExpr (.local slot))
| .byteArray => .byteArray (.u64 0) (.local slot) (.local (slot + 1))
| .array item =>
if supportedArrayElementType item then
.array (.u64 0) (.local slot)
else
.scalar .trap
| .sum left right =>
.sum (.local slot)
(extractedValueForParam (slot + 1) left)
(extractedValueForParam (slot + 1 + abiSlots left) right)
| .struct name _ fields => .struct name (extractedStructFieldsForParam slot fields)
| .variant name _ ctors =>
.variant name (.local slot) (extractedVariantCtorsForParam (slot + 1) ctors)
| .recVariant name _ => .heapVariant name (.local slot)
| _ => .scalar (.local slot)
partial def extractedStructFieldsForParam (slot : Nat) : List Ty → List ExtractedValue
| [] => []
| ty :: rest =>
extractedValueForParam slot ty :: extractedStructFieldsForParam (slot + abiSlots ty) rest
partial def extractedVariantCtorsForParam (slot : Nat) :
List (List Ty) → List (List ExtractedValue)
| [] => []
| fields :: rest =>
extractedStructFieldsForParam slot fields ::
extractedVariantCtorsForParam
(slot + fields.foldl (fun total field => total + abiSlots field) 0)
rest
end
def bindingForParam (slot : Nat) : Ty → Binding
| .u8 => .value (.scalar (u8WrapExpr (.local slot)))
| .u32 => .value (.scalar (u32WrapExpr (.local slot)))
| .byteArray => .value (.byteArray (.u64 0) (.local slot) (.local (slot + 1)))
| .array item =>
if supportedArrayElementType item then
.value (.array (.u64 0) (.local slot))
else
.value (.scalar .trap)
| .sum left right =>
.value
(.sum (.local slot)
(extractedValueForParam (slot + 1) left)
(extractedValueForParam (slot + 1 + abiSlots left) right))
| .struct name _ fields => .value (.struct name (extractedStructFieldsForParam slot fields))
| .variant name _ ctors =>
.value (.variant name (.local slot) (extractedVariantCtorsForParam (slot + 1) ctors))
| .recVariant name _ => .value (.heapVariant name (.local slot))
| _ => .slot slot
def bindingForInternalParam (slot : Nat) (ty : Ty) : Binding :=
.value (valueFromInternalSlots ty fun offset => .local (slot + offset))
partial def sourceParamBindingsFrom (slot : Nat) : List Ty → List Binding
| [] => []
| ty :: rest => bindingForParam slot ty :: sourceParamBindingsFrom (slot + abiSlots ty) rest
def sourceParamBindings (params : List Ty) : List Binding :=
sourceParamBindingsFrom 0 params
partial def internalParamBindingsFrom (slot : Nat) : List Ty → List Binding
| [] => []
| ty :: rest =>
bindingForInternalParam slot ty :: internalParamBindingsFrom (slot + internalSlots ty) rest
def internalParamBindings (params : List Ty) : List Binding :=
internalParamBindingsFrom 0 params
def functionParamBindings (useAbi : Bool) (params : List Ty) : List Binding :=
if useAbi then sourceParamBindings params else internalParamBindings params
partial def abiTargetsFrom (slot : Nat) : List Ty → List (Ty × List Nat)
| [] => []
| ty :: rest =>
let slots := (List.range (abiSlots ty)).map (fun offset => slot + offset)
(ty, slots) :: abiTargetsFrom (slot + abiSlots ty) rest
def abiTargets (params : List Ty) : List (Ty × List Nat) :=
abiTargetsFrom 0 params
partial def internalTargetsFrom (slot : Nat) : List Ty → List (Ty × List Nat)
| [] => []
| ty :: rest =>
let slots := (List.range (internalSlots ty)).map (fun offset => slot + offset)
(ty, slots) :: internalTargetsFrom (slot + internalSlots ty) rest
def internalTargets (params : List Ty) : List (Ty × List Nat) :=
internalTargetsFrom 0 params
def functionParamTargets (useAbi : Bool) (params : List Ty) : List (Ty × List Nat) :=
if useAbi then abiTargets params else internalTargets params
def sourceFieldBindingsFromKinds
(typeName : Name)
(fieldKinds : List (Option Ty))
(runtimeFields : List ExtractedValue) :
Except String (List Binding) := do
let rec loop :
List (Option Ty) → List ExtractedValue → List Binding →
Except String (List Binding)
| [], [], acc => .ok acc.reverse
| [], _ :: _, _ => .error s!"too many runtime fields for {typeName}"
| some _ :: restKinds, field :: restFields, acc =>
loop restKinds restFields (.value field :: acc)
| some _ :: _, [], _ => .error s!"too few runtime fields for {typeName}"
| none :: restKinds, fields, acc =>
loop restKinds fields (.value (.scalar (.u64 0)) :: acc)
loop fieldKinds runtimeFields []
partial def defaultCtorValues (ctors : List VariantCtorLayout) :
Except String (List (List ExtractedValue)) :=
ctors.mapM fun ctor => runtimeTypesFromKinds ctor.fields |>.mapM defaultValue
def typedFieldsFromKinds
(typeName : Name)
(fieldKinds : List (Option Ty))
(runtimeFields : List ExtractedValue) :
Except String (List (Ty × ExtractedValue)) := do
let rec loop :
List (Option Ty) → List ExtractedValue → List (Ty × ExtractedValue) →
Except String (List (Ty × ExtractedValue))
| [], [], acc => .ok acc.reverse
| [], _ :: _, _ => .error s!"too many runtime fields for {typeName}"
| some ty :: restKinds, field :: restFields, acc =>
loop restKinds restFields ((ty, field) :: acc)
| some _ :: _, [], _ => .error s!"too few runtime fields for {typeName}"
| none :: restKinds, fields, acc =>
loop restKinds fields acc
loop fieldKinds runtimeFields []
partial def defaultCtorTypedValues (ctors : List VariantCtorLayout) :
Except String (List (List (Ty × ExtractedValue))) :=
ctors.mapM fun ctor => do
let values ← runtimeTypesFromKinds ctor.fields |>.mapM fun ty => do
let value ← defaultValue ty
.ok (ty, value)
.ok values
end LeanExe.Extract.Core