2022-08-07 22:41:42 +02:00
|
|
|
//! This module implements a compiler for compiling the rnix AST
|
|
|
|
//! representation to Tvix bytecode.
|
2022-08-11 13:07:37 +02:00
|
|
|
//!
|
|
|
|
//! A note on `unwrap()`: This module contains a lot of calls to
|
|
|
|
//! `unwrap()` or `expect(...)` on data structures returned by `rnix`.
|
|
|
|
//! The reason for this is that rnix uses the same data structures to
|
|
|
|
//! represent broken and correct ASTs, so all typed AST variants have
|
|
|
|
//! the ability to represent an incorrect node.
|
|
|
|
//!
|
|
|
|
//! However, at the time that the AST is passed to the compiler we
|
|
|
|
//! have verified that `rnix` considers the code to be correct, so all
|
2022-08-16 14:11:06 +02:00
|
|
|
//! variants are fulfilled. In cases where the invariant is guaranteed
|
|
|
|
//! by the code in this module, `debug_assert!` has been used to catch
|
2022-08-11 13:07:37 +02:00
|
|
|
//! mistakes early during development.
|
2022-08-07 22:41:42 +02:00
|
|
|
|
2022-09-23 15:05:14 +02:00
|
|
|
mod bindings;
|
2022-08-27 18:49:08 +02:00
|
|
|
mod scope;
|
|
|
|
|
2022-09-22 23:31:40 +02:00
|
|
|
use codemap::Span;
|
2022-09-28 12:34:31 +02:00
|
|
|
use rnix::ast::{self, AstToken};
|
2022-09-16 18:35:39 +02:00
|
|
|
use smol_str::SmolStr;
|
2022-08-27 18:49:08 +02:00
|
|
|
use std::collections::HashMap;
|
2022-08-12 17:28:45 +02:00
|
|
|
use std::path::{Path, PathBuf};
|
2022-10-26 14:16:04 +02:00
|
|
|
use std::rc::{Rc, Weak};
|
2022-09-18 01:52:23 +02:00
|
|
|
use std::sync::Arc;
|
2022-08-12 17:04:05 +02:00
|
|
|
|
2022-08-07 22:41:42 +02:00
|
|
|
use crate::chunk::Chunk;
|
2022-08-22 22:48:47 +02:00
|
|
|
use crate::errors::{Error, ErrorKind, EvalResult};
|
2022-10-03 15:08:59 +02:00
|
|
|
use crate::observer::CompilerObserver;
|
2022-08-28 18:38:17 +02:00
|
|
|
use crate::opcode::{CodeIdx, Count, JumpOffset, OpCode, UpvalueIdx};
|
2022-10-06 13:33:09 +02:00
|
|
|
use crate::spans::ToSpan;
|
2022-10-13 05:27:09 +02:00
|
|
|
use crate::value::{Closure, Formals, Lambda, Thunk, Value};
|
2022-08-12 16:13:41 +02:00
|
|
|
use crate::warnings::{EvalWarning, WarningKind};
|
2022-08-09 15:53:09 +02:00
|
|
|
|
2022-09-02 23:26:32 +02:00
|
|
|
use self::scope::{LocalIdx, LocalPosition, Scope, Upvalue, UpvalueKind};
|
2022-08-27 18:49:08 +02:00
|
|
|
|
2022-08-12 16:07:32 +02:00
|
|
|
/// Represents the result of compiling a piece of Nix code. If
|
|
|
|
/// compilation was successful, the resulting bytecode can be passed
|
|
|
|
/// to the VM.
|
2022-08-26 19:17:40 +02:00
|
|
|
pub struct CompilationOutput {
|
2022-09-04 15:56:20 +02:00
|
|
|
pub lambda: Rc<Lambda>,
|
2022-08-12 16:07:32 +02:00
|
|
|
pub warnings: Vec<EvalWarning>,
|
2022-08-22 22:48:47 +02:00
|
|
|
pub errors: Vec<Error>,
|
2022-10-26 14:16:04 +02:00
|
|
|
|
|
|
|
// This field must outlive the rc::Weak reference which breaks
|
|
|
|
// the builtins -> import -> builtins reference cycle.
|
|
|
|
pub globals: Rc<GlobalsMap>,
|
2022-08-12 16:07:32 +02:00
|
|
|
}
|
|
|
|
|
2022-08-23 23:01:45 +02:00
|
|
|
/// Represents the lambda currently being compiled.
|
|
|
|
struct LambdaCtx {
|
2022-08-23 21:54:25 +02:00
|
|
|
lambda: Lambda,
|
2022-08-14 22:14:37 +02:00
|
|
|
scope: Scope,
|
2022-09-06 22:13:48 +02:00
|
|
|
captures_with_stack: bool,
|
2022-08-23 23:01:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl LambdaCtx {
|
|
|
|
fn new() -> Self {
|
|
|
|
LambdaCtx {
|
2022-10-22 22:55:21 +02:00
|
|
|
lambda: Lambda::default(),
|
2022-08-23 23:01:45 +02:00
|
|
|
scope: Default::default(),
|
2022-09-06 22:13:48 +02:00
|
|
|
captures_with_stack: false,
|
2022-08-23 23:01:45 +02:00
|
|
|
}
|
|
|
|
}
|
2022-09-02 00:19:53 +02:00
|
|
|
|
|
|
|
fn inherit(&self) -> Self {
|
2022-09-04 15:56:20 +02:00
|
|
|
LambdaCtx {
|
2022-10-22 22:55:21 +02:00
|
|
|
lambda: Lambda::default(),
|
2022-09-02 00:19:53 +02:00
|
|
|
scope: self.scope.inherit(),
|
2022-09-06 22:13:48 +02:00
|
|
|
captures_with_stack: false,
|
2022-09-04 15:56:20 +02:00
|
|
|
}
|
2022-09-02 00:19:53 +02:00
|
|
|
}
|
2022-08-23 23:01:45 +02:00
|
|
|
}
|
2022-08-13 16:34:20 +02:00
|
|
|
|
2022-10-26 14:16:04 +02:00
|
|
|
/// The map of globally available functions that should implicitly
|
|
|
|
/// be resolvable in the global scope.
|
|
|
|
pub type GlobalsMap = HashMap<&'static str, Rc<dyn Fn(&mut Compiler, Span)>>;
|
|
|
|
|
|
|
|
/// Functions with this type are used to construct a
|
|
|
|
/// self-referential `builtins` object; it takes a weak reference to
|
|
|
|
/// its own result, similar to how nixpkgs' overlays work.
|
|
|
|
/// Rc::new_cyclic() is what "ties the knot". The heap allocation
|
|
|
|
/// (Box) and vtable (dyn) do not impair runtime or compile-time
|
|
|
|
/// performance; they exist only during compiler startup.
|
|
|
|
pub type GlobalsMapFunc = Box<dyn FnOnce(&Weak<GlobalsMap>) -> GlobalsMap>;
|
2022-08-24 14:37:09 +02:00
|
|
|
|
2022-10-26 14:16:04 +02:00
|
|
|
pub struct Compiler<'observer> {
|
2022-08-23 23:01:45 +02:00
|
|
|
contexts: Vec<LambdaCtx>,
|
2022-08-12 16:07:32 +02:00
|
|
|
warnings: Vec<EvalWarning>,
|
2022-08-22 22:48:47 +02:00
|
|
|
errors: Vec<Error>,
|
2022-08-12 17:28:45 +02:00
|
|
|
root_dir: PathBuf,
|
2022-08-24 14:37:09 +02:00
|
|
|
|
|
|
|
/// Carries all known global tokens; the full set of which is
|
|
|
|
/// created when the compiler is invoked.
|
|
|
|
///
|
|
|
|
/// Each global has an associated token, which when encountered as
|
|
|
|
/// an identifier is resolved against the scope poisoning logic,
|
|
|
|
/// and a function that should emit code for the token.
|
2022-10-26 14:16:04 +02:00
|
|
|
globals: Rc<GlobalsMap>,
|
2022-09-01 15:38:05 +02:00
|
|
|
|
|
|
|
/// File reference in the codemap contains all known source code
|
|
|
|
/// and is used to track the spans from which instructions where
|
|
|
|
/// derived.
|
2022-09-18 01:52:23 +02:00
|
|
|
file: Arc<codemap::File>,
|
2022-09-02 16:44:31 +02:00
|
|
|
|
2022-09-04 15:56:20 +02:00
|
|
|
/// Carry an observer for the compilation process, which is called
|
|
|
|
/// whenever a chunk is emitted.
|
2022-10-03 15:08:59 +02:00
|
|
|
observer: &'observer mut dyn CompilerObserver,
|
2022-08-07 22:41:42 +02:00
|
|
|
}
|
|
|
|
|
2022-10-06 13:33:09 +02:00
|
|
|
impl Compiler<'_> {
|
|
|
|
pub(super) fn span_for<S: ToSpan>(&self, to_span: &S) -> Span {
|
|
|
|
to_span.span_for(&self.file)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-18 23:22:12 +02:00
|
|
|
/// Compiler construction
|
|
|
|
impl<'observer> Compiler<'observer> {
|
|
|
|
pub(crate) fn new(
|
|
|
|
location: Option<PathBuf>,
|
|
|
|
file: Arc<codemap::File>,
|
2022-10-26 14:16:04 +02:00
|
|
|
globals: Rc<GlobalsMap>,
|
2022-10-03 15:08:59 +02:00
|
|
|
observer: &'observer mut dyn CompilerObserver,
|
2022-09-18 23:22:12 +02:00
|
|
|
) -> EvalResult<Self> {
|
|
|
|
let mut root_dir = match location {
|
2022-10-13 17:21:29 +02:00
|
|
|
Some(dir) if cfg!(target_arch = "wasm32") || dir.is_absolute() => Ok(dir),
|
2022-10-13 08:10:58 +02:00
|
|
|
_ => {
|
|
|
|
let current_dir = std::env::current_dir().map_err(|e| Error {
|
2022-10-21 00:52:36 +02:00
|
|
|
kind: ErrorKind::RelativePathResolution(format!(
|
2022-10-13 08:10:58 +02:00
|
|
|
"could not determine current directory: {}",
|
|
|
|
e
|
|
|
|
)),
|
|
|
|
span: file.span,
|
|
|
|
})?;
|
|
|
|
if let Some(dir) = location {
|
|
|
|
Ok(current_dir.join(dir))
|
|
|
|
} else {
|
|
|
|
Ok(current_dir)
|
|
|
|
}
|
|
|
|
}
|
2022-09-18 23:22:12 +02:00
|
|
|
}?;
|
|
|
|
|
|
|
|
// If the path passed from the caller points to a file, the
|
|
|
|
// filename itself needs to be truncated as this must point to a
|
|
|
|
// directory.
|
|
|
|
if root_dir.is_file() {
|
|
|
|
root_dir.pop();
|
|
|
|
}
|
|
|
|
|
2022-10-13 17:21:29 +02:00
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
2022-10-13 08:10:58 +02:00
|
|
|
debug_assert!(root_dir.is_absolute());
|
2022-10-13 17:21:29 +02:00
|
|
|
|
2022-09-18 23:22:12 +02:00
|
|
|
Ok(Self {
|
|
|
|
root_dir,
|
|
|
|
file,
|
|
|
|
observer,
|
2022-10-26 14:16:04 +02:00
|
|
|
globals,
|
2022-09-18 23:22:12 +02:00
|
|
|
contexts: vec![LambdaCtx::new()],
|
|
|
|
warnings: vec![],
|
|
|
|
errors: vec![],
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-23 20:00:53 +02:00
|
|
|
// Helper functions for emitting code and metadata to the internal
|
|
|
|
// structures of the compiler.
|
2022-09-18 01:52:23 +02:00
|
|
|
impl Compiler<'_> {
|
2022-08-23 23:01:45 +02:00
|
|
|
fn context(&self) -> &LambdaCtx {
|
|
|
|
&self.contexts[self.contexts.len() - 1]
|
|
|
|
}
|
|
|
|
|
|
|
|
fn context_mut(&mut self) -> &mut LambdaCtx {
|
|
|
|
let idx = self.contexts.len() - 1;
|
|
|
|
&mut self.contexts[idx]
|
|
|
|
}
|
|
|
|
|
2022-08-23 20:00:53 +02:00
|
|
|
fn chunk(&mut self) -> &mut Chunk {
|
2022-08-27 19:41:10 +02:00
|
|
|
&mut self.context_mut().lambda.chunk
|
2022-08-23 20:00:53 +02:00
|
|
|
}
|
|
|
|
|
2022-08-23 22:57:36 +02:00
|
|
|
fn scope(&self) -> &Scope {
|
2022-08-23 23:01:45 +02:00
|
|
|
&self.context().scope
|
2022-08-23 22:57:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn scope_mut(&mut self) -> &mut Scope {
|
2022-08-23 23:01:45 +02:00
|
|
|
&mut self.context_mut().scope
|
2022-08-23 22:57:36 +02:00
|
|
|
}
|
|
|
|
|
2022-09-01 15:54:30 +02:00
|
|
|
/// Push a single instruction to the current bytecode chunk and
|
|
|
|
/// track the source span from which it was compiled.
|
2022-09-16 20:51:03 +02:00
|
|
|
fn push_op<T: ToSpan>(&mut self, data: OpCode, node: &T) -> CodeIdx {
|
2022-09-01 18:57:55 +02:00
|
|
|
let span = self.span_for(node);
|
2022-09-01 15:54:30 +02:00
|
|
|
self.chunk().push_op(data, span)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Emit a single constant to the current bytecode chunk and track
|
|
|
|
/// the source span from which it was compiled.
|
2022-10-26 14:16:04 +02:00
|
|
|
pub(super) fn emit_constant<T: ToSpan>(&mut self, value: Value, node: &T) {
|
2022-08-23 20:00:53 +02:00
|
|
|
let idx = self.chunk().push_constant(value);
|
2022-09-01 15:54:30 +02:00
|
|
|
self.push_op(OpCode::OpConstant(idx), node);
|
2022-08-23 20:00:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Actual code-emitting AST traversal methods.
|
2022-09-18 01:52:23 +02:00
|
|
|
impl Compiler<'_> {
|
2022-10-03 16:08:39 +02:00
|
|
|
fn compile(&mut self, slot: LocalIdx, expr: &ast::Expr) {
|
2022-08-16 22:43:45 +02:00
|
|
|
match expr {
|
|
|
|
ast::Expr::Literal(literal) => self.compile_literal(literal),
|
2022-10-10 05:46:51 +02:00
|
|
|
ast::Expr::Path(path) => self.compile_path(slot, path),
|
2022-08-29 20:51:08 +02:00
|
|
|
ast::Expr::Str(s) => self.compile_str(slot, s),
|
2022-09-03 15:18:06 +02:00
|
|
|
|
2022-08-29 20:51:08 +02:00
|
|
|
ast::Expr::UnaryOp(op) => self.compile_unary_op(slot, op),
|
2022-09-03 15:18:06 +02:00
|
|
|
|
|
|
|
ast::Expr::BinOp(binop) => {
|
2022-10-03 16:08:39 +02:00
|
|
|
self.thunk(slot, binop, move |c, s| c.compile_binop(s, binop))
|
2022-09-03 15:18:06 +02:00
|
|
|
}
|
|
|
|
|
2022-08-29 20:33:15 +02:00
|
|
|
ast::Expr::HasAttr(has_attr) => self.compile_has_attr(slot, has_attr),
|
2022-09-03 15:18:06 +02:00
|
|
|
|
2022-10-03 16:08:39 +02:00
|
|
|
ast::Expr::List(list) => self.thunk(slot, list, move |c, s| c.compile_list(s, list)),
|
2022-09-03 15:18:06 +02:00
|
|
|
|
2022-10-03 16:08:39 +02:00
|
|
|
ast::Expr::AttrSet(attrs) => {
|
|
|
|
self.thunk(slot, attrs, move |c, s| c.compile_attr_set(s, attrs))
|
|
|
|
}
|
2022-09-03 15:18:06 +02:00
|
|
|
|
2022-10-03 16:08:39 +02:00
|
|
|
ast::Expr::Select(select) => {
|
|
|
|
self.thunk(slot, select, move |c, s| c.compile_select(s, select))
|
|
|
|
}
|
2022-09-03 15:18:06 +02:00
|
|
|
|
2022-09-07 17:11:40 +02:00
|
|
|
ast::Expr::Assert(assert) => {
|
2022-10-03 16:08:39 +02:00
|
|
|
self.thunk(slot, assert, move |c, s| c.compile_assert(s, assert))
|
2022-09-07 17:11:40 +02:00
|
|
|
}
|
2022-10-10 21:57:54 +02:00
|
|
|
ast::Expr::IfElse(if_else) => {
|
|
|
|
self.thunk(slot, if_else, move |c, s| c.compile_if_else(s, if_else))
|
|
|
|
}
|
2022-10-22 18:59:06 +02:00
|
|
|
|
|
|
|
ast::Expr::LetIn(let_in) => {
|
|
|
|
self.thunk(slot, let_in, move |c, s| c.compile_let_in(s, let_in))
|
|
|
|
}
|
|
|
|
|
2022-08-28 23:11:23 +02:00
|
|
|
ast::Expr::Ident(ident) => self.compile_ident(slot, ident),
|
2022-10-03 16:08:39 +02:00
|
|
|
ast::Expr::With(with) => self.thunk(slot, with, |c, s| c.compile_with(s, with)),
|
2022-10-14 03:34:59 +02:00
|
|
|
ast::Expr::Lambda(lambda) => {
|
|
|
|
self.compile_lambda_or_thunk(false, slot, lambda, |c, s| {
|
|
|
|
c.compile_lambda(s, lambda)
|
|
|
|
})
|
|
|
|
}
|
2022-09-03 04:02:20 +02:00
|
|
|
ast::Expr::Apply(apply) => {
|
2022-10-03 16:08:39 +02:00
|
|
|
self.thunk(slot, apply, move |c, s| c.compile_apply(s, apply))
|
2022-09-03 04:02:20 +02:00
|
|
|
}
|
2022-08-16 22:43:45 +02:00
|
|
|
|
|
|
|
// Parenthesized expressions are simply unwrapped, leaving
|
|
|
|
// their value on the stack.
|
2022-10-03 16:08:39 +02:00
|
|
|
ast::Expr::Paren(paren) => self.compile(slot, &paren.expr().unwrap()),
|
2022-08-16 22:43:45 +02:00
|
|
|
|
2022-09-16 18:48:30 +02:00
|
|
|
ast::Expr::LegacyLet(legacy_let) => self.compile_legacy_let(slot, legacy_let),
|
2022-08-16 22:43:45 +02:00
|
|
|
|
|
|
|
ast::Expr::Root(_) => unreachable!("there cannot be more than one root"),
|
|
|
|
ast::Expr::Error(_) => unreachable!("compile is only called on validated trees"),
|
2022-08-07 22:41:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-03 16:08:39 +02:00
|
|
|
fn compile_literal(&mut self, node: &ast::Literal) {
|
2022-09-17 18:08:02 +02:00
|
|
|
let value = match node.kind() {
|
|
|
|
ast::LiteralKind::Float(f) => Value::Float(f.value().unwrap()),
|
2022-09-18 18:06:11 +02:00
|
|
|
ast::LiteralKind::Integer(i) => match i.value() {
|
|
|
|
Ok(v) => Value::Integer(v),
|
2022-10-03 16:08:39 +02:00
|
|
|
Err(err) => return self.emit_error(node, err.into()),
|
2022-09-18 18:06:11 +02:00
|
|
|
},
|
2022-09-01 18:57:55 +02:00
|
|
|
|
2022-08-16 22:43:45 +02:00
|
|
|
ast::LiteralKind::Uri(u) => {
|
2022-10-03 16:08:39 +02:00
|
|
|
self.emit_warning(node, WarningKind::DeprecatedLiteralURL);
|
2022-09-17 18:08:02 +02:00
|
|
|
Value::String(u.syntax().text().into())
|
2022-08-12 15:34:39 +02:00
|
|
|
}
|
2022-09-17 18:08:02 +02:00
|
|
|
};
|
|
|
|
|
2022-10-03 16:08:39 +02:00
|
|
|
self.emit_constant(value, node);
|
2022-08-07 22:41:42 +02:00
|
|
|
}
|
2022-08-08 01:16:28 +02:00
|
|
|
|
2022-10-10 05:46:51 +02:00
|
|
|
fn compile_path(&mut self, slot: LocalIdx, node: &ast::Path) {
|
2022-08-16 22:43:45 +02:00
|
|
|
// TODO(tazjin): placeholder implementation while waiting for
|
|
|
|
// https://github.com/nix-community/rnix-parser/pull/96
|
|
|
|
|
|
|
|
let raw_path = node.to_string();
|
|
|
|
let path = if raw_path.starts_with('/') {
|
|
|
|
Path::new(&raw_path).to_owned()
|
|
|
|
} else if raw_path.starts_with('~') {
|
2022-10-15 16:42:27 +02:00
|
|
|
return self.thunk(slot, node, move |c, _| {
|
|
|
|
// We assume that paths that home paths start with ~/ or fail to parse
|
|
|
|
// TODO: this should be checked using a parse-fail test.
|
|
|
|
debug_assert!(raw_path.len() > 2 && raw_path.starts_with("~/"));
|
|
|
|
|
|
|
|
let home_relative_path = &raw_path[2..(raw_path.len())];
|
|
|
|
c.emit_constant(Value::UnresolvedPath(home_relative_path.into()), node);
|
|
|
|
c.push_op(OpCode::OpResolveHomePath, node);
|
|
|
|
});
|
2022-08-16 22:43:45 +02:00
|
|
|
} else if raw_path.starts_with('.') {
|
|
|
|
let mut buf = self.root_dir.clone();
|
|
|
|
buf.push(&raw_path);
|
|
|
|
buf
|
2022-10-10 05:46:51 +02:00
|
|
|
} else if raw_path.starts_with('<') {
|
2022-08-16 22:43:45 +02:00
|
|
|
// TODO: decide what to do with findFile
|
2022-10-10 05:46:51 +02:00
|
|
|
if raw_path.len() == 2 {
|
|
|
|
return self.emit_error(
|
|
|
|
node,
|
2022-10-21 00:52:36 +02:00
|
|
|
ErrorKind::NixPathResolution("Empty <> path not allowed".into()),
|
2022-10-10 05:46:51 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
let path = &raw_path[1..(raw_path.len() - 1)];
|
|
|
|
// Make a thunk to resolve the path (without using `findFile`, at least for now?)
|
|
|
|
return self.thunk(slot, node, move |c, _| {
|
2022-10-15 16:21:14 +02:00
|
|
|
c.emit_constant(Value::UnresolvedPath(path.into()), node);
|
2022-10-10 05:46:51 +02:00
|
|
|
c.push_op(OpCode::OpFindFile, node);
|
|
|
|
});
|
|
|
|
} else {
|
2022-09-11 22:12:02 +02:00
|
|
|
self.emit_error(
|
2022-10-03 16:08:39 +02:00
|
|
|
node,
|
2022-10-10 05:46:51 +02:00
|
|
|
ErrorKind::NotImplemented("other path types not yet implemented"),
|
2022-09-11 22:12:02 +02:00
|
|
|
);
|
|
|
|
return;
|
2022-08-12 17:04:05 +02:00
|
|
|
};
|
|
|
|
|
2022-08-12 17:52:48 +02:00
|
|
|
// TODO: Use https://github.com/rust-lang/rfcs/issues/2208
|
|
|
|
// once it is available
|
2022-10-13 06:58:42 +02:00
|
|
|
let value = Value::Path(crate::value::canon_path(path));
|
2022-10-03 16:08:39 +02:00
|
|
|
self.emit_constant(value, node);
|
2022-08-12 17:04:05 +02:00
|
|
|
}
|
|
|
|
|
2022-09-15 16:38:35 +02:00
|
|
|
/// Helper that compiles the given string parts strictly. The caller
|
|
|
|
/// (`compile_str`) needs to figure out if the result of compiling this
|
|
|
|
/// needs to be thunked or not.
|
|
|
|
fn compile_str_parts(
|
|
|
|
&mut self,
|
|
|
|
slot: LocalIdx,
|
|
|
|
parent_node: &ast::Str,
|
|
|
|
parts: Vec<ast::InterpolPart<String>>,
|
|
|
|
) {
|
|
|
|
// The string parts are produced in literal order, however
|
|
|
|
// they need to be reversed on the stack in order to
|
|
|
|
// efficiently create the real string in case of
|
|
|
|
// interpolation.
|
2022-09-15 17:42:26 +02:00
|
|
|
for part in parts.iter().rev() {
|
2022-09-15 16:38:35 +02:00
|
|
|
match part {
|
|
|
|
// Interpolated expressions are compiled as normal and
|
|
|
|
// dealt with by the VM before being assembled into
|
|
|
|
// the final string. We need to coerce them here,
|
|
|
|
// so OpInterpolate definitely has a string to consume.
|
|
|
|
ast::InterpolPart::Interpolation(ipol) => {
|
2022-10-03 16:08:39 +02:00
|
|
|
self.compile(slot, &ipol.expr().unwrap());
|
2022-09-15 16:38:35 +02:00
|
|
|
// implicitly forces as well
|
2022-09-15 17:42:26 +02:00
|
|
|
self.push_op(OpCode::OpCoerceToString, ipol);
|
2022-09-12 18:27:00 +02:00
|
|
|
}
|
2022-08-09 16:37:38 +02:00
|
|
|
|
2022-08-22 20:04:50 +02:00
|
|
|
ast::InterpolPart::Literal(lit) => {
|
2022-09-15 17:42:26 +02:00
|
|
|
self.emit_constant(Value::String(lit.as_str().into()), parent_node);
|
2022-08-09 16:37:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-09-15 16:38:35 +02:00
|
|
|
|
2022-09-15 17:42:26 +02:00
|
|
|
if parts.len() != 1 {
|
|
|
|
self.push_op(OpCode::OpInterpolate(Count(parts.len())), parent_node);
|
2022-09-15 16:38:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-03 16:08:39 +02:00
|
|
|
fn compile_str(&mut self, slot: LocalIdx, node: &ast::Str) {
|
2022-09-15 16:38:35 +02:00
|
|
|
let parts = node.normalized_parts();
|
|
|
|
|
|
|
|
// We need to thunk string expressions if they are the result of
|
|
|
|
// interpolation. A string that only consists of a single part (`"${foo}"`)
|
|
|
|
// can't desugar to the enclosed expression (`foo`) because we need to
|
|
|
|
// coerce the result to a string value. This would require forcing the
|
|
|
|
// value of the inner expression, so we need to wrap it in another thunk.
|
|
|
|
if parts.len() != 1 || matches!(&parts[0], ast::InterpolPart::Interpolation(_)) {
|
2022-10-03 16:08:39 +02:00
|
|
|
self.thunk(slot, node, move |c, s| {
|
|
|
|
c.compile_str_parts(s, node, parts);
|
2022-09-15 16:38:35 +02:00
|
|
|
});
|
|
|
|
} else {
|
2022-10-03 16:08:39 +02:00
|
|
|
self.compile_str_parts(slot, node, parts);
|
2022-09-15 16:38:35 +02:00
|
|
|
}
|
2022-08-09 16:37:38 +02:00
|
|
|
}
|
|
|
|
|
2022-10-03 16:08:39 +02:00
|
|
|
fn compile_unary_op(&mut self, slot: LocalIdx, op: &ast::UnaryOp) {
|
|
|
|
self.compile(slot, &op.expr().unwrap());
|
|
|
|
self.emit_force(op);
|
2022-08-16 22:43:45 +02:00
|
|
|
|
|
|
|
let opcode = match op.operator().unwrap() {
|
|
|
|
ast::UnaryOpKind::Invert => OpCode::OpInvert,
|
|
|
|
ast::UnaryOpKind::Negate => OpCode::OpNegate,
|
|
|
|
};
|
|
|
|
|
2022-10-03 16:08:39 +02:00
|
|
|
self.push_op(opcode, op);
|
2022-08-16 22:43:45 +02:00
|
|
|
}
|
|
|
|
|
2022-10-03 16:08:39 +02:00
|
|
|
fn compile_binop(&mut self, slot: LocalIdx, op: &ast::BinOp) {
|
2022-08-16 22:43:45 +02:00
|
|
|
use ast::BinOpKind;
|
|
|
|
|
2022-08-11 16:06:23 +02:00
|
|
|
// Short-circuiting and other strange operators, which are
|
|
|
|
// under the same node type as NODE_BIN_OP, but need to be
|
|
|
|
// handled separately (i.e. before compiling the expressions
|
|
|
|
// used for standard binary operators).
|
2022-08-16 22:43:45 +02:00
|
|
|
|
2022-08-11 13:12:58 +02:00
|
|
|
match op.operator().unwrap() {
|
2022-08-29 20:51:08 +02:00
|
|
|
BinOpKind::And => return self.compile_and(slot, op),
|
|
|
|
BinOpKind::Or => return self.compile_or(slot, op),
|
|
|
|
BinOpKind::Implication => return self.compile_implication(slot, op),
|
2022-08-11 13:12:58 +02:00
|
|
|
_ => {}
|
|
|
|
};
|
|
|
|
|
2022-08-16 22:43:45 +02:00
|
|
|
// For all other operators, the two values need to be left on
|
|
|
|
// the stack in the correct order before pushing the
|
|
|
|
// instruction for the operation itself.
|
2022-10-03 16:08:39 +02:00
|
|
|
self.compile(slot, &op.lhs().unwrap());
|
2022-09-01 18:13:49 +02:00
|
|
|
self.emit_force(&op.lhs().unwrap());
|
2022-08-29 20:57:28 +02:00
|
|
|
|
2022-10-03 16:08:39 +02:00
|
|
|
self.compile(slot, &op.rhs().unwrap());
|
2022-09-01 18:13:49 +02:00
|
|
|
self.emit_force(&op.rhs().unwrap());
|
2022-08-08 01:16:28 +02:00
|
|
|
|
2022-08-10 20:09:59 +02:00
|
|
|
match op.operator().unwrap() {
|
2022-10-03 16:08:39 +02:00
|
|
|
BinOpKind::Add => self.push_op(OpCode::OpAdd, op),
|
|
|
|
BinOpKind::Sub => self.push_op(OpCode::OpSub, op),
|
|
|
|
BinOpKind::Mul => self.push_op(OpCode::OpMul, op),
|
|
|
|
BinOpKind::Div => self.push_op(OpCode::OpDiv, op),
|
|
|
|
BinOpKind::Update => self.push_op(OpCode::OpAttrsUpdate, op),
|
|
|
|
BinOpKind::Equal => self.push_op(OpCode::OpEqual, op),
|
|
|
|
BinOpKind::Less => self.push_op(OpCode::OpLess, op),
|
|
|
|
BinOpKind::LessOrEq => self.push_op(OpCode::OpLessOrEq, op),
|
|
|
|
BinOpKind::More => self.push_op(OpCode::OpMore, op),
|
|
|
|
BinOpKind::MoreOrEq => self.push_op(OpCode::OpMoreOrEq, op),
|
|
|
|
BinOpKind::Concat => self.push_op(OpCode::OpConcat, op),
|
2022-08-10 20:09:59 +02:00
|
|
|
|
|
|
|
BinOpKind::NotEqual => {
|
2022-10-03 16:08:39 +02:00
|
|
|
self.push_op(OpCode::OpEqual, op);
|
|
|
|
self.push_op(OpCode::OpInvert, op)
|
2022-08-10 20:09:59 +02:00
|
|
|
}
|
|
|
|
|
2022-08-11 13:12:58 +02:00
|
|
|
// Handled by separate branch above.
|
2022-08-16 22:43:45 +02:00
|
|
|
BinOpKind::And | BinOpKind::Implication | BinOpKind::Or => {
|
2022-08-11 16:06:23 +02:00
|
|
|
unreachable!()
|
|
|
|
}
|
2022-08-08 01:16:28 +02:00
|
|
|
};
|
|
|
|
}
|
2022-08-08 01:32:07 +02:00
|
|
|
|
2022-10-03 16:08:39 +02:00
|
|
|
fn compile_and(&mut self, slot: LocalIdx, node: &ast::BinOp) {
|
2022-08-16 22:43:45 +02:00
|
|
|
debug_assert!(
|
|
|
|
matches!(node.operator(), Some(ast::BinOpKind::And)),
|
|
|
|
"compile_and called with wrong operator kind: {:?}",
|
|
|
|
node.operator(),
|
|
|
|
);
|
2022-08-08 01:32:07 +02:00
|
|
|
|
2022-08-16 22:43:45 +02:00
|
|
|
// Leave left-hand side value on the stack.
|
2022-10-03 16:08:39 +02:00
|
|
|
self.compile(slot, &node.lhs().unwrap());
|
2022-09-01 18:13:49 +02:00
|
|
|
self.emit_force(&node.lhs().unwrap());
|
2022-08-16 22:43:45 +02:00
|
|
|
|
|
|
|
// If this value is false, jump over the right-hand side - the
|
|
|
|
// whole expression is false.
|
2022-10-03 16:08:39 +02:00
|
|
|
let end_idx = self.push_op(OpCode::OpJumpIfFalse(JumpOffset(0)), node);
|
2022-08-16 22:43:45 +02:00
|
|
|
|
|
|
|
// Otherwise, remove the previous value and leave the
|
|
|
|
// right-hand side on the stack. Its result is now the value
|
|
|
|
// of the whole expression.
|
2022-10-03 16:08:39 +02:00
|
|
|
self.push_op(OpCode::OpPop, node);
|
|
|
|
self.compile(slot, &node.rhs().unwrap());
|
2022-09-01 18:13:49 +02:00
|
|
|
self.emit_force(&node.rhs().unwrap());
|
2022-08-16 22:43:45 +02:00
|
|
|
|
|
|
|
self.patch_jump(end_idx);
|
2022-10-03 16:08:39 +02:00
|
|
|
self.push_op(OpCode::OpAssertBool, node);
|
2022-08-08 01:32:07 +02:00
|
|
|
}
|
2022-08-08 02:07:56 +02:00
|
|
|
|
2022-10-03 16:08:39 +02:00
|
|
|
fn compile_or(&mut self, slot: LocalIdx, node: &ast::BinOp) {
|
2022-08-16 22:43:45 +02:00
|
|
|
debug_assert!(
|
|
|
|
matches!(node.operator(), Some(ast::BinOpKind::Or)),
|
|
|
|
"compile_or called with wrong operator kind: {:?}",
|
|
|
|
node.operator(),
|
|
|
|
);
|
2022-08-08 02:07:56 +02:00
|
|
|
|
2022-08-16 22:43:45 +02:00
|
|
|
// Leave left-hand side value on the stack
|
2022-10-03 16:08:39 +02:00
|
|
|
self.compile(slot, &node.lhs().unwrap());
|
2022-09-01 18:13:49 +02:00
|
|
|
self.emit_force(&node.lhs().unwrap());
|
2022-08-15 00:31:02 +02:00
|
|
|
|
2022-08-16 22:43:45 +02:00
|
|
|
// Opposite of above: If this value is **true**, we can
|
|
|
|
// short-circuit the right-hand side.
|
2022-10-03 16:08:39 +02:00
|
|
|
let end_idx = self.push_op(OpCode::OpJumpIfTrue(JumpOffset(0)), node);
|
|
|
|
self.push_op(OpCode::OpPop, node);
|
|
|
|
self.compile(slot, &node.rhs().unwrap());
|
2022-09-01 18:13:49 +02:00
|
|
|
self.emit_force(&node.rhs().unwrap());
|
2022-08-29 20:57:28 +02:00
|
|
|
|
2022-08-16 22:43:45 +02:00
|
|
|
self.patch_jump(end_idx);
|
2022-10-03 16:08:39 +02:00
|
|
|
self.push_op(OpCode::OpAssertBool, node);
|
2022-08-16 22:43:45 +02:00
|
|
|
}
|
|
|
|
|
2022-10-03 16:08:39 +02:00
|
|
|
fn compile_implication(&mut self, slot: LocalIdx, node: &ast::BinOp) {
|
2022-08-16 22:43:45 +02:00
|
|
|
debug_assert!(
|
|
|
|
matches!(node.operator(), Some(ast::BinOpKind::Implication)),
|
|
|
|
"compile_implication called with wrong operator kind: {:?}",
|
|
|
|
node.operator(),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Leave left-hand side value on the stack and invert it.
|
2022-10-03 16:08:39 +02:00
|
|
|
self.compile(slot, &node.lhs().unwrap());
|
2022-09-01 18:13:49 +02:00
|
|
|
self.emit_force(&node.lhs().unwrap());
|
2022-10-03 16:08:39 +02:00
|
|
|
self.push_op(OpCode::OpInvert, node);
|
2022-08-16 22:43:45 +02:00
|
|
|
|
|
|
|
// Exactly as `||` (because `a -> b` = `!a || b`).
|
2022-10-03 16:08:39 +02:00
|
|
|
let end_idx = self.push_op(OpCode::OpJumpIfTrue(JumpOffset(0)), node);
|
|
|
|
self.push_op(OpCode::OpPop, node);
|
|
|
|
self.compile(slot, &node.rhs().unwrap());
|
2022-09-01 18:13:49 +02:00
|
|
|
self.emit_force(&node.rhs().unwrap());
|
2022-08-29 20:57:28 +02:00
|
|
|
|
2022-08-16 22:43:45 +02:00
|
|
|
self.patch_jump(end_idx);
|
2022-10-03 16:08:39 +02:00
|
|
|
self.push_op(OpCode::OpAssertBool, node);
|
2022-08-16 22:43:45 +02:00
|
|
|
}
|
|
|
|
|
2022-09-05 00:30:58 +02:00
|
|
|
/// Compile list literals into equivalent bytecode. List
|
|
|
|
/// construction is fairly simple, consisting of pushing code for
|
|
|
|
/// each literal element and an instruction with the element
|
|
|
|
/// count.
|
|
|
|
///
|
|
|
|
/// The VM, after evaluating the code for each element, simply
|
|
|
|
/// constructs the list from the given number of elements.
|
2022-10-03 16:08:39 +02:00
|
|
|
fn compile_list(&mut self, slot: LocalIdx, node: &ast::List) {
|
2022-08-16 22:43:45 +02:00
|
|
|
let mut count = 0;
|
|
|
|
|
2022-09-06 19:43:49 +02:00
|
|
|
// Open a temporary scope to correctly account for stack items
|
|
|
|
// that exist during the construction.
|
2022-09-13 15:04:52 +02:00
|
|
|
self.scope_mut().begin_scope();
|
2022-09-06 19:43:49 +02:00
|
|
|
|
2022-08-16 22:43:45 +02:00
|
|
|
for item in node.items() {
|
2022-09-06 19:43:49 +02:00
|
|
|
// Start tracing new stack slots from the second list
|
|
|
|
// element onwards. The first list element is located in
|
|
|
|
// the stack slot of the list itself.
|
|
|
|
let item_slot = match count {
|
|
|
|
0 => slot,
|
|
|
|
_ => {
|
|
|
|
let item_span = self.span_for(&item);
|
|
|
|
self.scope_mut().declare_phantom(item_span, false)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-08-16 22:43:45 +02:00
|
|
|
count += 1;
|
2022-10-03 16:08:39 +02:00
|
|
|
self.compile(item_slot, &item);
|
2022-09-06 19:43:49 +02:00
|
|
|
self.scope_mut().mark_initialised(item_slot);
|
2022-08-16 22:43:45 +02:00
|
|
|
}
|
|
|
|
|
2022-10-03 16:08:39 +02:00
|
|
|
self.push_op(OpCode::OpList(Count(count)), node);
|
2022-09-06 19:43:49 +02:00
|
|
|
self.scope_mut().end_scope();
|
2022-08-16 22:43:45 +02:00
|
|
|
}
|
|
|
|
|
2022-10-03 16:08:39 +02:00
|
|
|
fn compile_attr(&mut self, slot: LocalIdx, node: &ast::Attr) {
|
2022-09-23 15:05:14 +02:00
|
|
|
match node {
|
|
|
|
ast::Attr::Dynamic(dynamic) => {
|
2022-10-03 16:08:39 +02:00
|
|
|
self.compile(slot, &dynamic.expr().unwrap());
|
2022-09-23 15:05:14 +02:00
|
|
|
self.emit_force(&dynamic.expr().unwrap());
|
|
|
|
}
|
|
|
|
|
|
|
|
ast::Attr::Str(s) => {
|
2022-10-03 16:08:39 +02:00
|
|
|
self.compile_str(slot, s);
|
|
|
|
self.emit_force(s);
|
2022-09-23 15:05:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ast::Attr::Ident(ident) => self.emit_literal_ident(&ident),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-03 16:08:39 +02:00
|
|
|
fn compile_has_attr(&mut self, slot: LocalIdx, node: &ast::HasAttr) {
|
2022-09-23 15:05:14 +02:00
|
|
|
// Put the attribute set on the stack.
|
2022-10-03 16:08:39 +02:00
|
|
|
self.compile(slot, &node.expr().unwrap());
|
|
|
|
self.emit_force(node);
|
2022-09-23 15:05:14 +02:00
|
|
|
|
|
|
|
// Push all path fragments with an operation for fetching the
|
|
|
|
// next nested element, for all fragments except the last one.
|
|
|
|
for (count, fragment) in node.attrpath().unwrap().attrs().enumerate() {
|
|
|
|
if count > 0 {
|
|
|
|
self.push_op(OpCode::OpAttrsTrySelect, &fragment);
|
|
|
|
self.emit_force(&fragment);
|
|
|
|
}
|
|
|
|
|
2022-10-03 16:08:39 +02:00
|
|
|
self.compile_attr(slot, &fragment);
|
2022-09-23 15:05:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// After the last fragment, emit the actual instruction that
|
|
|
|
// leaves a boolean on the stack.
|
2022-10-03 16:08:39 +02:00
|
|
|
self.push_op(OpCode::OpHasAttr, node);
|
2022-09-23 15:05:14 +02:00
|
|
|
}
|
|
|
|
|
2022-10-03 16:08:39 +02:00
|
|
|
fn compile_select(&mut self, slot: LocalIdx, node: &ast::Select) {
|
2022-09-23 15:05:14 +02:00
|
|
|
let set = node.expr().unwrap();
|
|
|
|
let path = node.attrpath().unwrap();
|
|
|
|
|
|
|
|
if node.or_token().is_some() {
|
|
|
|
self.compile_select_or(slot, set, path, node.default_expr().unwrap());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Push the set onto the stack
|
2022-10-03 16:08:39 +02:00
|
|
|
self.compile(slot, &set);
|
2022-09-23 15:05:14 +02:00
|
|
|
|
|
|
|
// Compile each key fragment and emit access instructions.
|
|
|
|
//
|
|
|
|
// TODO: multi-select instruction to avoid re-pushing attrs on
|
|
|
|
// nested selects.
|
|
|
|
for fragment in path.attrs() {
|
|
|
|
// Force the current set value.
|
|
|
|
self.emit_force(&fragment);
|
|
|
|
|
2022-10-03 16:08:39 +02:00
|
|
|
self.compile_attr(slot, &fragment);
|
2022-09-23 15:05:14 +02:00
|
|
|
self.push_op(OpCode::OpAttrsSelect, &fragment);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Compile an `or` expression into a chunk of conditional jumps.
|
|
|
|
///
|
|
|
|
/// If at any point during attribute set traversal a key is
|
|
|
|
/// missing, the `OpAttrOrNotFound` instruction will leave a
|
|
|
|
/// special sentinel value on the stack.
|
|
|
|
///
|
|
|
|
/// After each access, a conditional jump evaluates the top of the
|
|
|
|
/// stack and short-circuits to the default value if it sees the
|
|
|
|
/// sentinel.
|
|
|
|
///
|
|
|
|
/// Code like `{ a.b = 1; }.a.c or 42` yields this bytecode and
|
|
|
|
/// runtime stack:
|
|
|
|
///
|
|
|
|
/// ```notrust
|
|
|
|
/// Bytecode Runtime stack
|
|
|
|
/// ┌────────────────────────────┐ ┌─────────────────────────┐
|
|
|
|
/// │ ... │ │ ... │
|
|
|
|
/// │ 5 OP_ATTRS(1) │ → │ 5 [ { a.b = 1; } ] │
|
|
|
|
/// │ 6 OP_CONSTANT("a") │ → │ 6 [ { a.b = 1; } "a" ] │
|
|
|
|
/// │ 7 OP_ATTR_OR_NOT_FOUND │ → │ 7 [ { b = 1; } ] │
|
|
|
|
/// │ 8 JUMP_IF_NOT_FOUND(13) │ → │ 8 [ { b = 1; } ] │
|
|
|
|
/// │ 9 OP_CONSTANT("C") │ → │ 9 [ { b = 1; } "c" ] │
|
|
|
|
/// │ 10 OP_ATTR_OR_NOT_FOUND │ → │ 10 [ NOT_FOUND ] │
|
|
|
|
/// │ 11 JUMP_IF_NOT_FOUND(13) │ → │ 11 [ ] │
|
|
|
|
/// │ 12 JUMP(14) │ │ .. jumped over │
|
|
|
|
/// │ 13 CONSTANT(42) │ → │ 12 [ 42 ] │
|
|
|
|
/// │ 14 ... │ │ .. .... │
|
|
|
|
/// └────────────────────────────┘ └─────────────────────────┘
|
|
|
|
/// ```
|
|
|
|
fn compile_select_or(
|
|
|
|
&mut self,
|
|
|
|
slot: LocalIdx,
|
|
|
|
set: ast::Expr,
|
|
|
|
path: ast::Attrpath,
|
|
|
|
default: ast::Expr,
|
|
|
|
) {
|
2022-10-03 16:08:39 +02:00
|
|
|
self.compile(slot, &set);
|
2022-09-23 15:05:14 +02:00
|
|
|
let mut jumps = vec![];
|
|
|
|
|
|
|
|
for fragment in path.attrs() {
|
|
|
|
self.emit_force(&fragment);
|
2022-10-03 16:08:39 +02:00
|
|
|
self.compile_attr(slot, &fragment.clone());
|
2022-09-23 15:05:14 +02:00
|
|
|
self.push_op(OpCode::OpAttrsTrySelect, &fragment);
|
|
|
|
jumps.push(self.push_op(OpCode::OpJumpIfNotFound(JumpOffset(0)), &fragment));
|
|
|
|
}
|
|
|
|
|
|
|
|
let final_jump = self.push_op(OpCode::OpJump(JumpOffset(0)), &path);
|
|
|
|
|
|
|
|
for jump in jumps {
|
|
|
|
self.patch_jump(jump);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compile the default value expression and patch the final
|
|
|
|
// jump to point *beyond* it.
|
2022-10-03 16:08:39 +02:00
|
|
|
self.compile(slot, &default);
|
2022-09-23 15:05:14 +02:00
|
|
|
self.patch_jump(final_jump);
|
|
|
|
}
|
|
|
|
|
2022-10-10 18:56:11 +02:00
|
|
|
/// Compile `assert` expressions using jumping instructions in the VM.
|
|
|
|
///
|
|
|
|
/// ```notrust
|
|
|
|
/// ┌─────────────────────┐
|
|
|
|
/// │ 0 [ conditional ] │
|
|
|
|
/// │ 1 JUMP_IF_FALSE →┼─┐
|
|
|
|
/// │ 2 [ main body ] │ │ Jump to else body if
|
|
|
|
/// ┌┼─3─← JUMP │ │ condition is false.
|
|
|
|
/// Jump over else body ││ 4 OP_ASSERT_FAIL ←┼─┘
|
|
|
|
/// if condition is true.└┼─5─→ ... │
|
|
|
|
/// └─────────────────────┘
|
|
|
|
/// ```
|
2022-10-03 16:08:39 +02:00
|
|
|
fn compile_assert(&mut self, slot: LocalIdx, node: &ast::Assert) {
|
2022-08-16 22:43:45 +02:00
|
|
|
// Compile the assertion condition to leave its value on the stack.
|
2022-10-03 16:08:39 +02:00
|
|
|
self.compile(slot, &node.condition().unwrap());
|
2022-09-19 13:09:03 +02:00
|
|
|
self.emit_force(&node.condition().unwrap());
|
2022-10-10 18:56:11 +02:00
|
|
|
let then_idx = self.push_op(OpCode::OpJumpIfFalse(JumpOffset(0)), node);
|
2022-08-16 22:43:45 +02:00
|
|
|
|
2022-10-10 18:56:11 +02:00
|
|
|
self.push_op(OpCode::OpPop, node);
|
2022-10-03 16:08:39 +02:00
|
|
|
self.compile(slot, &node.body().unwrap());
|
2022-10-10 18:56:11 +02:00
|
|
|
|
|
|
|
let else_idx = self.push_op(OpCode::OpJump(JumpOffset(0)), node);
|
|
|
|
|
|
|
|
self.patch_jump(then_idx);
|
|
|
|
self.push_op(OpCode::OpPop, node);
|
|
|
|
self.push_op(OpCode::OpAssertFail, &node.condition().unwrap());
|
|
|
|
|
|
|
|
self.patch_jump(else_idx);
|
2022-08-16 22:43:45 +02:00
|
|
|
}
|
|
|
|
|
2022-09-05 00:30:58 +02:00
|
|
|
/// Compile conditional expressions using jumping instructions in the VM.
|
|
|
|
///
|
|
|
|
/// ```notrust
|
|
|
|
/// ┌────────────────────┐
|
|
|
|
/// │ 0 [ conditional ] │
|
|
|
|
/// │ 1 JUMP_IF_FALSE →┼─┐
|
|
|
|
/// │ 2 [ main body ] │ │ Jump to else body if
|
|
|
|
/// ┌┼─3─← JUMP │ │ condition is false.
|
|
|
|
/// Jump over else body ││ 4 [ else body ]←┼─┘
|
|
|
|
/// if condition is true.└┼─5─→ ... │
|
|
|
|
/// └────────────────────┘
|
|
|
|
/// ```
|
2022-10-03 16:08:39 +02:00
|
|
|
fn compile_if_else(&mut self, slot: LocalIdx, node: &ast::IfElse) {
|
|
|
|
self.compile(slot, &node.condition().unwrap());
|
2022-09-03 15:18:06 +02:00
|
|
|
self.emit_force(&node.condition().unwrap());
|
2022-08-16 22:43:45 +02:00
|
|
|
|
2022-09-01 16:36:13 +02:00
|
|
|
let then_idx = self.push_op(
|
|
|
|
OpCode::OpJumpIfFalse(JumpOffset(0)),
|
|
|
|
&node.condition().unwrap(),
|
|
|
|
);
|
2022-08-16 22:43:45 +02:00
|
|
|
|
2022-10-03 16:08:39 +02:00
|
|
|
self.push_op(OpCode::OpPop, node); // discard condition value
|
|
|
|
self.compile(slot, &node.body().unwrap());
|
2022-08-16 22:43:45 +02:00
|
|
|
|
2022-10-03 16:08:39 +02:00
|
|
|
let else_idx = self.push_op(OpCode::OpJump(JumpOffset(0)), node);
|
2022-08-16 22:43:45 +02:00
|
|
|
|
|
|
|
self.patch_jump(then_idx); // patch jump *to* else_body
|
2022-10-03 16:08:39 +02:00
|
|
|
self.push_op(OpCode::OpPop, node); // discard condition value
|
|
|
|
self.compile(slot, &node.else_body().unwrap());
|
2022-08-16 22:43:45 +02:00
|
|
|
|
|
|
|
self.patch_jump(else_idx); // patch jump *over* else body
|
|
|
|
}
|
|
|
|
|
2022-09-05 00:30:58 +02:00
|
|
|
/// Compile `with` expressions by emitting instructions that
|
|
|
|
/// pop/remove the indices of attribute sets that are implicitly
|
|
|
|
/// in scope through `with` on the "with-stack".
|
2022-10-03 16:08:39 +02:00
|
|
|
fn compile_with(&mut self, slot: LocalIdx, node: &ast::With) {
|
2022-09-13 15:04:52 +02:00
|
|
|
self.scope_mut().begin_scope();
|
2022-08-14 23:13:57 +02:00
|
|
|
// TODO: Detect if the namespace is just an identifier, and
|
|
|
|
// resolve that directly (thus avoiding duplication on the
|
|
|
|
// stack).
|
2022-10-03 16:08:39 +02:00
|
|
|
self.compile(slot, &node.namespace().unwrap());
|
2022-08-30 18:55:04 +02:00
|
|
|
|
2022-09-01 18:57:55 +02:00
|
|
|
let span = self.span_for(&node.namespace().unwrap());
|
2022-09-02 23:26:32 +02:00
|
|
|
|
|
|
|
// The attribute set from which `with` inherits values
|
|
|
|
// occupies a slot on the stack, but this stack slot is not
|
|
|
|
// directly accessible. As it must be accounted for to
|
|
|
|
// calculate correct offsets, what we call a "phantom" local
|
|
|
|
// is declared here.
|
2022-09-06 16:12:04 +02:00
|
|
|
let local_idx = self.scope_mut().declare_phantom(span, true);
|
2022-08-28 18:38:17 +02:00
|
|
|
let with_idx = self.scope().stack_index(local_idx);
|
2022-08-27 17:59:31 +02:00
|
|
|
|
2022-08-27 18:55:02 +02:00
|
|
|
self.scope_mut().push_with();
|
2022-08-14 23:13:57 +02:00
|
|
|
|
2022-09-13 15:10:27 +02:00
|
|
|
self.push_op(OpCode::OpPushWith(with_idx), &node.namespace().unwrap());
|
2022-08-14 23:13:57 +02:00
|
|
|
|
2022-10-03 16:08:39 +02:00
|
|
|
self.compile(slot, &node.body().unwrap());
|
2022-08-27 01:59:03 +02:00
|
|
|
|
2022-10-03 16:08:39 +02:00
|
|
|
self.push_op(OpCode::OpPopWith, node);
|
2022-08-27 18:55:02 +02:00
|
|
|
self.scope_mut().pop_with();
|
2022-10-03 16:08:39 +02:00
|
|
|
self.cleanup_scope(node);
|
2022-08-14 23:13:57 +02:00
|
|
|
}
|
|
|
|
|
2022-09-05 04:12:50 +02:00
|
|
|
/// Compiles pattern function arguments, such as `{ a, b }: ...`.
|
|
|
|
///
|
|
|
|
/// These patterns are treated as a special case of locals binding
|
|
|
|
/// where the attribute set itself is placed on the first stack
|
|
|
|
/// slot of the call frame (either as a phantom, or named in case
|
|
|
|
/// of an `@` binding), and the function call sets up the rest of
|
|
|
|
/// the stack as if the parameters were rewritten into a `let`
|
|
|
|
/// binding.
|
|
|
|
///
|
|
|
|
/// For example:
|
|
|
|
///
|
|
|
|
/// ```nix
|
|
|
|
/// ({ a, b ? 2, c ? a * b, ... }@args: <body>) { a = 10; }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// would be compiled similarly to a binding such as
|
|
|
|
///
|
|
|
|
/// ```nix
|
|
|
|
/// let args = { a = 10; };
|
|
|
|
/// in let a = args.a;
|
|
|
|
/// b = args.a or 2;
|
|
|
|
/// c = args.c or a * b;
|
|
|
|
/// in <body>
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// The only tricky bit being that bindings have to fail if too
|
|
|
|
/// many arguments are provided. This is done by emitting a
|
|
|
|
/// special instruction that checks the set of keys from a
|
|
|
|
/// constant containing the expected keys.
|
2022-10-13 05:27:09 +02:00
|
|
|
fn compile_param_pattern(&mut self, pattern: &ast::Pattern) -> Formals {
|
2022-10-03 16:08:39 +02:00
|
|
|
let span = self.span_for(pattern);
|
2022-09-05 04:12:50 +02:00
|
|
|
let set_idx = match pattern.pat_bind() {
|
|
|
|
Some(name) => self.declare_local(&name, name.ident().unwrap().to_string()),
|
2022-09-06 16:12:04 +02:00
|
|
|
None => self.scope_mut().declare_phantom(span, true),
|
2022-09-05 04:12:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// At call time, the attribute set is already at the top of
|
|
|
|
// the stack.
|
|
|
|
self.scope_mut().mark_initialised(set_idx);
|
2022-10-03 16:08:39 +02:00
|
|
|
self.emit_force(pattern);
|
2022-09-05 04:12:50 +02:00
|
|
|
|
2022-10-13 05:53:03 +02:00
|
|
|
let ellipsis = pattern.ellipsis_token().is_some();
|
|
|
|
if !ellipsis {
|
|
|
|
self.push_op(OpCode::OpValidateClosedFormals, pattern);
|
|
|
|
}
|
|
|
|
|
2022-09-05 04:12:50 +02:00
|
|
|
// Similar to `let ... in ...`, we now do multiple passes over
|
|
|
|
// the bindings to first declare them, then populate them, and
|
|
|
|
// then finalise any necessary recursion into the scope.
|
|
|
|
let mut entries: Vec<(LocalIdx, ast::PatEntry)> = vec![];
|
|
|
|
let mut indices: Vec<LocalIdx> = vec![];
|
2022-10-13 05:27:09 +02:00
|
|
|
let mut arguments = HashMap::default();
|
2022-09-05 04:12:50 +02:00
|
|
|
|
|
|
|
for entry in pattern.pat_entries() {
|
|
|
|
let ident = entry.ident().unwrap();
|
|
|
|
let idx = self.declare_local(&ident, ident.to_string());
|
2022-10-13 05:27:09 +02:00
|
|
|
let has_default = entry.default().is_some();
|
2022-09-05 04:12:50 +02:00
|
|
|
entries.push((idx, entry));
|
|
|
|
indices.push(idx);
|
2022-10-13 05:27:09 +02:00
|
|
|
arguments.insert(ident.into(), has_default);
|
2022-09-05 04:12:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// For each of the bindings, push the set on the stack and
|
|
|
|
// attempt to select from it.
|
|
|
|
let stack_idx = self.scope().stack_index(set_idx);
|
|
|
|
for (idx, entry) in entries.into_iter() {
|
2022-10-03 16:08:39 +02:00
|
|
|
self.push_op(OpCode::OpGetLocal(stack_idx), pattern);
|
2022-09-05 04:12:50 +02:00
|
|
|
self.emit_literal_ident(&entry.ident().unwrap());
|
|
|
|
|
|
|
|
// Use the same mechanism as `compile_select_or` if a
|
|
|
|
// default value was provided, or simply select otherwise.
|
|
|
|
if let Some(default_expr) = entry.default() {
|
|
|
|
self.push_op(OpCode::OpAttrsTrySelect, &entry.ident().unwrap());
|
|
|
|
|
|
|
|
let jump_to_default =
|
|
|
|
self.push_op(OpCode::OpJumpIfNotFound(JumpOffset(0)), &default_expr);
|
|
|
|
|
|
|
|
let jump_over_default = self.push_op(OpCode::OpJump(JumpOffset(0)), &default_expr);
|
|
|
|
|
|
|
|
self.patch_jump(jump_to_default);
|
2022-10-03 16:08:39 +02:00
|
|
|
self.compile(idx, &default_expr);
|
2022-09-05 04:12:50 +02:00
|
|
|
self.patch_jump(jump_over_default);
|
|
|
|
} else {
|
|
|
|
self.push_op(OpCode::OpAttrsSelect, &entry.ident().unwrap());
|
|
|
|
}
|
|
|
|
|
|
|
|
self.scope_mut().mark_initialised(idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
for idx in indices {
|
|
|
|
if self.scope()[idx].needs_finaliser {
|
|
|
|
let stack_idx = self.scope().stack_index(idx);
|
2022-10-03 16:08:39 +02:00
|
|
|
self.push_op(OpCode::OpFinalise(stack_idx), pattern);
|
2022-09-05 04:12:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-13 05:27:09 +02:00
|
|
|
Formals {
|
|
|
|
arguments,
|
|
|
|
ellipsis,
|
2022-10-13 05:53:03 +02:00
|
|
|
span,
|
2022-10-13 05:27:09 +02:00
|
|
|
}
|
2022-09-05 04:12:50 +02:00
|
|
|
}
|
|
|
|
|
2022-10-14 03:34:59 +02:00
|
|
|
fn compile_lambda(&mut self, slot: LocalIdx, node: &ast::Lambda) {
|
2022-10-13 05:27:09 +02:00
|
|
|
// Compile the function itself, recording its formal arguments (if any)
|
|
|
|
// for later use
|
|
|
|
let formals = match node.param().unwrap() {
|
|
|
|
ast::Param::Pattern(pat) => Some(self.compile_param_pattern(&pat)),
|
2022-09-05 04:12:50 +02:00
|
|
|
|
2022-08-24 00:54:34 +02:00
|
|
|
ast::Param::IdentParam(param) => {
|
|
|
|
let name = param
|
|
|
|
.ident()
|
|
|
|
.unwrap()
|
|
|
|
.ident_token()
|
|
|
|
.unwrap()
|
|
|
|
.text()
|
|
|
|
.to_string();
|
|
|
|
|
2022-09-01 18:57:55 +02:00
|
|
|
let idx = self.declare_local(¶m, &name);
|
2022-08-28 18:38:17 +02:00
|
|
|
self.scope_mut().mark_initialised(idx);
|
2022-10-13 05:27:09 +02:00
|
|
|
None
|
2022-08-24 00:54:34 +02:00
|
|
|
}
|
2022-10-13 05:27:09 +02:00
|
|
|
};
|
2022-08-24 00:54:34 +02:00
|
|
|
|
2022-10-03 16:08:39 +02:00
|
|
|
self.compile(slot, &node.body().unwrap());
|
2022-10-13 05:27:09 +02:00
|
|
|
self.context_mut().lambda.formals = formals;
|
2022-10-14 03:34:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn thunk<N, F>(&mut self, outer_slot: LocalIdx, node: &N, content: F)
|
|
|
|
where
|
|
|
|
N: ToSpan,
|
|
|
|
F: FnOnce(&mut Compiler, LocalIdx),
|
|
|
|
{
|
|
|
|
self.compile_lambda_or_thunk(true, outer_slot, node, content)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Compile an expression into a runtime cloure or thunk
|
|
|
|
fn compile_lambda_or_thunk<N, F>(
|
|
|
|
&mut self,
|
2022-10-18 09:48:19 +02:00
|
|
|
is_suspended_thunk: bool,
|
2022-10-14 03:34:59 +02:00
|
|
|
outer_slot: LocalIdx,
|
|
|
|
node: &N,
|
|
|
|
content: F,
|
|
|
|
) where
|
|
|
|
N: ToSpan,
|
|
|
|
F: FnOnce(&mut Compiler, LocalIdx),
|
|
|
|
{
|
2022-10-22 22:55:21 +02:00
|
|
|
let name = self.scope()[outer_slot].name();
|
2022-10-14 03:34:59 +02:00
|
|
|
self.new_context();
|
2022-10-22 22:55:21 +02:00
|
|
|
|
|
|
|
// Set the (optional) name of the current slot on the lambda that is
|
|
|
|
// being compiled.
|
|
|
|
self.context_mut().lambda.name = name;
|
|
|
|
|
2022-10-14 03:34:59 +02:00
|
|
|
let span = self.span_for(node);
|
|
|
|
let slot = self.scope_mut().declare_phantom(span, false);
|
|
|
|
self.scope_mut().begin_scope();
|
|
|
|
|
|
|
|
content(self, slot);
|
2022-10-03 16:08:39 +02:00
|
|
|
self.cleanup_scope(node);
|
2022-08-24 00:54:34 +02:00
|
|
|
|
|
|
|
// TODO: determine and insert enclosing name, if available.
|
|
|
|
|
|
|
|
// Pop the lambda context back off, and emit the finished
|
|
|
|
// lambda as a constant.
|
2022-09-04 22:18:35 +02:00
|
|
|
let mut compiled = self.contexts.pop().unwrap();
|
|
|
|
|
|
|
|
// Check if tail-call optimisation is possible and perform it.
|
|
|
|
optimise_tail_call(&mut compiled.lambda.chunk);
|
|
|
|
|
2022-09-06 22:13:48 +02:00
|
|
|
// Capturing the with stack counts as an upvalue, as it is
|
|
|
|
// emitted as an upvalue data instruction.
|
|
|
|
if compiled.captures_with_stack {
|
|
|
|
compiled.lambda.upvalue_count += 1;
|
|
|
|
}
|
|
|
|
|
2022-09-03 13:52:24 +02:00
|
|
|
let lambda = Rc::new(compiled.lambda);
|
2022-10-18 09:48:19 +02:00
|
|
|
if is_suspended_thunk {
|
2022-10-14 03:34:59 +02:00
|
|
|
self.observer.observe_compiled_thunk(&lambda);
|
|
|
|
} else {
|
|
|
|
self.observer.observe_compiled_lambda(&lambda);
|
|
|
|
}
|
2022-08-24 00:54:34 +02:00
|
|
|
|
2022-10-14 03:34:59 +02:00
|
|
|
// If no upvalues are captured, emit directly and move on.
|
2022-09-03 13:52:24 +02:00
|
|
|
if lambda.upvalue_count == 0 {
|
2022-10-14 03:34:59 +02:00
|
|
|
self.emit_constant(
|
2022-10-18 09:48:19 +02:00
|
|
|
if is_suspended_thunk {
|
2022-10-16 01:10:10 +02:00
|
|
|
Value::Thunk(Thunk::new_suspended(lambda, span))
|
2022-10-14 03:34:59 +02:00
|
|
|
} else {
|
|
|
|
Value::Closure(Closure::new(lambda))
|
|
|
|
},
|
|
|
|
node,
|
|
|
|
);
|
2022-08-26 23:21:08 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-14 03:34:59 +02:00
|
|
|
// Otherwise, we need to emit the variable number of
|
|
|
|
// operands that allow the runtime to close over the
|
2022-08-28 05:07:20 +02:00
|
|
|
// upvalues and leave a blueprint in the constant index from
|
2022-10-14 03:34:59 +02:00
|
|
|
// which the result can be constructed.
|
2022-09-03 13:52:24 +02:00
|
|
|
let blueprint_idx = self.chunk().push_constant(Value::Blueprint(lambda));
|
2022-08-26 23:21:08 +02:00
|
|
|
|
2022-10-16 01:10:10 +02:00
|
|
|
let code_idx = self.push_op(
|
2022-10-18 09:48:19 +02:00
|
|
|
if is_suspended_thunk {
|
2022-10-16 01:10:10 +02:00
|
|
|
OpCode::OpThunkSuspended(blueprint_idx)
|
2022-10-14 03:34:59 +02:00
|
|
|
} else {
|
2022-10-16 01:10:10 +02:00
|
|
|
OpCode::OpThunkClosure(blueprint_idx)
|
2022-10-14 03:34:59 +02:00
|
|
|
},
|
|
|
|
node,
|
|
|
|
);
|
|
|
|
|
2022-09-06 22:13:48 +02:00
|
|
|
self.emit_upvalue_data(
|
|
|
|
outer_slot,
|
2022-10-03 16:08:39 +02:00
|
|
|
node,
|
2022-09-06 22:13:48 +02:00
|
|
|
compiled.scope.upvalues,
|
|
|
|
compiled.captures_with_stack,
|
|
|
|
);
|
2022-10-16 01:10:10 +02:00
|
|
|
|
2022-10-18 09:48:19 +02:00
|
|
|
if !is_suspended_thunk && !self.scope()[outer_slot].needs_finaliser {
|
2022-10-16 01:10:10 +02:00
|
|
|
if !self.scope()[outer_slot].must_thunk {
|
|
|
|
// The closure has upvalues, but is not recursive. Therefore no thunk is required,
|
|
|
|
// which saves us the overhead of Rc<RefCell<>>
|
|
|
|
self.chunk()[code_idx] = OpCode::OpClosure(blueprint_idx);
|
|
|
|
} else {
|
|
|
|
// This case occurs when a closure has upvalue-references to itself but does not need a
|
|
|
|
// finaliser. Since no OpFinalise will be emitted later on we synthesize one here.
|
|
|
|
// It is needed here only to set [`Closure::is_finalised`] which is used for sanity checks.
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
self.push_op(
|
|
|
|
OpCode::OpFinalise(self.scope().stack_index(outer_slot)),
|
|
|
|
&self.span_for(node),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2022-08-28 22:44:56 +02:00
|
|
|
}
|
|
|
|
|
2022-10-03 16:08:39 +02:00
|
|
|
fn compile_apply(&mut self, slot: LocalIdx, node: &ast::Apply) {
|
2022-08-28 22:44:56 +02:00
|
|
|
// To call a function, we leave its arguments on the stack,
|
|
|
|
// followed by the function expression itself, and then emit a
|
|
|
|
// call instruction. This way, the stack is perfectly laid out
|
|
|
|
// to enter the function call straight away.
|
2022-10-03 16:08:39 +02:00
|
|
|
self.compile(slot, &node.argument().unwrap());
|
|
|
|
self.compile(slot, &node.lambda().unwrap());
|
2022-09-03 04:02:20 +02:00
|
|
|
self.emit_force(&node.lambda().unwrap());
|
2022-10-03 16:08:39 +02:00
|
|
|
self.push_op(OpCode::OpCall, node);
|
2022-08-28 22:44:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Emit the data instructions that the runtime needs to correctly
|
2022-09-06 22:13:48 +02:00
|
|
|
/// assemble the upvalues struct.
|
2022-09-16 20:51:03 +02:00
|
|
|
fn emit_upvalue_data<T: ToSpan>(
|
2022-09-06 22:13:48 +02:00
|
|
|
&mut self,
|
|
|
|
slot: LocalIdx,
|
|
|
|
node: &T,
|
|
|
|
upvalues: Vec<Upvalue>,
|
|
|
|
capture_with: bool,
|
|
|
|
) {
|
2022-08-28 22:44:56 +02:00
|
|
|
for upvalue in upvalues {
|
2022-09-01 17:02:06 +02:00
|
|
|
match upvalue.kind {
|
|
|
|
UpvalueKind::Local(idx) => {
|
2022-10-22 18:29:02 +02:00
|
|
|
let target = &self.scope()[idx];
|
2022-08-28 18:38:17 +02:00
|
|
|
let stack_idx = self.scope().stack_index(idx);
|
|
|
|
|
2022-10-22 18:29:02 +02:00
|
|
|
// If the target is not yet initialised, we need to defer
|
|
|
|
// the local access
|
|
|
|
if !target.initialised {
|
2022-09-22 23:31:40 +02:00
|
|
|
self.push_op(OpCode::DataDeferredLocal(stack_idx), &upvalue.span);
|
2022-09-02 00:29:39 +02:00
|
|
|
self.scope_mut().mark_needs_finaliser(slot);
|
2022-08-28 15:50:46 +02:00
|
|
|
} else {
|
2022-10-16 01:10:10 +02:00
|
|
|
// a self-reference
|
2022-10-22 16:55:10 +02:00
|
|
|
if slot == idx {
|
2022-10-16 01:10:10 +02:00
|
|
|
self.scope_mut().mark_must_thunk(slot);
|
|
|
|
}
|
2022-09-22 23:31:40 +02:00
|
|
|
self.push_op(OpCode::DataLocalIdx(stack_idx), &upvalue.span);
|
2022-08-28 15:50:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-01 17:02:06 +02:00
|
|
|
UpvalueKind::Upvalue(idx) => {
|
2022-09-22 23:31:40 +02:00
|
|
|
self.push_op(OpCode::DataUpvalueIdx(idx), &upvalue.span);
|
fix(tvix/eval): correctly thread through dynamic upvalues
This puts together the puzzle pieces for threading dynamic
upvalues (that is, upvalues resolved from the `with`-stack) all the
way through.
Reading the test case enclosed in this commit and walking through it
is recommended to understand what problem is being tackled here.
In short, because the compiler can not statically know *which*
with-scope a dynamic argument is resolved from it needs to lay the
groundwork for resolving from *all* possible scopes.
There are multiple different approaches to doing this. The approach
chosen in this commit is that if a dynamic upvalue is detected, the
compiler will emit instructions to close over this dynamic value
in *all* enclosing lambda contexts.
It uses a new instruction for this that will leave around a sentinel
value in case an identifier could not be resolved, and wire the
location of this found value (or sentinel) up through the upvalues to
the next level of nesting.
In this tradeoff, tvix potentially closes over more upvalues than are
needed (but in practice, how often do people create *really* deep
`with`-stacks? and in *this* kind of code situation? maybe we should
even warn for this!) but avoids keeping the entire attribute sets
themselves around.
Looking at the test case, each surrounding closure will close
over *all* dynamic identifiers that are referenced later on visible to
it, but only the last one for each identifier will actually end up
being used.
This also covers our bases for an additional edge-case this creates,
in which an identifier potentially resolves to a dynamic upvalue *and*
to a dynamic value within the function's own scope (again, would
anyone really do this?) by introducing a resolution instruction for
that particular case.
There is likely some potential for cleaning up this code which is
quite ugly in some parts, but as this implementation is now carefully
calibrated to work I decided it is time to commit it and clean it up
in subsequent commits.
Change-Id: Ib701e3e6da39bd2c95938d1384036ff4f9fb3749
Reviewed-on: https://cl.tvl.fyi/c/depot/+/6322
Tested-by: BuildkiteCI
Reviewed-by: sterni <sternenseemann@systemli.org>
2022-08-28 02:45:45 +02:00
|
|
|
}
|
2022-08-26 23:21:08 +02:00
|
|
|
};
|
|
|
|
}
|
2022-09-06 22:13:48 +02:00
|
|
|
|
|
|
|
if capture_with {
|
|
|
|
// TODO(tazjin): probably better to emit span for the ident that caused this
|
|
|
|
self.push_op(OpCode::DataCaptureWith, node);
|
|
|
|
}
|
2022-08-24 00:54:34 +02:00
|
|
|
}
|
|
|
|
|
2022-08-16 22:43:45 +02:00
|
|
|
/// Emit the literal string value of an identifier. Required for
|
|
|
|
/// several operations related to attribute sets, where
|
|
|
|
/// identifiers are used as string keys.
|
|
|
|
fn emit_literal_ident(&mut self, ident: &ast::Ident) {
|
2022-10-13 05:27:09 +02:00
|
|
|
self.emit_constant(Value::String(ident.clone().into()), ident);
|
2022-08-14 19:12:20 +02:00
|
|
|
}
|
|
|
|
|
2022-08-26 17:38:20 +02:00
|
|
|
/// Patch the jump instruction at the given index, setting its
|
|
|
|
/// jump offset from the placeholder to the current code position.
|
|
|
|
///
|
|
|
|
/// This is required because the actual target offset of jumps is
|
|
|
|
/// not known at the time when the jump operation itself is
|
|
|
|
/// emitted.
|
2022-08-11 12:12:07 +02:00
|
|
|
fn patch_jump(&mut self, idx: CodeIdx) {
|
2022-08-26 19:46:43 +02:00
|
|
|
let offset = JumpOffset(self.chunk().code.len() - 1 - idx.0);
|
2022-08-11 12:12:07 +02:00
|
|
|
|
2022-08-23 20:00:53 +02:00
|
|
|
match &mut self.chunk().code[idx.0] {
|
2022-08-11 21:03:10 +02:00
|
|
|
OpCode::OpJump(n)
|
|
|
|
| OpCode::OpJumpIfFalse(n)
|
|
|
|
| OpCode::OpJumpIfTrue(n)
|
|
|
|
| OpCode::OpJumpIfNotFound(n) => {
|
2022-08-11 12:12:07 +02:00
|
|
|
*n = offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
op => panic!("attempted to patch unsupported op: {:?}", op),
|
|
|
|
}
|
|
|
|
}
|
2022-08-13 16:34:20 +02:00
|
|
|
|
2022-09-02 00:19:53 +02:00
|
|
|
/// Decrease scope depth of the current function and emit
|
|
|
|
/// instructions to clean up the stack at runtime.
|
2022-09-16 20:51:03 +02:00
|
|
|
fn cleanup_scope<N: ToSpan>(&mut self, node: &N) {
|
2022-08-13 16:34:20 +02:00
|
|
|
// When ending a scope, all corresponding locals need to be
|
|
|
|
// removed, but the value of the body needs to remain on the
|
|
|
|
// stack. This is implemented by a separate instruction.
|
2022-09-06 16:05:08 +02:00
|
|
|
let (popcount, unused_spans) = self.scope_mut().end_scope();
|
2022-09-03 02:12:37 +02:00
|
|
|
|
2022-09-16 21:01:06 +02:00
|
|
|
for span in &unused_spans {
|
2022-09-06 16:05:08 +02:00
|
|
|
self.emit_warning(span, WarningKind::UnusedBinding);
|
2022-08-13 16:34:20 +02:00
|
|
|
}
|
|
|
|
|
2022-09-06 16:05:08 +02:00
|
|
|
if popcount > 0 {
|
|
|
|
self.push_op(OpCode::OpCloseScope(Count(popcount)), node);
|
2022-08-13 16:34:20 +02:00
|
|
|
}
|
|
|
|
}
|
2022-08-13 19:17:25 +02:00
|
|
|
|
2022-09-02 00:19:53 +02:00
|
|
|
/// Open a new lambda context within which to compile a function,
|
|
|
|
/// closure or thunk.
|
|
|
|
fn new_context(&mut self) {
|
|
|
|
// This must inherit the scope-poisoning status of the parent
|
|
|
|
// in order for upvalue resolution to work correctly with
|
|
|
|
// poisoned identifiers.
|
|
|
|
self.contexts.push(self.context().inherit());
|
|
|
|
}
|
|
|
|
|
2022-08-16 14:16:56 +02:00
|
|
|
/// Declare a local variable known in the scope that is being
|
|
|
|
/// compiled by pushing it to the locals. This is used to
|
|
|
|
/// determine the stack offset of variables.
|
2022-09-16 20:51:03 +02:00
|
|
|
fn declare_local<S: Into<String>, N: ToSpan>(&mut self, node: &N, name: S) -> LocalIdx {
|
2022-08-16 14:11:06 +02:00
|
|
|
let name = name.into();
|
2022-09-13 15:04:52 +02:00
|
|
|
let depth = self.scope().scope_depth();
|
2022-08-16 14:11:06 +02:00
|
|
|
|
2022-08-24 14:37:09 +02:00
|
|
|
// Do this little dance to get ahold of the *static* key and
|
|
|
|
// use it for poisoning if required.
|
|
|
|
let key: Option<&'static str> = match self.globals.get_key_value(name.as_str()) {
|
|
|
|
Some((key, _)) => Some(*key),
|
|
|
|
None => None,
|
2022-08-16 14:11:06 +02:00
|
|
|
};
|
2022-08-16 14:16:56 +02:00
|
|
|
|
2022-08-24 14:37:09 +02:00
|
|
|
if let Some(global_ident) = key {
|
2022-09-16 21:01:06 +02:00
|
|
|
self.emit_warning(node, WarningKind::ShadowedGlobal(global_ident));
|
2022-08-24 14:37:09 +02:00
|
|
|
self.scope_mut().poison(global_ident, depth);
|
|
|
|
}
|
|
|
|
|
2022-08-28 14:34:35 +02:00
|
|
|
for other in self.scope().locals.iter().rev() {
|
2022-09-02 23:26:32 +02:00
|
|
|
if other.has_name(&name) && other.depth == depth {
|
2022-09-16 21:01:06 +02:00
|
|
|
self.emit_error(node, ErrorKind::VariableAlreadyDefined(other.span));
|
2022-09-12 15:12:43 +02:00
|
|
|
|
2022-08-27 16:16:46 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-01 18:57:55 +02:00
|
|
|
let span = self.span_for(node);
|
|
|
|
self.scope_mut().declare_local(name, span)
|
2022-08-28 16:50:16 +02:00
|
|
|
}
|
|
|
|
|
2022-09-06 22:13:48 +02:00
|
|
|
/// Determine whether the current lambda context has any ancestors
|
|
|
|
/// that use dynamic scope resolution, and mark contexts as
|
|
|
|
/// needing to capture their enclosing `with`-stack in their
|
|
|
|
/// upvalues.
|
|
|
|
fn has_dynamic_ancestor(&mut self) -> bool {
|
|
|
|
let mut ancestor_has_with = false;
|
|
|
|
|
|
|
|
for ctx in self.contexts.iter_mut() {
|
|
|
|
if ancestor_has_with {
|
|
|
|
// If the ancestor has an active with stack, mark this
|
|
|
|
// lambda context as needing to capture it.
|
|
|
|
ctx.captures_with_stack = true;
|
|
|
|
} else {
|
|
|
|
// otherwise, check this context and move on
|
|
|
|
ancestor_has_with = ctx.scope.has_with();
|
2022-08-28 00:52:55 +02:00
|
|
|
}
|
2022-08-27 22:58:46 +02:00
|
|
|
}
|
|
|
|
|
2022-09-06 22:13:48 +02:00
|
|
|
ancestor_has_with
|
2022-08-26 20:48:51 +02:00
|
|
|
}
|
|
|
|
|
2022-09-16 20:51:03 +02:00
|
|
|
fn emit_force<N: ToSpan>(&mut self, node: &N) {
|
2022-09-01 18:13:49 +02:00
|
|
|
self.push_op(OpCode::OpForce, node);
|
2022-08-29 20:57:28 +02:00
|
|
|
}
|
|
|
|
|
2022-09-16 21:01:06 +02:00
|
|
|
fn emit_warning<N: ToSpan>(&mut self, node: &N, kind: WarningKind) {
|
|
|
|
let span = self.span_for(node);
|
2022-09-01 18:57:55 +02:00
|
|
|
self.warnings.push(EvalWarning { kind, span })
|
2022-08-22 22:01:40 +02:00
|
|
|
}
|
2022-08-22 22:48:47 +02:00
|
|
|
|
2022-09-16 21:01:06 +02:00
|
|
|
fn emit_error<N: ToSpan>(&mut self, node: &N, kind: ErrorKind) {
|
|
|
|
let span = self.span_for(node);
|
2022-09-01 22:50:27 +02:00
|
|
|
self.errors.push(Error { kind, span })
|
2022-08-22 22:48:47 +02:00
|
|
|
}
|
2022-08-13 18:42:50 +02:00
|
|
|
}
|
|
|
|
|
2022-09-04 22:18:35 +02:00
|
|
|
/// Perform tail-call optimisation if the last call within a
|
|
|
|
/// compiled chunk is another call.
|
|
|
|
fn optimise_tail_call(chunk: &mut Chunk) {
|
|
|
|
let last_op = chunk
|
|
|
|
.code
|
|
|
|
.last_mut()
|
|
|
|
.expect("compiler bug: chunk should never be empty");
|
|
|
|
|
|
|
|
if matches!(last_op, OpCode::OpCall) {
|
|
|
|
*last_op = OpCode::OpTailCall;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-24 14:37:09 +02:00
|
|
|
/// Prepare the full set of globals from additional globals supplied
|
|
|
|
/// by the caller of the compiler, as well as the built-in globals
|
2022-10-26 14:16:04 +02:00
|
|
|
/// that are always part of the language. This also "ties the knot"
|
|
|
|
/// required in order for import to have a reference cycle back to
|
|
|
|
/// the globals.
|
2022-08-24 14:37:09 +02:00
|
|
|
///
|
|
|
|
/// Note that all builtin functions are *not* considered part of the
|
|
|
|
/// language in this sense and MUST be supplied as additional global
|
|
|
|
/// values, including the `builtins` set itself.
|
2022-10-26 14:16:04 +02:00
|
|
|
pub fn prepare_globals(additional: GlobalsMapFunc) -> Rc<GlobalsMap> {
|
|
|
|
Rc::new_cyclic(Box::new(|weak: &Weak<GlobalsMap>| {
|
|
|
|
let mut globals = additional(weak);
|
|
|
|
|
2022-08-24 14:37:09 +02:00
|
|
|
globals.insert(
|
2022-10-26 14:16:04 +02:00
|
|
|
"true",
|
|
|
|
Rc::new(|compiler, span| {
|
|
|
|
compiler.push_op(OpCode::OpTrue, &span);
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
globals.insert(
|
|
|
|
"false",
|
|
|
|
Rc::new(|compiler, span| {
|
|
|
|
compiler.push_op(OpCode::OpFalse, &span);
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
globals.insert(
|
|
|
|
"null",
|
|
|
|
Rc::new(|compiler, span| {
|
|
|
|
compiler.push_op(OpCode::OpNull, &span);
|
|
|
|
}),
|
2022-08-24 14:37:09 +02:00
|
|
|
);
|
|
|
|
|
2022-10-26 14:16:04 +02:00
|
|
|
globals
|
|
|
|
}))
|
2022-08-24 14:37:09 +02:00
|
|
|
}
|
|
|
|
|
2022-09-03 03:20:00 +02:00
|
|
|
pub fn compile(
|
2022-10-03 16:08:39 +02:00
|
|
|
expr: &ast::Expr,
|
2022-08-24 15:03:17 +02:00
|
|
|
location: Option<PathBuf>,
|
2022-09-18 01:52:23 +02:00
|
|
|
file: Arc<codemap::File>,
|
2022-10-26 14:16:04 +02:00
|
|
|
globals: Rc<GlobalsMap>,
|
2022-10-03 15:08:59 +02:00
|
|
|
observer: &mut dyn CompilerObserver,
|
2022-08-26 19:17:40 +02:00
|
|
|
) -> EvalResult<CompilationOutput> {
|
2022-10-26 14:16:04 +02:00
|
|
|
let mut c = Compiler::new(location, file, globals.clone(), observer)?;
|
2022-08-07 22:41:42 +02:00
|
|
|
|
2022-10-03 16:08:39 +02:00
|
|
|
let root_span = c.span_for(expr);
|
2022-09-06 16:12:04 +02:00
|
|
|
let root_slot = c.scope_mut().declare_phantom(root_span, false);
|
2022-10-03 16:08:39 +02:00
|
|
|
c.compile(root_slot, &expr);
|
2022-08-07 22:41:42 +02:00
|
|
|
|
2022-08-29 17:40:52 +02:00
|
|
|
// The final operation of any top-level Nix program must always be
|
|
|
|
// `OpForce`. A thunk should not be returned to the user in an
|
|
|
|
// unevaluated state (though in practice, a value *containing* a
|
|
|
|
// thunk might be returned).
|
2022-10-03 16:08:39 +02:00
|
|
|
c.emit_force(expr);
|
2022-08-29 17:40:52 +02:00
|
|
|
|
2022-09-04 15:56:20 +02:00
|
|
|
let lambda = Rc::new(c.contexts.pop().unwrap().lambda);
|
|
|
|
c.observer.observe_compiled_toplevel(&lambda);
|
|
|
|
|
2022-08-26 19:17:40 +02:00
|
|
|
Ok(CompilationOutput {
|
2022-09-04 15:56:20 +02:00
|
|
|
lambda,
|
2022-08-12 16:07:32 +02:00
|
|
|
warnings: c.warnings,
|
2022-08-22 22:48:47 +02:00
|
|
|
errors: c.errors,
|
2022-10-26 14:16:04 +02:00
|
|
|
globals: globals,
|
2022-08-12 16:07:32 +02:00
|
|
|
})
|
2022-08-07 22:41:42 +02:00
|
|
|
}
|