rune/compile/ir/
interpreter.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
use crate::alloc::prelude::*;
use crate::alloc::{try_format, Vec};
use crate::ast::Spanned;
use crate::compile::ir;
use crate::compile::ir::scopes::MissingLocal;
use crate::compile::meta;
use crate::compile::{self, IrErrorKind, ItemId, ModId, WithSpan};
use crate::hir;
use crate::query::{Query, Used};
use crate::runtime::{self, ConstValue, Object, OwnedTuple, Repr, Value};
use crate::TypeHash;

/// The interpreter that executed [Ir][crate::ir::Ir].
pub struct Interpreter<'a, 'arena> {
    /// A budget associated with the compiler, for how many expressions it's
    /// allowed to evaluate.
    pub(crate) budget: Budget,
    /// The module in which the interpreter is run.
    pub(crate) module: ModId,
    /// The item where the constant expression is located.
    pub(crate) item: ItemId,
    /// Constant scopes.
    pub(crate) scopes: ir::Scopes,
    /// Query engine to look for constant expressions.
    pub(crate) q: Query<'a, 'arena>,
}

impl Interpreter<'_, '_> {
    /// Outer evaluation for an expression which performs caching into `consts`.
    pub(crate) fn eval_const(&mut self, ir: &ir::Ir, used: Used) -> compile::Result<ConstValue> {
        tracing::trace!("processing constant: {}", self.q.pool.item(self.item));

        if let Some(const_value) = self.q.consts.get(self.item) {
            return Ok(const_value.try_clone()?);
        }

        if !self.q.consts.mark(self.item)? {
            return Err(compile::Error::new(ir, IrErrorKind::ConstCycle));
        }

        let ir_value = match ir::eval_ir(ir, self, used) {
            Ok(ir_value) => ir_value,
            Err(outcome) => match outcome {
                ir::EvalOutcome::Error(error) => {
                    return Err(error);
                }
                ir::EvalOutcome::NotConst(span) => {
                    return Err(compile::Error::new(span, IrErrorKind::NotConst))
                }
                ir::EvalOutcome::Break(span, _, _) => {
                    return Err(compile::Error::new(span, IrErrorKind::BreakOutsideOfLoop))
                }
            },
        };

        let const_value: ConstValue = crate::from_value(ir_value).with_span(ir)?;

        if self
            .q
            .consts
            .insert(self.item, const_value.try_clone()?)?
            .is_some()
        {
            return Err(compile::Error::new(ir, IrErrorKind::ConstCycle));
        }

        Ok(const_value)
    }

    /// Evaluate to an ir value.
    pub(crate) fn eval_value(&mut self, ir: &ir::Ir, used: Used) -> compile::Result<Value> {
        match ir::eval_ir(ir, self, used) {
            Ok(ir_value) => Ok(ir_value),
            Err(outcome) => match outcome {
                ir::EvalOutcome::Error(error) => Err(error),
                ir::EvalOutcome::NotConst(span) => {
                    Err(compile::Error::new(span, IrErrorKind::NotConst))
                }
                ir::EvalOutcome::Break(span, _, _) => {
                    Err(compile::Error::new(span, IrErrorKind::BreakOutsideOfLoop))
                }
            },
        }
    }

    /// Resolve the given constant value from the block scope.
    ///
    /// This looks up `const <ident> = <expr>` and evaluates them while caching
    /// their result.
    pub(crate) fn resolve_var(
        &mut self,
        span: &dyn Spanned,
        name: &hir::Variable,
        used: Used,
    ) -> compile::Result<Value> {
        if let Some(ir_value) = self.scopes.try_get(name) {
            return Ok(ir_value.try_clone()?);
        }

        let mut base = self.q.pool.item(self.item).try_to_owned()?;

        loop {
            let item = self
                .q
                .pool
                .alloc_item(base.extended(name.try_to_string()?)?)?;

            if let Some(const_value) = self.q.consts.get(item) {
                return Ok(const_value.to_value_with(self.q.context).with_span(span)?);
            }

            if let Some(meta) = self.q.query_meta(span, item, used)? {
                match &meta.kind {
                    meta::Kind::Const => {
                        let Some(const_value) = self.q.get_const_value(meta.hash) else {
                            return Err(compile::Error::msg(
                                span,
                                try_format!("Missing constant for hash {}", meta.hash),
                            ));
                        };

                        return Ok(const_value.to_value_with(self.q.context).with_span(span)?);
                    }
                    _ => {
                        return Err(compile::Error::new(
                            span,
                            IrErrorKind::UnsupportedMeta {
                                meta: meta.info(self.q.pool)?,
                            },
                        ));
                    }
                }
            }

            if !base.pop() {
                break;
            }
        }

        Err(compile::Error::new(
            span,
            MissingLocal(name.try_to_string()?.try_into_boxed_str()?),
        ))
    }

    pub(crate) fn call_const_fn<S>(
        &mut self,
        spanned: S,
        id: ItemId,
        args: Vec<Value>,
        used: Used,
    ) -> compile::Result<Value>
    where
        S: Copy + Spanned,
    {
        let span = Spanned::span(&spanned);
        let const_fn = self.q.const_fn_for(id).with_span(span)?;

        if const_fn.ir_fn.args.len() != args.len() {
            return Err(compile::Error::new(
                span,
                IrErrorKind::ArgumentCountMismatch {
                    actual: args.len(),
                    expected: const_fn.ir_fn.args.len(),
                },
            ));
        }

        let guard = self.scopes.isolate()?;

        for (name, value) in const_fn.ir_fn.args.iter().zip(args) {
            self.scopes.decl(*name, value).with_span(span)?;
        }

        let value = self.eval_value(&const_fn.ir_fn.ir, used)?;
        self.scopes.pop(guard).with_span(span)?;
        Ok(value)
    }
}

impl ir::Scopes {
    /// Get the given target as mut.
    pub(crate) fn get_target(&self, ir_target: &ir::IrTarget) -> compile::Result<Value> {
        match &ir_target.kind {
            ir::IrTargetKind::Name(name) => Ok(self.get_name(name, ir_target)?.clone()),
            ir::IrTargetKind::Field(ir_target, field) => {
                let value = self.get_target(ir_target)?;
                let object = value.borrow_ref::<Object>().with_span(ir_target)?;

                let Some(value) = object.get(field.as_ref()) else {
                    return Err(compile::Error::new(
                        ir_target,
                        IrErrorKind::MissingField {
                            field: field.try_clone()?,
                        },
                    ));
                };

                Ok(value.clone())
            }
            ir::IrTargetKind::Index(target, index) => {
                let value = self.get_target(target)?;

                match value.type_hash() {
                    runtime::Vec::HASH => {
                        let vec = value.borrow_ref::<runtime::Vec>().with_span(ir_target)?;

                        if let Some(value) = vec.get(*index) {
                            return Ok(value.clone());
                        }
                    }
                    runtime::OwnedTuple::HASH => {
                        let tuple = value
                            .borrow_ref::<runtime::OwnedTuple>()
                            .with_span(ir_target)?;

                        if let Some(value) = tuple.get(*index) {
                            return Ok(value.clone());
                        }
                    }
                    _ => {
                        return Err(compile::Error::expected_type::<OwnedTuple>(
                            ir_target,
                            value.type_info(),
                        ));
                    }
                }

                Err(compile::Error::new(
                    ir_target,
                    IrErrorKind::MissingIndex { index: *index },
                ))
            }
        }
    }

    /// Update the given target with the given constant value.
    pub(crate) fn set_target(
        &mut self,
        ir_target: &ir::IrTarget,
        value: Value,
    ) -> compile::Result<()> {
        match &ir_target.kind {
            ir::IrTargetKind::Name(name) => {
                *self.get_name_mut(name, ir_target)? = value;
                Ok(())
            }
            ir::IrTargetKind::Field(target, field) => {
                let target = self.get_target(target)?;
                let mut object = target.borrow_mut::<Object>().with_span(ir_target)?;
                let field = field.as_ref().try_to_owned()?;
                object.insert(field, value).with_span(ir_target)?;
                Ok(())
            }
            ir::IrTargetKind::Index(target, index) => {
                let target = self.get_target(target)?;

                match target.as_ref() {
                    Repr::Inline(value) => {
                        return Err(compile::Error::expected_type::<OwnedTuple>(
                            ir_target,
                            value.type_info(),
                        ));
                    }
                    Repr::Dynamic(value) => {
                        return Err(compile::Error::expected_type::<OwnedTuple>(
                            ir_target,
                            value.type_info(),
                        ));
                    }
                    Repr::Any(any) => match any.type_hash() {
                        runtime::Vec::HASH => {
                            let mut vec = any.borrow_mut::<runtime::Vec>().with_span(ir_target)?;

                            if let Some(current) = vec.get_mut(*index) {
                                *current = value;
                                return Ok(());
                            }
                        }
                        runtime::OwnedTuple::HASH => {
                            let mut tuple = any
                                .borrow_mut::<runtime::OwnedTuple>()
                                .with_span(ir_target)?;

                            if let Some(current) = tuple.get_mut(*index) {
                                *current = value;
                                return Ok(());
                            }
                        }
                        _ => {
                            return Err(compile::Error::expected_type::<OwnedTuple>(
                                ir_target,
                                any.type_info(),
                            ));
                        }
                    },
                };

                Err(compile::Error::msg(ir_target, "missing index"))
            }
        }
    }

    /// Mutate the given target with the given constant value.
    pub(crate) fn mut_target(
        &mut self,
        ir_target: &ir::IrTarget,
        op: impl FnOnce(&mut Value) -> compile::Result<()>,
    ) -> compile::Result<()> {
        match &ir_target.kind {
            ir::IrTargetKind::Name(name) => {
                let value = self.get_name_mut(name, ir_target)?;
                op(value)
            }
            ir::IrTargetKind::Field(target, field) => {
                let value = self.get_target(target)?;
                let mut object = value.borrow_mut::<Object>().with_span(ir_target)?;

                let Some(value) = object.get_mut(field.as_ref()) else {
                    return Err(compile::Error::new(
                        ir_target,
                        IrErrorKind::MissingField {
                            field: field.try_clone()?,
                        },
                    ));
                };

                op(value)
            }
            ir::IrTargetKind::Index(target, index) => {
                let current = self.get_target(target)?;

                match current.as_ref() {
                    Repr::Dynamic(value) => Err(compile::Error::expected_type::<OwnedTuple>(
                        ir_target,
                        value.type_info(),
                    )),
                    Repr::Any(value) => match value.type_hash() {
                        runtime::Vec::HASH => {
                            let mut vec =
                                value.borrow_mut::<runtime::Vec>().with_span(ir_target)?;

                            let value = vec.get_mut(*index).ok_or_else(|| {
                                compile::Error::new(
                                    ir_target,
                                    IrErrorKind::MissingIndex { index: *index },
                                )
                            })?;

                            op(value)
                        }
                        runtime::OwnedTuple::HASH => {
                            let mut tuple = value
                                .borrow_mut::<runtime::OwnedTuple>()
                                .with_span(ir_target)?;

                            let value = tuple.get_mut(*index).ok_or_else(|| {
                                compile::Error::new(
                                    ir_target,
                                    IrErrorKind::MissingIndex { index: *index },
                                )
                            })?;

                            op(value)
                        }
                        _ => Err(compile::Error::expected_type::<OwnedTuple>(
                            ir_target,
                            value.type_info(),
                        )),
                    },
                    actual => Err(compile::Error::expected_type::<OwnedTuple>(
                        ir_target,
                        actual.type_info(),
                    )),
                }
            }
        }
    }
}

/// A budget dictating the number of evaluations the compiler is allowed to do.
pub(crate) struct Budget {
    budget: usize,
}

impl Budget {
    /// Construct a new constant evaluation budget with the given constraint.
    pub(crate) fn new(budget: usize) -> Self {
        Self { budget }
    }

    /// Take an item from the budget. Errors if the budget is exceeded.
    pub(crate) fn take<S>(&mut self, spanned: S) -> compile::Result<()>
    where
        S: Spanned,
    {
        if self.budget == 0 {
            return Err(compile::Error::new(spanned, IrErrorKind::BudgetExceeded));
        }

        self.budget -= 1;
        Ok(())
    }
}