rune/compile/v1/
needs.rs

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
use core::fmt;
use core::mem::replace;

use crate::ast::Spanned;
use crate::compile;
use crate::runtime::{Inst, InstAddress, Output};
use crate::shared::{rune_diagnose, Backtrace};

use super::{Ctxt, DisplayNamed, ScopeId, Scopes};

/// Trait used to abstract interactions over different needs.
pub(super) trait Needs<'a, 'hir> {
    /// Access the span for the needs.
    fn span(&self) -> &'hir dyn Spanned;

    /// Get output of the needs or error.
    fn output(&self) -> compile::Result<Output>;

    /// Get the need as an output.
    ///
    /// Returns `None` if `Needs::None` is set.
    fn try_alloc_output(&mut self) -> compile::Result<Option<Output>>;

    fn assign_addr(
        &mut self,
        cx: &mut Ctxt<'_, 'hir, '_>,
        from: InstAddress,
    ) -> compile::Result<()>;

    /// Allocate an output falling back to discarding if one is not available.
    fn alloc_output(&mut self) -> compile::Result<Output>;

    /// Try to allocate an address from this needs.
    fn try_alloc_addr(&mut self) -> compile::Result<Option<&mut Address<'a, 'hir>>>;

    /// Get the need as an address without trying to allocate it if it's
    /// missing.
    fn try_as_addr(&self) -> compile::Result<Option<&Address<'a, 'hir>>>;

    /// Get the populated address of the need.
    fn addr(&self) -> compile::Result<&Address<'a, 'hir>> {
        let Some(addr) = self.try_as_addr()? else {
            return Err(compile::Error::msg(
                self.span(),
                "Expected need to be populated",
            ));
        };

        Ok(addr)
    }
}

impl<'a, 'hir> Needs<'a, 'hir> for Any<'a, 'hir> {
    #[inline]
    fn span(&self) -> &'hir dyn Spanned {
        self.span
    }

    #[inline]
    fn output(&self) -> compile::Result<Output> {
        Any::output(self)
    }

    #[inline]
    fn assign_addr(
        &mut self,
        cx: &mut Ctxt<'_, 'hir, '_>,
        from: InstAddress,
    ) -> compile::Result<()> {
        Any::assign_addr(self, cx, from)
    }

    #[inline]
    fn alloc_output(&mut self) -> compile::Result<Output> {
        Any::alloc_output(self)
    }

    #[inline]
    fn try_alloc_output(&mut self) -> compile::Result<Option<Output>> {
        Any::try_alloc_output(self)
    }

    #[inline]
    fn try_alloc_addr(&mut self) -> compile::Result<Option<&mut Address<'a, 'hir>>> {
        Any::try_alloc_addr(self)
    }

    #[inline]
    fn try_as_addr(&self) -> compile::Result<Option<&Address<'a, 'hir>>> {
        Any::try_as_addr(self)
    }
}

impl<'a, 'hir> Needs<'a, 'hir> for Address<'a, 'hir> {
    #[inline]
    fn span(&self) -> &'hir dyn Spanned {
        self.span
    }

    #[inline]
    fn output(&self) -> compile::Result<Output> {
        Ok(Address::output(self))
    }

    #[inline]
    fn assign_addr(
        &mut self,
        cx: &mut Ctxt<'_, 'hir, '_>,
        from: InstAddress,
    ) -> compile::Result<()> {
        Address::assign_addr(self, cx, from)
    }

    #[inline]
    fn alloc_output(&mut self) -> compile::Result<Output> {
        Address::alloc_output(self)
    }

    #[inline]
    fn try_alloc_output(&mut self) -> compile::Result<Option<Output>> {
        Ok(Some(Address::alloc_output(self)?))
    }

    #[inline]
    fn try_alloc_addr(&mut self) -> compile::Result<Option<&mut Address<'a, 'hir>>> {
        Ok(Some(Address::alloc_addr(self)?))
    }

    #[inline]
    fn try_as_addr(&self) -> compile::Result<Option<&Address<'a, 'hir>>> {
        Ok(Some(self))
    }
}

#[derive(Clone, Copy)]
pub(super) enum AddressKind {
    /// The value is locally allocated and should be freed in the immediate
    /// scope.
    Local,
    /// The slot has been reserved, but has not been assigned to anything yet.
    Dangling,
    /// The address is assigned from elsewhere and *should not* be touched.
    Assigned,
    /// The address is allocated on behalf of the given scope, and we should
    /// defer deallocating it until the given scope is deallocated.
    Scope(ScopeId),
    /// The address has been freed.
    Freed,
}

impl fmt::Display for AddressKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            AddressKind::Local => write!(f, "local"),
            AddressKind::Dangling => write!(f, "dangling"),
            AddressKind::Assigned => write!(f, "assigned"),
            AddressKind::Scope(scope) => write!(f, "scope({scope})"),
            AddressKind::Freed => write!(f, "freed"),
        }
    }
}

#[must_use = "An allocated address must be freed"]
pub(super) struct Address<'a, 'hir> {
    span: &'hir dyn Spanned,
    scopes: &'a Scopes<'hir>,
    address: InstAddress,
    kind: AddressKind,
    /// A diagnostical name for the address.
    name: Option<&'static str>,
    #[cfg_attr(not(feature = "tracing"), allow(unused))]
    backtrace: Backtrace,
}

impl<'a, 'hir> Address<'a, 'hir> {
    /// A locally allocated address.
    #[inline]
    #[track_caller]
    pub(super) fn local(
        span: &'hir dyn Spanned,
        scopes: &'a Scopes<'hir>,
        addr: InstAddress,
    ) -> Self {
        Self {
            span,
            scopes,
            address: addr,
            kind: AddressKind::Local,
            name: None,
            backtrace: Backtrace::capture(),
        }
    }

    /// A locally assigned address.
    #[inline]
    #[track_caller]
    pub(super) fn assigned(
        span: &'hir dyn Spanned,
        scopes: &'a Scopes<'hir>,
        addr: InstAddress,
    ) -> Self {
        Self {
            span,
            scopes,
            address: addr,
            kind: AddressKind::Assigned,
            name: None,
            backtrace: Backtrace::capture(),
        }
    }

    /// A locally reserved address.
    #[inline]
    #[track_caller]
    pub(super) fn dangling(
        span: &'hir dyn Spanned,
        scopes: &'a Scopes<'hir>,
        addr: InstAddress,
    ) -> Self {
        Self {
            span,
            scopes,
            address: addr,
            kind: AddressKind::Dangling,
            name: None,
            backtrace: Backtrace::capture(),
        }
    }

    /// Assign a name to the address.
    #[inline]
    pub(super) fn with_name(mut self, name: &'static str) -> Self {
        self.name = Some(name);
        self
    }

    #[inline]
    pub(super) fn addr(&self) -> InstAddress {
        self.address
    }

    #[inline]
    pub(super) fn alloc_addr(&mut self) -> compile::Result<&mut Self> {
        if matches!(self.kind, AddressKind::Dangling) {
            self.kind = AddressKind::Local;
        }

        Ok(self)
    }

    #[inline]
    pub(super) fn alloc_output(&mut self) -> compile::Result<Output> {
        Ok(self.alloc_addr()?.output())
    }

    #[inline]
    pub(super) fn output(&self) -> Output {
        self.address.output()
    }

    pub(super) fn assign_addr(
        &self,
        cx: &mut Ctxt<'_, '_, '_>,
        from: InstAddress,
    ) -> compile::Result<()> {
        if from != self.address {
            cx.asm.push(
                Inst::Copy {
                    addr: from,
                    out: self.address.output(),
                },
                self.span,
            )?;
        }

        Ok(())
    }

    /// Forget the current address.
    ///
    /// This will be freed when the scope it's associated with is freed.
    pub(super) fn forget(mut self) -> compile::Result<()> {
        self.kind = AddressKind::Freed;
        Ok(())
    }

    pub(super) fn free(self) -> compile::Result<()> {
        self.free_inner(true)
    }

    pub(super) fn free_non_dangling(self) -> compile::Result<()> {
        self.free_inner(false)
    }

    /// Free the current address.
    fn free_inner(mut self, dangling: bool) -> compile::Result<()> {
        match replace(&mut self.kind, AddressKind::Freed) {
            AddressKind::Local | AddressKind::Dangling => {
                self.scopes
                    .free_addr(self.span, self.address, self.name, dangling)?;
            }
            AddressKind::Scope(scope) => {
                if self.scopes.top_id() == scope {
                    self.scopes
                        .free_addr(self.span, self.address, self.name, dangling)?;
                }
            }
            AddressKind::Freed => {
                return Err(compile::Error::msg(
                    self.span,
                    "Address has already been freed",
                ));
            }
            _ => {}
        }

        Ok(())
    }
}

impl fmt::Display for Address<'_, '_> {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{} address {}",
            self.kind,
            DisplayNamed::new(self.address, self.name)
        )
    }
}

impl fmt::Debug for Address<'_, '_> {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(self, f)
    }
}

impl Drop for Address<'_, '_> {
    fn drop(&mut self) {
        if matches!(self.kind, AddressKind::Freed) {
            return;
        }

        rune_diagnose!("{self} was not freed:\nallocated at:\n{}", self.backtrace);
    }
}

/// The kind of a needs.
enum AnyKind<'a, 'hir> {
    Defer {
        scopes: &'a Scopes<'hir>,
        scope: ScopeId,
        name: Option<&'static str>,
    },
    Address {
        address: Address<'a, 'hir>,
    },
    Ignore {
        #[allow(unused)]
        name: Option<&'static str>,
    },
    Freed,
}

impl fmt::Display for AnyKind<'_, '_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            AnyKind::Defer { scope, name, .. } => {
                write!(f, "defer({})", DisplayNamed::new(scope, *name))
            }
            AnyKind::Address { address } => address.fmt(f),
            AnyKind::Ignore { name } => DisplayNamed::new("ignore", *name).fmt(f),
            AnyKind::Freed => write!(f, "freed"),
        }
    }
}

impl fmt::Debug for AnyKind<'_, '_> {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(self, f)
    }
}

/// A needs hint for an expression.
/// This is used to contextually determine what an expression is expected to
/// produce.
#[must_use = "A need must be freed when its no longer in use"]
pub(super) struct Any<'a, 'hir> {
    span: &'hir dyn Spanned,
    kind: AnyKind<'a, 'hir>,
    #[cfg_attr(not(feature = "tracing"), allow(unused))]
    backtrace: Backtrace,
}

impl<'a, 'hir> Any<'a, 'hir> {
    /// A needs that should be ignored.
    #[track_caller]
    pub(super) fn ignore(span: &'hir dyn Spanned) -> Self {
        Self {
            span,
            kind: AnyKind::Ignore { name: None },
            backtrace: Backtrace::capture(),
        }
    }

    /// Defer allocation of a slot until it is requested.
    #[track_caller]
    pub(super) fn defer(scopes: &'a Scopes<'hir>, scope: ScopeId, span: &'hir dyn Spanned) -> Self {
        Self {
            span,
            kind: AnyKind::Defer {
                scopes,
                scope,
                name: None,
            },
            backtrace: Backtrace::capture(),
        }
    }

    /// An assigned address.
    #[track_caller]
    pub(super) fn assigned(
        span: &'hir dyn Spanned,
        scopes: &'a Scopes<'hir>,
        addr: InstAddress,
    ) -> Self {
        Self {
            span,
            kind: AnyKind::Address {
                address: Address::assigned(span, scopes, addr),
            },
            backtrace: Backtrace::capture(),
        }
    }

    /// Assign a name to the request.
    pub(super) fn with_name(mut self, new_name: &'static str) -> Self {
        match &mut self.kind {
            AnyKind::Defer { name, .. } => {
                *name = Some(new_name);
            }
            AnyKind::Address { address } => {
                address.name = Some(new_name);
            }
            AnyKind::Ignore { name, .. } => {
                *name = Some(new_name);
            }
            AnyKind::Freed => {}
        };

        self
    }

    pub(super) fn assign_addr(
        &mut self,
        cx: &mut Ctxt<'_, 'hir, '_>,
        from: InstAddress,
    ) -> compile::Result<()> {
        match &self.kind {
            AnyKind::Defer { scopes, name, .. } => {
                self.kind = AnyKind::Address {
                    address: Address {
                        span: self.span,
                        scopes,
                        address: from,
                        kind: AddressKind::Assigned,
                        name: *name,
                        backtrace: Backtrace::capture(),
                    },
                };
            }
            AnyKind::Address { address } => {
                address.assign_addr(cx, from)?;
            }
            _ => {}
        }

        Ok(())
    }

    /// Allocate an address even if it's a locally allocated one.
    #[inline]
    pub(super) fn try_alloc_addr(&mut self) -> compile::Result<Option<&mut Address<'a, 'hir>>> {
        if let AnyKind::Defer {
            scopes,
            scope,
            name,
        } = self.kind
        {
            let address = Address {
                span: self.span,
                scopes,
                address: scopes.alloc_in(self.span, scope)?,
                kind: AddressKind::Scope(scope),
                name,
                backtrace: Backtrace::capture(),
            };

            self.kind = AnyKind::Address { address };
        }

        match &mut self.kind {
            AnyKind::Address { address } => Ok(Some(address.alloc_addr()?)),
            _ => Ok(None),
        }
    }

    /// Get the needs as an output.
    #[inline]
    pub(super) fn try_alloc_output(&mut self) -> compile::Result<Option<Output>> {
        let Some(addr) = self.try_alloc_addr()? else {
            return Ok(None);
        };

        Ok(Some(addr.output()))
    }

    /// Test if any sort of value is needed.
    #[inline(always)]
    pub(super) fn alloc_output(&mut self) -> compile::Result<Output> {
        let Some(addr) = self.try_alloc_addr()? else {
            return Ok(Output::discard());
        };

        Ok(addr.output())
    }

    /// Try to treat as an address.
    #[inline]
    pub(super) fn addr(&self) -> compile::Result<&Address<'a, 'hir>> {
        match &self.kind {
            AnyKind::Address { address } => Ok(address),
            kind => Err(compile::Error::msg(
                self.span,
                format!("No address for need {kind}"),
            )),
        }
    }

    /// Coerce into an owned address.
    #[inline]
    pub(super) fn into_addr(mut self) -> compile::Result<Address<'a, 'hir>> {
        match replace(&mut self.kind, AnyKind::Freed) {
            AnyKind::Address { address } => Ok(address),
            kind => Err(compile::Error::msg(
                self.span,
                format!("No address for need {kind}"),
            )),
        }
    }

    /// Coerce into a output.
    #[inline]
    pub(super) fn output(&self) -> compile::Result<Output> {
        match &self.kind {
            AnyKind::Address { address } => Ok(Output::keep(address.address.offset())),
            AnyKind::Ignore { .. } => Ok(Output::discard()),
            kind => Err(compile::Error::msg(
                self.span,
                format!("Needs {kind} has not been allocated for output"),
            )),
        }
    }

    /// Gets the initialized address unless it is specifically set to `None`.
    #[inline]
    pub(super) fn try_as_addr(&self) -> compile::Result<Option<&Address<'a, 'hir>>> {
        match &self.kind {
            AnyKind::Address { address } => {
                if matches!(address.kind, AddressKind::Dangling) {
                    return Err(compile::Error::msg(
                        self.span,
                        "Expected address to be initialized",
                    ));
                }

                Ok(Some(address))
            }
            _ => Ok(None),
        }
    }

    /// Free the current needs.
    pub(super) fn free(mut self) -> compile::Result<()> {
        if let AnyKind::Address { address } = replace(&mut self.kind, AnyKind::Freed) {
            address.free()?;
        }

        Ok(())
    }
}

impl fmt::Display for Any<'_, '_> {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.kind.fmt(f)
    }
}

impl fmt::Debug for Any<'_, '_> {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(self, f)
    }
}

impl Drop for Any<'_, '_> {
    fn drop(&mut self) {
        if matches!(self.kind, AnyKind::Ignore { .. } | AnyKind::Freed) {
            return;
        }

        rune_diagnose!("{self} was not freed:\nallocated at:\n{}", self.backtrace);
    }
}