Skip to content

Truly independent web browser

17.6 KB
Raw Blame
/*
 * Copyright (c) 2026-present, the Ladybird developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

//! Writes the RuntimeFunctions trait, with one method per function the generated interpreter calls, and an
//! extern "C" entry point for each with exactly the signature the interpreter's call sites assume. The entry points
//! translate every calling convention into one Rust signature per kind of call. The methods have no default bodies, so
//! the runtime does not build unless it defines every function that the interpreter, in either of its variants, calls.
//!
//! JIT code calls the same functions, with the System V calling convention on every x86-64 target. Only on Windows is
//! that not the one the interpreter uses, so there every entry point has a forwarder for JIT code to call.

use std::collections::HashMap;
use std::fmt::Write;

use flapc::metadata::{InstructionDefinition, ParameterMode, SlowPathAbi, SlowPathLayout};
use flapc::runtime_interface::{RuntimeFunction, RuntimeFunctionKind};

use crate::ops::{field_name, rust_identifier};

fn method_name(symbol: &str) -> String {
    rust_identifier(
        symbol
            .strip_prefix("asm_slow_path_")
            .or_else(|| symbol.strip_prefix("asm_"))
            .unwrap_or_else(|| panic!("unexpected runtime function symbol {symbol}")),
    )
}

struct Record<'a> {
    op: &'a str,
    layout: &'a SlowPathLayout,
    /// The array name and the name of the instruction field that counts its elements.
    array: Option<(String, String)>,
}

impl<'a> Record<'a> {
    fn new(op: &'a str, layout: &'a SlowPathLayout, ops: &HashMap<&str, &InstructionDefinition>) -> Self {
        let array = layout.array.as_ref().map(|array| {
            let definition = ops[op];
            let array_layout = definition.array.as_ref().expect("an operand array has a layout");
            (
                field_name(&array.name),
                field_name(&definition.fields[array_layout.count_field_index].name),
            )
        });
        Self { op, layout, array }
    }

    fn method_parameters(&self) -> String {
        let mut parameters = format!("_instruction: &op::{0}, _values: &mut op::{0}Values", self.op);
        if let Some((array, _)) = &self.array {
            let _ = write!(parameters, ", _{array}: &mut [Value]");
        }
        parameters
    }

    /// Statements that turn the raw record pointer `values` into the method's arguments.
    fn bind_record(&self, out: &mut String) {
        if let Some((array, count)) = &self.array {
            let _ = writeln!(
                out,
                "    // SAFETY: The interpreter stores the instruction's {count} operands after the record."
            );
            let _ = writeln!(
                out,
                "    let {array} = unsafe {{ ak::slice_from_raw_parts_mut((&raw mut (*values).{array}).cast::<Value>(), instruction.{count} as usize) }};"
            );
        }
        let _ = writeln!(
            out,
            "    // SAFETY: The interpreter passes a record for the instruction it is executing."
        );
        let _ = writeln!(out, "    let values = unsafe {{ &mut *values }};");
    }

    fn call_arguments(&self) -> String {
        match &self.array {
            Some((array, _)) => format!("instruction, values, {array}"),
            None => "instruction, values".to_string(),
        }
    }
}

/// Where the interpreter's calling convention is not the one JIT code calls with.
const WINDOWS_X86_64: &str = "all(target_arch = \"x86_64\", windows)";

/// The Rust signature of the entry point of a runtime function.
struct EntryPointSignature {
    is_unsafe: bool,
    /// The name and the type of each parameter.
    parameters: Vec<(String, String)>,
    result: Option<&'static str>,
}

/// Parameters of these names and types.
fn parameters(parameters: &[(&str, &str)]) -> Vec<(String, String)> {
    parameters
        .iter()
        .map(|(name, ty)| (name.to_string(), ty.to_string()))
        .collect()
}

impl EntryPointSignature {
    fn of(kind: &RuntimeFunctionKind) -> Self {
        let instruction_parameters = |op: &str| {
            parameters(&[
                ("vm", "*const Vm"),
                ("pc", "u32"),
                ("instruction", format!("*const op::{op}").as_str()),
            ])
        };
        let (is_unsafe, parameters, result) = match kind {
            RuntimeFunctionKind::SlowPath { op, abi, layout } => {
                let inputs = layout
                    .fields
                    .iter()
                    .filter(|field| field.mode != ParameterMode::Out)
                    .map(|field| (field_name(&field.name), "Value".to_string()));
                let mut parameters = instruction_parameters(op);
                match abi {
                    SlowPathAbi::Scalar => {
                        parameters.extend(inputs);
                        (true, parameters, Some("AsmSlowPathResult"))
                    }
                    SlowPathAbi::Mixed => {
                        parameters.push(("outputs".to_string(), format!("*mut op::{op}Values")));
                        parameters.extend(inputs);
                        (true, parameters, Some("i64"))
                    }
                    SlowPathAbi::Record => {
                        parameters.push(("values".to_string(), format!("*mut op::{op}Values")));
                        (true, parameters, Some("i64"))
                    }
                }
            }
            RuntimeFunctionKind::Try { op, .. } => {
                let mut parameters = instruction_parameters(op);
                parameters.push(("values".to_string(), format!("*mut op::{op}Values")));
                (true, parameters, Some("i64"))
            }
            RuntimeFunctionKind::BinarySlowPath => (
                true,
                parameters(&[
                    ("vm", "*const Vm"),
                    ("pc", "u32"),
                    ("dst", "*mut Value"),
                    ("lhs", "Value"),
                    ("rhs", "Value"),
                    ("length", "u32"),
                ]),
                Some("i64"),
            ),
            RuntimeFunctionKind::JumpSlowPath => (
                true,
                parameters(&[
                    ("vm", "*const Vm"),
                    ("pc", "u32"),
                    ("lhs", "Value"),
                    ("rhs", "Value"),
                    ("true_target", "u32"),
                    ("false_target", "u32"),
                ]),
                Some("i64"),
            ),
            RuntimeFunctionKind::Helper => (false, parameters(&[("argument", "u64")]), Some("u64")),
            RuntimeFunctionKind::HelperWithTwoArguments => {
                (false, parameters(&[("first", "u64"), ("second", "u64")]), Some("u64"))
            }
            RuntimeFunctionKind::FallbackHandler => (
                true,
                parameters(&[("vm", "*const Vm"), ("pc", "u32"), ("instruction", "*const u8")]),
                Some("i64"),
            ),
            RuntimeFunctionKind::BreakpointCheck => (true, parameters(&[("vm", "*const Vm"), ("pc", "u32")]), None),
            RuntimeFunctionKind::StackOverflowSlowPath => {
                (true, parameters(&[("vm", "*const Vm"), ("pc", "u32")]), Some("i64"))
            }
        };
        Self {
            is_unsafe,
            parameters,
            result,
        }
    }

    /// The function up to and including its opening brace.
    fn write_header(&self, out: &mut String, abi: &str, name: &str) {
        let safety = if self.is_unsafe { "unsafe " } else { "" };
        let result = self.result.map_or(String::new(), |result| format!(" -> {result}"));
        let parameters = self
            .parameters
            .iter()
            .map(|(name, ty)| format!("{name}: {ty}"))
            .collect::<Vec<_>>()
            .join(", ");
        let _ = writeln!(out, "pub {safety}extern \"{abi}\" fn {name}({parameters}){result} {{");
    }

    fn write_system_v_forwarder(&self, out: &mut String, symbol: &str) {
        let arguments = self
            .parameters
            .iter()
            .map(|(name, _)| name.as_str())
            .collect::<Vec<_>>()
            .join(", ");
        let _ = writeln!(out, "#[cfg({WINDOWS_X86_64})]");
        self.write_header(out, "sysv64", &format!("{symbol}_sysv64"));
        if self.is_unsafe {
            out.push_str("    // SAFETY: JIT code calls this like the interpreter calls the entry point.\n");
            let _ = writeln!(out, "    unsafe {{ {symbol}({arguments}) }}\n}}");
        } else {
            let _ = writeln!(out, "    {symbol}({arguments})\n}}");
        }
    }
}

pub fn generate(functions: &[RuntimeFunction], ops: &[InstructionDefinition]) -> String {
    let ops: HashMap<&str, &InstructionDefinition> = ops.iter().map(|op| (op.name.as_str(), op)).collect();
    let mut methods = String::new();
    let mut entry_points = String::new();

    for function in functions {
        let symbol = &function.symbol;
        let name = method_name(symbol);
        let _ = writeln!(methods, "    /// Called as `{symbol}`.");
        let signature = EntryPointSignature::of(&function.kind);
        let _ = writeln!(entry_points, "#[unsafe(no_mangle)]");
        signature.write_header(&mut entry_points, "C", symbol);
        match &function.kind {
            RuntimeFunctionKind::SlowPath { op, abi, layout } => {
                let record = Record::new(op, layout, &ops);
                let _ = writeln!(
                    methods,
                    "    fn {name}(_vm: &Vm, pc: u32, {}) -> SlowPathControl;",
                    record.method_parameters()
                );
                generate_slow_path_entry_point(&mut entry_points, &name, *abi, &record);
            }
            RuntimeFunctionKind::Try { op, layout } => {
                let record = Record::new(op, layout, &ops);
                let _ = writeln!(
                    methods,
                    "    /// Returns whether it handled the instruction; if not, the interpreter takes the instruction's slow path.\n    fn {name}(_vm: &Vm, _pc: u32, {}) -> bool;",
                    record.method_parameters()
                );
                entry_points
                    .push_str("    // SAFETY: The interpreter passes its VM and the instruction it is executing.\n");
                entry_points.push_str("    let (vm, instruction) = unsafe { (&*vm, &*instruction) };\n");
                record.bind_record(&mut entry_points);
                let _ = writeln!(
                    entry_points,
                    "    i64::from(!<Runtime as RuntimeFunctions>::{name}(vm, pc, {}))\n}}",
                    record.call_arguments()
                );
            }
            RuntimeFunctionKind::BinarySlowPath => {
                let _ = writeln!(
                    methods,
                    "    fn {name}(_vm: &Vm, pc: u32, _dst: &Cell<Value>, _lhs: Value, _rhs: Value, _length: u32) -> SlowPathControl;"
                );
                let _ = writeln!(
                    entry_points,
                    "    // SAFETY: The interpreter passes its VM and a live value slot.\n    let (vm, dst) = unsafe {{ (&*vm, &*dst.cast::<Cell<Value>>()) }};\n    <Runtime as RuntimeFunctions>::{name}(vm, pc, dst, lhs, rhs, length).0\n}}"
                );
            }
            RuntimeFunctionKind::JumpSlowPath => {
                let _ = writeln!(
                    methods,
                    "    fn {name}(_vm: &Vm, pc: u32, _lhs: Value, _rhs: Value, _true_target: u32, _false_target: u32) -> SlowPathControl;"
                );
                let _ = writeln!(
                    entry_points,
                    "    // SAFETY: The interpreter passes its VM.\n    let vm = unsafe {{ &*vm }};\n    <Runtime as RuntimeFunctions>::{name}(vm, pc, lhs, rhs, true_target, false_target).0\n}}"
                );
            }
            RuntimeFunctionKind::Helper => {
                let _ = writeln!(methods, "    fn {name}(_argument: u64) -> u64;");
                let _ = writeln!(entry_points, "    <Runtime as RuntimeFunctions>::{name}(argument)\n}}");
            }
            RuntimeFunctionKind::HelperWithTwoArguments => {
                let _ = writeln!(methods, "    fn {name}(_first: u64, _second: u64) -> u64;");
                let _ = writeln!(
                    entry_points,
                    "    <Runtime as RuntimeFunctions>::{name}(first, second)\n}}"
                );
            }
            RuntimeFunctionKind::FallbackHandler => {
                let _ = writeln!(
                    methods,
                    "    fn {name}(_vm: &Vm, pc: u32, _instruction: *const u8) -> SlowPathControl;"
                );
                let _ = writeln!(
                    entry_points,
                    "    // SAFETY: The interpreter passes its VM.\n    let vm = unsafe {{ &*vm }};\n    <Runtime as RuntimeFunctions>::{name}(vm, pc, instruction).0\n}}"
                );
            }
            RuntimeFunctionKind::BreakpointCheck => {
                let _ = writeln!(methods, "    fn {name}(_vm: &Vm, pc: u32);");
                let _ = writeln!(
                    entry_points,
                    "    // SAFETY: The interpreter passes its VM.\n    let vm = unsafe {{ &*vm }};\n    <Runtime as RuntimeFunctions>::{name}(vm, pc);\n}}"
                );
            }
            RuntimeFunctionKind::StackOverflowSlowPath => {
                let _ = writeln!(methods, "    fn {name}(_vm: &Vm, pc: u32) -> SlowPathControl;");
                let _ = writeln!(
                    entry_points,
                    "    // SAFETY: The interpreter passes its VM.\n    let vm = unsafe {{ &*vm }};\n    <Runtime as RuntimeFunctions>::{name}(vm, pc).0\n}}"
                );
            }
        }
        signature.write_system_v_forwarder(&mut entry_points, symbol);
        entry_points.push('\n');
    }

    let mut out = String::new();
    out.push_str("// Generated by the libjs_rust build script -- DO NOT EDIT\n\n");
    out.push_str("pub trait RuntimeFunctions {\n");
    out.push_str(&methods);
    out.push_str("}\n\n");
    out.push_str(&entry_points);
    let _ = writeln!(
        out,
        "pub const RUNTIME_FUNCTION_SYMBOLS: [&str; {}] = [",
        functions.len()
    );
    for function in functions {
        let _ = writeln!(out, "    \"{}\",", function.symbol);
    }
    out.push_str("];\n\n");
    for (condition, suffix) in [
        (format!("not({WINDOWS_X86_64})"), ""),
        (WINDOWS_X86_64.to_string(), "_sysv64"),
    ] {
        out.push_str(
            "/// The address JIT code calls every runtime function at, by symbol, in the order of RUNTIME_FUNCTION_SYMBOLS.\n",
        );
        let _ = writeln!(out, "#[cfg({condition})]");
        let _ = writeln!(
            out,
            "pub fn runtime_function_addresses() -> [(&'static str, usize); {}] {{\n    [",
            functions.len()
        );
        for function in functions {
            let _ = writeln!(
                out,
                "        (\"{0}\", {0}{suffix} as *const () as usize),",
                function.symbol
            );
        }
        out.push_str("    ]\n}\n");
    }
    out
}

fn generate_slow_path_entry_point(out: &mut String, name: &str, abi: SlowPathAbi, record: &Record) {
    let op = record.op;
    let fields = &record.layout.fields;
    let initialize_values = || {
        let initializers: Vec<String> = fields
            .iter()
            .map(|field| {
                let name = field_name(&field.name);
                if field.mode == ParameterMode::Out {
                    format!("{name}: Value::EMPTY")
                } else {
                    name
                }
            })
            .collect();
        format!(
            "    let mut values = op::{op}Values {{ {} }};\n",
            initializers.join(", ")
        )
    };
    let call = format!(
        "<Runtime as RuntimeFunctions>::{name}(vm, pc, {})",
        record.call_arguments()
    );
    let bind_vm = "    // SAFETY: The interpreter passes its VM and the instruction it is executing.\n    let (vm, instruction) = unsafe { (&*vm, &*instruction) };\n";

    match abi {
        SlowPathAbi::Scalar => {
            let primary_output = fields
                .iter()
                .find(|field| field.mode == ParameterMode::Out && !field.optional)
                .map(|field| field_name(&field.name));
            out.push_str(bind_vm);
            out.push_str(&initialize_values());
            let _ = writeln!(out, "    let values = &mut values;\n    let control = {call};");
            match primary_output {
                Some(output) => {
                    let _ = writeln!(
                        out,
                        "    AsmSlowPathResult {{ control: control.0, value: values.{output}.0 }}\n}}"
                    );
                }
                None => {
                    out.push_str("    AsmSlowPathResult { control: control.0, value: 0 }\n}\n");
                }
            }
        }
        SlowPathAbi::Mixed => {
            out.push_str(bind_vm);
            out.push_str(&initialize_values());
            let _ = writeln!(out, "    let values = &mut values;\n    let control = {call};");
            out.push_str("    // SAFETY: The interpreter passes a record to receive the outputs.\n    let outputs = unsafe { &mut *outputs };\n");
            for field in fields.iter().filter(|field| field.mode != ParameterMode::In) {
                let name = field_name(&field.name);
                let _ = writeln!(out, "    outputs.{name} = values.{name};");
            }
            out.push_str("    control.0\n}\n");
        }
        SlowPathAbi::Record => {
            out.push_str(bind_vm);
            record.bind_record(out);
            let _ = writeln!(out, "    {call}.0\n}}");
        }
    }
}