xur-bytecode 0.1.7

The register-based bytecode of the Xur language, and lowering to it
Documentation
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
//! Lowering resolved syntax to bytecode, one function at a time.
//!
//! Functions are lowered on demand, the first time they are called, so a
//! program only pays for the code it runs and modules imported at runtime
//! need no up-front work.

mod expr;
mod pat;

use std::collections::HashMap;
use std::sync::Arc;

use xur_ast::{
    Attribute, Block as AstBlock, Expr, FnRetTy, LitKind, MetaItemInner, MetaItemKind, NodeId,
    Param, Ty, TyKind,
};
use xur_resolve::{DefId, DefKind, ModuleIdx, Namespace, Program, Res, Resolutions};
use xur_span::{Span, Symbol};

use crate::ir::{
    Block, BlockId, Constant, Decorator, DefRef, Function, Inst, InstKind, Reg, RtType, Terminator,
    TypeArg,
};
use crate::items::{ItemIndex, ItemRef};

/// Lowers the function or constant `def`, or returns `None` if `def` is
/// neither. A constant lowers to a function without parameters that
/// computes its value.
pub fn lower(program: &Program, items: &ItemIndex, def: DefId) -> Option<Function> {
    let resolutions = program.resolutions();
    let name = resolutions.def_path(def);
    match items.get(def)? {
        ItemRef::Fn {
            item,
            attrs,
            module,
            impl_generics,
        } => {
            let mut lowerer = Lowerer::new(program, items, module);
            let impl_type_param_count = if let Some(generics) = impl_generics {
                for param in &generics.params {
                    lowerer.type_params.push(resolutions.node_defs[&param.id]);
                    lowerer.type_param_names.push(param.ident.name);
                    lowerer.type_param_required.push(param.required);
                }
                generics.params.len() as u32
            } else {
                0
            };
            lowerer.impl_type_param_count = impl_type_param_count;
            for param in &item.generics.params {
                lowerer.type_params.push(resolutions.node_defs[&param.id]);
                lowerer.type_param_names.push(param.ident.name);
                lowerer.type_param_required.push(param.required);
            }
            if let FnRetTy::Ty(ty) = &item.decl.output {
                lowerer.ret_ty = lowerer.rt_type(ty);
            }
            lowerer.params(&item.decl.inputs);
            let value = lowerer.block(&item.body);
            lowerer.ret(value, item.body.span);
            let params = item.decl.inputs.len() as u32;
            let decorators = extract_decorators(resolutions, module, attrs);
            let mut func = lowerer.finish(name, 0, params, item.body.span);
            func.decorators = decorators;
            Some(func)
        }
        ItemRef::Const { item, module } => {
            let mut lowerer = Lowerer::new(program, items, module);
            let value = lowerer.expr(&item.expr);
            lowerer.terminate(Terminator::Return(value), item.expr.span);
            Some(lowerer.finish(name, 0, 0, item.expr.span))
        }
        ItemRef::Ctor { .. } | ItemRef::Enum { .. } => None,
    }
}

/// A block under construction.
struct BlockData {
    insts: Vec<Inst>,
    term: Option<(Terminator, Span)>,
}

/// Where `break` and `continue` go in the innermost loop.
struct LoopScope {
    break_to: BlockId,
    continue_to: BlockId,
    /// Where `break value` puts the loop's value.
    result: Reg,
}

struct Lowerer<'a> {
    program: &'a Program,
    resolutions: &'a Resolutions,
    items: &'a ItemIndex,
    module: ModuleIdx,
    blocks: Vec<BlockData>,
    current: BlockId,
    registers: u32,
    /// The register of each local variable, by its binding pattern.
    locals: HashMap<NodeId, Reg>,
    loops: Vec<LoopScope>,
    impl_type_param_count: u32,
    /// The definitions of the function's type parameters, in order.
    type_params: Vec<DefId>,
    type_param_names: Vec<Symbol>,
    type_param_required: Vec<bool>,
    closures: Vec<Arc<Function>>,
    /// For each parameter, which type param index it uses.
    param_type_params: Vec<Option<u32>>,
    param_names: Vec<Option<Symbol>>,
    param_types: Vec<Option<RtType>>,
    /// The declared return type, checked on return.
    ret_ty: Option<RtType>,
    /// A type hint from a `let x: Type = call()` binding, passed as a type
    /// arg to the call when it has none of its own.
    hint_type_arg: Option<TypeArg>,
}

impl<'a> Lowerer<'a> {
    fn new(program: &'a Program, items: &'a ItemIndex, module: ModuleIdx) -> Self {
        let mut lowerer = Self {
            program,
            resolutions: program.resolutions(),
            items,
            module,
            blocks: Vec::new(),
            current: BlockId(0),
            registers: 0,
            locals: HashMap::new(),
            loops: Vec::new(),
            impl_type_param_count: 0,
            type_params: Vec::new(),
            type_param_names: Vec::new(),
            type_param_required: Vec::new(),
            closures: Vec::new(),
            param_type_params: Vec::new(),
            param_names: Vec::new(),
            param_types: Vec::new(),
            ret_ty: None,
            hint_type_arg: None,
        };
        lowerer.current = lowerer.new_block();
        lowerer
    }

    fn finish(self, name: String, captures: u32, params: u32, span: Span) -> Function {
        let blocks = self
            .blocks
            .into_iter()
            .map(|block| {
                let (term, term_span) = block.term.unwrap_or((Terminator::Unreachable, span));
                Block {
                    insts: block.insts,
                    term,
                    term_span,
                }
            })
            .collect();
        Function {
            name,
            module: self.module,
            captures,
            params,
            registers: self.registers,
            impl_type_param_count: self.impl_type_param_count,
            type_params: self.type_param_names,
            type_param_required: self.type_param_required,
            param_type_params: self.param_type_params,
            param_names: self.param_names,
            param_types: self.param_types,
            ret_type: self.ret_ty,
            decorators: Vec::new(),
            blocks,
            closures: self.closures,
            span,
        }
    }

    /// Binds the parameters, which arrive in the registers after any
    /// captures, checking those whose type is written.
    fn params(&mut self, params: &[Param]) {
        let registers: Vec<_> = params.iter().map(|_| self.new_reg()).collect();
        for (param, reg) in params.iter().zip(registers) {
            self.param_type_params
                .push(self.type_param_index(&param.ty));
            let name = match &param.pat.kind {
                xur_ast::PatKind::Ident(ident) => Some(ident.name),
                _ => None,
            };
            self.param_names.push(name);
            self.param_types.push(self.rt_type(&param.ty));
            self.check_type(reg, &param.ty, param.span);
            self.bind_irrefutable(&param.pat, reg);
        }
    }

    /// If `ty` is a bare type parameter, returns its index.
    fn type_param_index(&self, ty: &Ty) -> Option<u32> {
        if let TyKind::Path(path) = &ty.kind
            && let Some(&Res::Def(DefKind::TyParam, param)) = self.resolutions.res.get(&path.id)
        {
            self.type_params
                .iter()
                .position(|&p| p == param)
                .map(|i| i as u32)
        } else {
            None
        }
    }

    /// Returns `value`, checking it against the declared return type.
    fn ret(&mut self, value: Reg, span: Span) {
        if let Some(ty) = self.ret_ty.clone() {
            self.emit(
                InstKind::CheckType {
                    value,
                    ty,
                    coerce: false,
                },
                span,
            );
        }
        self.terminate(Terminator::Return(value), span);
    }

    /// Checks at runtime that `value` has the type `ty`, if `ty` is written
    /// and concrete. No coercion — use `check_type_coerce` for `as`.
    fn check_type(&mut self, value: Reg, ty: &Ty, span: Span) {
        if let Some(ty) = self.rt_type(ty) {
            self.emit(
                InstKind::CheckType {
                    value,
                    ty,
                    coerce: false,
                },
                span,
            );
        }
    }

    /// Checks and coerces `value` to `ty` (the `as` operator).
    fn check_type_coerce(&mut self, value: Reg, ty: &Ty, span: Span) {
        if let Some(ty) = self.rt_type(ty) {
            self.emit(
                InstKind::CheckType {
                    value,
                    ty,
                    coerce: true,
                },
                span,
            );
        }
    }

    /// The runtime check for a written type, or `None` if nothing about
    /// it can be checked.
    fn rt_type(&self, ty: &Ty) -> Option<RtType> {
        crate::types::rt_type(self.resolutions, ty)
    }

    fn new_reg(&mut self) -> Reg {
        let reg = Reg(self.registers);
        self.registers += 1;
        reg
    }

    fn new_block(&mut self) -> BlockId {
        let id = BlockId(self.blocks.len() as u32);
        self.blocks.push(BlockData {
            insts: Vec::new(),
            term: None,
        });
        id
    }

    fn switch_to(&mut self, block: BlockId) {
        self.current = block;
    }

    fn emit(&mut self, kind: InstKind, span: Span) {
        self.blocks[self.current.0 as usize]
            .insts
            .push(Inst { kind, span });
    }

    /// Ends the current block. Code lowered after a `return`, `break` or
    /// `continue` goes into a fresh block that nothing jumps to.
    fn terminate(&mut self, term: Terminator, span: Span) {
        let block = &mut self.blocks[self.current.0 as usize];
        if block.term.is_none() {
            block.term = Some((term, span));
        }
    }

    /// Ends the current block and continues in a new, unreachable one.
    fn diverge(&mut self, term: Terminator, span: Span) {
        self.terminate(term, span);
        let dead = self.new_block();
        self.switch_to(dead);
    }

    fn constant(&mut self, value: Constant, span: Span) -> Reg {
        let dst = self.new_reg();
        self.emit(InstKind::Const { dst, value }, span);
        dst
    }

    fn unit(&mut self, span: Span) -> Reg {
        self.constant(Constant::Unit, span)
    }

    fn error(&mut self, message: impl Into<String>, span: Span) -> Reg {
        self.emit(
            InstKind::Error {
                message: message.into(),
            },
            span,
        );
        self.unit(span)
    }

    /// Lowers a block, returning the register holding its value.
    fn block(&mut self, block: &AstBlock) -> Reg {
        self.block_value(block)
    }

    fn expr_or_unit(&mut self, expr: Option<&Expr>, span: Span) -> Reg {
        match expr {
            Some(expr) => self.expr(expr),
            None => self.unit(span),
        }
    }
}

fn extract_decorators(
    resolutions: &Resolutions,
    module: ModuleIdx,
    attrs: &[Attribute],
) -> Vec<Decorator> {
    let module_def = resolutions.module_defs[module.0];
    let mut decorators = Vec::new();
    for attr in attrs {
        let segments = &attr.meta.path.segments;
        if segments.len() != 1 || segments[0].ident.name.as_str() != "decorate" {
            continue;
        }
        let MetaItemKind::List(items) = &attr.meta.kind else {
            continue;
        };
        for item in items {
            let MetaItemInner::MetaItem(meta) = item else {
                continue;
            };
            let def = resolve_path_in_module(resolutions, module_def, &meta.path.segments);
            let Some(def) = def else { continue };
            let args = match &meta.kind {
                MetaItemKind::Word => Vec::new(),
                MetaItemKind::List(inner) => inner
                    .iter()
                    .filter_map(|a| match a {
                        MetaItemInner::Lit(lit) => lit_to_constant(lit),
                        _ => None,
                    })
                    .collect(),
                _ => Vec::new(),
            };
            decorators.push(Decorator { def, args });
        }
    }
    decorators
}

fn resolve_path_in_module(
    resolutions: &Resolutions,
    module_def: DefId,
    segments: &[xur_ast::PathSegment],
) -> Option<DefRef> {
    if segments.len() == 1 {
        let name = segments[0].ident.name;
        let res = resolutions.lookup(module_def, Namespace::Value, name)?;
        return res_to_def_ref(res);
    }
    // Multi-segment: resolve the first as a module, then walk.
    let mut scope = module_def;
    for (i, seg) in segments.iter().enumerate() {
        let name = seg.ident.name;
        let is_last = i + 1 == segments.len();
        if is_last {
            let res = resolutions.lookup(scope, Namespace::Value, name)?;
            return res_to_def_ref(res);
        }
        let res = resolutions
            .lookup(scope, Namespace::Value, name)
            .or_else(|| resolutions.lookup(scope, Namespace::Type, name))?;
        match res {
            Res::Def(DefKind::Mod, def) => scope = def,
            Res::Def(DefKind::Enum, def) => scope = def,
            _ => return None,
        }
    }
    None
}

fn res_to_def_ref(res: Res) -> Option<DefRef> {
    match res {
        Res::Def(DefKind::Fn, def) => Some(DefRef::Fn(def)),
        Res::Def(DefKind::NativeFn(id), _) => Some(DefRef::Native(id)),
        Res::Def(DefKind::Const, def) => Some(DefRef::Const(def)),
        _ => None,
    }
}

fn lit_to_constant(lit: &xur_ast::Lit) -> Option<Constant> {
    match &lit.kind {
        LitKind::Bool(b) => Some(Constant::Bool(*b)),
        LitKind::Int(i) => Some(Constant::Int(*i as i64)),
        LitKind::Float(digits) => digits.as_str().parse().ok().map(Constant::Float),
        LitKind::Str(s) => Some(Constant::Str(s.as_str().into())),
        _ => None,
    }
}