Skip to main content

offlineasm_backend/backends/
backend_rloop.rs

1//! Lowers offlineasm to Rust for the portable `rloop` interpreter.
2
3use crate::ast::Node;
4use crate::backends::{LabelFlags, LowerCtx};
5use offlineasm_ir::regs::{self, VReg};
6use std::collections::HashMap;
7
8pub fn normalize_op(opcode: &str) -> &str {
9    opcode
10}
11
12fn rust_ident(name: &str) -> String {
13    let raw = name.strip_prefix('_').unwrap_or(name);
14    raw.replace(|c: char| !(c.is_ascii_alphanumeric() || c == '_'), "_")
15}
16
17const RLOOP_GPRS: &[(VReg, &str)] = &[
18    (regs::t0, "regs.t0"),
19    (regs::t1, "regs.t1"),
20    (regs::t2, "regs.t2"),
21    (regs::t3, "regs.t3"),
22    (regs::t4, "regs.pc"),
23    (regs::t5, "regs.t5"),
24    (regs::t6, "regs.t6"),
25    (regs::t7, "regs.t7"),
26    (regs::csr0, "regs.t9"),
27    (regs::csr1, "regs.t8"),
28    (regs::csr2, "regs.t10"),
29    (regs::csr3, "regs.t11"),
30    (regs::cfr, "regs.cfr"),
31    (regs::lr, "regs.lr"),
32    (regs::sp, "regs.sp"),
33];
34
35const RLOOP_FPRS: &[(VReg, &str)] = &[
36    (regs::ft0, "regs.d0"),
37    (regs::ft1, "regs.d1"),
38    (regs::ft2, "regs.d2"),
39    (regs::ft3, "regs.d3"),
40    (regs::ft4, "regs.d4"),
41    (regs::ft5, "regs.d5"),
42];
43
44fn reg_canon(name: &str) -> Result<&'static str, String> {
45    let bad = || format!("Bad register {} for RLOOP", name);
46    let id = regs::gpr_id(name).ok_or_else(bad)?;
47    RLOOP_GPRS
48        .iter()
49        .find(|(r, _)| r.id() == id)
50        .map(|&(_, field)| field)
51        .ok_or_else(bad)
52}
53
54fn fpr_canon(name: &str) -> Result<&'static str, String> {
55    let bad = || format!("Bad FP register {} for RLOOP", name);
56    let id = regs::fpr_id(name).ok_or_else(bad)?;
57    RLOOP_FPRS
58        .iter()
59        .find(|(r, _)| r.id() == id)
60        .map(|&(_, field)| field)
61        .ok_or_else(bad)
62}
63
64/// Returns the lvalue identifier for a register-like node, or `None` when
65/// the node has no nameable location.
66fn as_reg_ident(node: &Node) -> Option<Result<String, String>> {
67    match node {
68        Node::Register { name, .. } => Some(reg_canon(name).map(|s| s.to_string())),
69        Node::FpRegister { name, .. } => Some(fpr_canon(name).map(|s| s.to_string())),
70        Node::SpecialRegister { name } => Some(Err(format!(
71            "rloop has no physical registers: {} cannot be used here",
72            name
73        ))),
74        Node::Variable { name, .. } => Some(Ok(name.clone())),
75        _ => None,
76    }
77}
78/// Value types the generated interpreter code distinguishes.
79#[derive(Clone, Copy, Debug, PartialEq, Eq)]
80pub enum RTy {
81    Intptr,
82    Uintptr,
83    Int32,
84    Uint32,
85    Int16,
86    Uint16,
87    Int64,
88    Uint64,
89    Int8,
90    Uint8,
91    VoidPtr,
92    NativeFunc,
93    Double,
94    BitsAsDouble,
95    BitsAsInt64,
96    Opcode,
97}
98use RTy::*;
99
100fn rust_prim(ty: RTy) -> &'static str {
101    match ty {
102        Intptr | Int64 | BitsAsInt64 => "i64",
103        Uintptr | Uint64 => "u64",
104        Int32 => "i32",
105        Uint32 => "u32",
106        Int16 => "i16",
107        Uint16 => "u16",
108        Int8 => "i8",
109        Uint8 => "u8",
110        VoidPtr | NativeFunc | Opcode => "u64",
111        Double | BitsAsDouble => "f64",
112    }
113}
114
115fn accessor_expr(reg: &str, ty: RTy) -> String {
116    match ty {
117        Intptr => format!("(({}) as i64)", reg),
118        Uintptr => format!("(({}) as u64)", reg),
119        Uint64 => reg.to_string(),
120        Int64 | BitsAsInt64 => format!("(({}) as i64)", reg),
121        Int32 => format!("(({}) as u32 as i32)", reg),
122        Uint32 => format!("(({}) as u32)", reg),
123        Int16 => format!("(({}) as u16 as i16)", reg),
124        Uint16 => format!("(({}) as u16)", reg),
125        Int8 => format!("(({}) as u8 as i8)", reg),
126        Uint8 => format!("(({}) as u8)", reg),
127        VoidPtr | NativeFunc | Opcode => reg.to_string(),
128        Double | BitsAsDouble => format!("f64::from_bits({})", reg),
129    }
130}
131
132fn store_expr(value_expr: &str, ty: RTy) -> String {
133    match ty {
134        Uint8 => format!("((({}) as u8) as u64)", value_expr),
135        Int8 => format!("((({}) as i8) as i64 as u64)", value_expr),
136        Uint16 => format!("((({}) as u16) as u64)", value_expr),
137        Int16 => format!("((({}) as i16) as i64 as u64)", value_expr),
138        Uint32 => format!("((({}) as u32) as u64)", value_expr),
139        Int32 => format!("((({}) as i32) as i64 as u64)", value_expr),
140        Uint64 | Uintptr => format!("(({}) as u64)", value_expr),
141        Int64 | Intptr | BitsAsInt64 => format!("((({}) as i64) as u64)", value_expr),
142        Double | BitsAsDouble => format!("(({}) as f64).to_bits()", value_expr),
143        VoidPtr | NativeFunc | Opcode => format!("(({}) as u64)", value_expr),
144    }
145}
146
147fn literal_cast(value: i128, ty: RTy) -> String {
148    format!("(({}i64) as {})", value, rust_prim(ty))
149}
150
151fn pointer_expr(node: &Node) -> Result<String, String> {
152    match node {
153        Node::Address { base, offset, .. } => {
154            let b = render_value(base, VoidPtr)?;
155            let off = match offset.as_ref() {
156                Node::Immediate { value, .. } => *value,
157                other => {
158                    return Err(format!(
159                        "Address offset must be an immediate in rloop, got {}",
160                        other.dump()
161                    ))
162                }
163            };
164            Ok(format!("(((({}) as i64) + ({})) as usize)", b, off))
165        }
166        Node::BaseIndex {
167            base,
168            index,
169            offset,
170            ..
171        } => {
172            let b = render_value(base, VoidPtr)?;
173            let idx = render_value(index, Intptr)?;
174            let shift = node.scale_shift()?;
175            let off = match offset.as_ref() {
176                Node::Immediate { value, .. } => *value,
177                other => {
178                    return Err(format!(
179                        "BaseIndex offset must be an immediate in rloop, got {}",
180                        other.dump()
181                    ))
182                }
183            };
184            Ok(format!(
185                "(((({}) as i64) + (({}) << {}) + ({})) as usize)",
186                b, idx, shift, off
187            ))
188        }
189        other => Err(format!("not an address in rloop: {}", other.dump())),
190    }
191}
192
193fn mem_read(node: &Node, ty: RTy) -> Result<String, String> {
194    let ptr = pointer_expr(node)?;
195    Ok(format!(
196        "unsafe {{ *(({}) as *const {}) }}",
197        ptr,
198        rust_prim(ty)
199    ))
200}
201
202/// Renders `node` as a Rust expression of type `ty`.
203///
204/// The expression splices into the generated `rloop` interpreter body.
205///
206/// # Errors
207///
208/// Returns an error if `node` is not an operand form the RLoop backend can
209/// render (an unresolved offset, a label the interpreter cannot address).
210pub fn render_value(node: &Node, ty: RTy) -> Result<String, String> {
211    match node {
212        Node::Immediate { value, .. } => Ok(literal_cast(*value, ty)),
213        Node::RustConst { expr, .. } => Ok(format!(
214            "(({}) as {})",
215            quote::quote!(#expr),
216            rust_prim(ty)
217        )),
218        Node::Register { name, .. } => Ok(accessor_expr(reg_canon(name)?, ty)),
219        Node::FpRegister { name, .. } => Ok(accessor_expr(fpr_canon(name)?, ty)),
220        Node::SpecialRegister { name } => Err(format!(
221            "rloop has no physical registers: {} cannot be used here",
222            name
223        )),
224        Node::Variable { name, .. } => Ok(accessor_expr(name, ty)),
225        Node::VecRegister { name, .. } => {
226            Err(format!("vector registers are not supported by rloop: {}", name))
227        }
228        Node::Address { .. } | Node::BaseIndex { .. } => mem_read(node, ty),
229        Node::AbsoluteAddress { .. } => {
230            Err("rloop: absolute-address operands have no value to interpret".to_string())
231        }
232        Node::LabelReference { .. } | Node::LocalLabelReference { .. } => {
233            Err("label references cannot be used as general rloop value operands (only as branch/jmp/lea targets)".to_string())
234        }
235        other => Err(format!("cannot render {} as an rloop value operand", other.dump())),
236    }
237}
238
239fn emit_store(
240    dst: &Node,
241    value_expr: String,
242    ty: RTy,
243    emit: &mut dyn FnMut(String),
244) -> Result<(), String> {
245    if let Some(ident) = as_reg_ident(dst) {
246        emit(format!("{} = {};", ident?, store_expr(&value_expr, ty)));
247        return Ok(());
248    }
249    match dst {
250        Node::Address { .. } | Node::BaseIndex { .. } => {
251            let ptr = pointer_expr(dst)?;
252            let t = rust_prim(ty);
253            emit(format!(
254                "unsafe {{ *(({}) as *mut {}) = ({}) as {}; }}",
255                ptr, t, value_expr, t
256            ));
257            Ok(())
258        }
259        other => Err(format!("cannot store to {} in rloop", other.dump())),
260    }
261}
262
263fn emit_store_bool(
264    dst: &Node,
265    cond_expr: String,
266    ty: RTy,
267    emit: &mut dyn FnMut(String),
268) -> Result<(), String> {
269    if let Some(ident) = as_reg_ident(dst) {
270        emit(format!(
271            "{} = if {} {{ 1u64 }} else {{ 0u64 }};",
272            ident?, cond_expr
273        ));
274        return Ok(());
275    }
276    emit_store(
277        dst,
278        format!("(if {} {{ 1 }} else {{ 0 }})", cond_expr),
279        ty,
280        emit,
281    )
282}
283
284fn branch_target(node: &Node, local_strings: &HashMap<String, String>) -> Result<String, String> {
285    match node {
286        Node::LabelReference { label, offset, .. } => {
287            if *offset != 0 {
288                return Err(format!(
289                    "label reference with nonzero offset cannot be a branch target: {}",
290                    label
291                ));
292            }
293            Ok(format!("Opcode::{}", rust_ident(label)))
294        }
295        Node::LocalLabelReference { label, .. } => {
296            let interned = local_strings
297                .get(label)
298                .cloned()
299                .unwrap_or_else(|| label.clone());
300            Ok(format!("Opcode::{}", rust_ident(&interned)))
301        }
302        other => Err(format!(
303            "expected a label reference as an rloop branch target, got {}",
304            other.dump()
305        )),
306    }
307}
308
309fn raw_label_ident(node: &Node) -> Result<String, String> {
310    match node {
311        Node::LabelReference {
312            label, offset: 0, ..
313        } => Ok(rust_ident(label)),
314        other => Err(format!(
315            "expected a plain label reference, got {}",
316            other.dump()
317        )),
318    }
319}
320
321fn canon_dump(node: &Node) -> Result<String, String> {
322    as_reg_ident(node)
323        .unwrap_or_else(|| Err(format!("expected a register operand, got {}", node.dump())))
324}
325
326fn opcode_to_u64(variant: &str) -> String {
327    format!("(Opcode::{} as u64)", variant)
328}
329
330fn u64_to_opcode(expr: &str) -> String {
331    format!("Opcode::from_u64({})", expr)
332}
333
334fn threaded_return(target: &str) -> String {
335    format!("return ({}, regs);", target)
336}
337
338fn emit_synced_call(emit: &mut dyn FnMut(String), stmt: String) {
339    emit("m.regs = regs;".to_string());
340    emit(stmt);
341    emit("regs = m.regs;".to_string());
342}
343
344fn wrapping_binop(a: &str, op: &str, b: &str, ty: RTy) -> String {
345    if matches!(ty, Double | BitsAsDouble) {
346        return format!("({} {} {})", a, op, b);
347    }
348    match op {
349        "+" => format!("({}).wrapping_add({})", a, b),
350        "-" => format!("({}).wrapping_sub({})", a, b),
351        "*" => format!("({}).wrapping_mul({})", a, b),
352        _ => format!("({} {} {})", a, op, b),
353    }
354}
355
356fn emit_operation(
357    operands: &[Node],
358    ty: RTy,
359    op: &str,
360    emit: &mut dyn FnMut(String),
361) -> Result<(), String> {
362    let (op1, op2, dst) = match operands.len() {
363        3 => (&operands[0], &operands[1], &operands[2]),
364        2 => (&operands[1], &operands[0], &operands[1]),
365        n => return Err(format!("expected 2 or 3 operands, got {}", n)),
366    };
367    let a = render_value(op1, ty)?;
368    let b = render_value(op2, ty)?;
369    let expr = wrapping_binop(&a, op, &b, ty);
370    emit_store(dst, expr, ty, emit)
371}
372
373fn emit_unary_operation(
374    operands: &[Node],
375    ty: RTy,
376    op: &str,
377    emit: &mut dyn FnMut(String),
378) -> Result<(), String> {
379    if operands.len() != 1 {
380        return Err(format!("expected 1 operand, got {}", operands.len()));
381    }
382    let v = render_value(&operands[0], ty)?;
383    let expr = match op {
384        "-" if matches!(ty, Double | BitsAsDouble) => format!("(-({}))", v),
385        "-" => format!("({}).wrapping_neg()", v),
386        "~" => format!("(!({}))", v),
387        _ => return Err(format!("unknown unary operator {}", op)),
388    };
389    emit_store(&operands[0], expr, ty, emit)
390}
391
392fn emit_shift_operation(
393    operands: &[Node],
394    ty: RTy,
395    op: &str,
396    emit: &mut dyn FnMut(String),
397) -> Result<(), String> {
398    if operands.len() != 2 && operands.len() != 3 {
399        return Err(format!("expected 2 or 3 operands, got {}", operands.len()));
400    }
401    let dst = if operands.len() == 3 {
402        &operands[2]
403    } else {
404        &operands[1]
405    };
406    let value = render_value(&operands[1], ty)?;
407    let amount = render_value(&operands[0], Intptr)?;
408    let mask = match ty {
409        Int32 | Uint32 => 0x1f,
410        _ => 0x3f,
411    };
412    let masked = format!("(({}) & {})", amount, mask);
413    let shifted = if op == "<<" {
414        format!("(({}) << ({}))", value, masked)
415    } else {
416        format!("(({}) >> ({}))", value, masked)
417    };
418    emit_store(dst, shifted, ty, emit)
419}
420
421fn emit_compare_and_branch(
422    operands: &[Node],
423    ty: RTy,
424    cmp: &str,
425    local_strings: &HashMap<String, String>,
426    emit: &mut dyn FnMut(String),
427) -> Result<(), String> {
428    if operands.len() != 3 {
429        return Err(format!("expected 3 operands, got {}", operands.len()));
430    }
431    let a = render_value(&operands[0], ty)?;
432    let b = render_value(&operands[1], ty)?;
433    let target = branch_target(&operands[2], local_strings)?;
434    emit(format!(
435        "if {} {} {} {{ {} }}",
436        a,
437        cmp,
438        b,
439        threaded_return(&target)
440    ));
441    Ok(())
442}
443
444fn emit_compare_double_nan_branch(
445    operands: &[Node],
446    cmp: &str,
447    local_strings: &HashMap<String, String>,
448    emit: &mut dyn FnMut(String),
449) -> Result<(), String> {
450    if operands.len() != 3 {
451        return Err(format!("expected 3 operands, got {}", operands.len()));
452    }
453    let a = render_value(&operands[0], Double)?;
454    let b = render_value(&operands[1], Double)?;
455    let target = branch_target(&operands[2], local_strings)?;
456    emit(format!(
457        "if ({}).is_nan() || ({}).is_nan() || ({} {} {}) {{ {} }}",
458        a,
459        b,
460        a,
461        cmp,
462        b,
463        threaded_return(&target)
464    ));
465    Ok(())
466}
467
468fn emit_compare_and_set(
469    operands: &[Node],
470    ty: RTy,
471    cmp: &str,
472    emit: &mut dyn FnMut(String),
473) -> Result<(), String> {
474    if operands.len() != 3 {
475        return Err(format!("expected 3 operands, got {}", operands.len()));
476    }
477    let a = render_value(&operands[0], ty)?;
478    let b = render_value(&operands[1], ty)?;
479    let cond = format!("({} {} {})", a, cmp, b);
480    emit_store_bool(&operands[2], cond, ty, emit)
481}
482
483fn condition_expr(operands: &[Node], ty: RTy, tail: &str) -> Result<String, String> {
484    let op1 = render_value(&operands[0], ty)?;
485    match operands.len() {
486        2 => Ok(format!("({} {})", op1, tail)),
487        3 => {
488            let mask = render_value(&operands[1], ty)?;
489            Ok(format!("(({} & {}) {})", op1, mask, tail))
490        }
491        n => Err(format!("expected 2 or 3 operands, got {}", n)),
492    }
493}
494
495fn emit_test_and_branch_if(
496    operands: &[Node],
497    ty: RTy,
498    tail: &str,
499    local_strings: &HashMap<String, String>,
500    emit: &mut dyn FnMut(String),
501) -> Result<(), String> {
502    if operands.len() < 2 {
503        return Err(format!(
504            "expected at least 2 operands, got {}",
505            operands.len()
506        ));
507    }
508    let target = branch_target(&operands[operands.len() - 1], local_strings)?;
509
510    let cond = condition_expr(operands, ty, tail)?;
511    emit(format!("if {} {{ {} }}", cond, threaded_return(&target)));
512    Ok(())
513}
514
515fn emit_test_set(
516    operands: &[Node],
517    ty: RTy,
518    tail: &str,
519    emit: &mut dyn FnMut(String),
520) -> Result<(), String> {
521    if operands.len() < 2 {
522        return Err(format!(
523            "expected at least 2 operands, got {}",
524            operands.len()
525        ));
526    }
527    let dst_idx = operands.len() - 1;
528
529    let cond = condition_expr(operands, ty, tail)?;
530    emit_store_bool(&operands[dst_idx], cond, ty, emit)
531}
532
533fn emit_op_and_branch(
534    operands: &[Node],
535    op: &str,
536    ty: RTy,
537    tail: &str,
538    local_strings: &HashMap<String, String>,
539    emit: &mut dyn FnMut(String),
540) -> Result<(), String> {
541    if operands.len() != 3 {
542        return Err(format!("expected 3 operands, got {}", operands.len()));
543    }
544    let a = render_value(&operands[1], ty)?;
545    let b = render_value(&operands[0], ty)?;
546    let temp = wrapping_binop(&a, op, &b, ty);
547    let target = branch_target(&operands[2], local_strings)?;
548    let prim = rust_prim(ty);
549    emit("{".to_string());
550    emit(format!("let __t: {} = {};", prim, temp));
551    emit_store(&operands[1], "__t".to_string(), ty, emit)?;
552    emit(format!(
553        "if __t {} {{ {} }}",
554        tail,
555        threaded_return(&target)
556    ));
557    emit("}".to_string());
558    Ok(())
559}
560
561fn emit_op_and_branch_if_overflow(
562    operands: &[Node],
563    op: &str,
564    local_strings: &HashMap<String, String>,
565    emit: &mut dyn FnMut(String),
566) -> Result<(), String> {
567    if operands.len() != 3 {
568        return Err(format!("expected 3 operands, got {}", operands.len()));
569    }
570    let a = render_value(&operands[1], Int32)?;
571    let b = render_value(&operands[0], Int32)?;
572    let method = match op {
573        "+" => "overflowing_add",
574        "-" => "overflowing_sub",
575        "*" => "overflowing_mul",
576        _ => return Err(format!("unimplemented overflow operator {}", op)),
577    };
578    let target = branch_target(&operands[2], local_strings)?;
579    emit("{".to_string());
580    emit(format!(
581        "let (__v, __of): (i32, bool) = ({}).{}({});",
582        a, method, b
583    ));
584    emit_store(&operands[1], "__v".to_string(), Int32, emit)?;
585    emit(format!("if __of {{ {} }}", threaded_return(&target)));
586    emit("}".to_string());
587    Ok(())
588}
589
590fn emit_call_slow_path(
591    operands: &[Node],
592    void_result: bool,
593    emit: &mut dyn FnMut(String),
594) -> Result<(), String> {
595    if operands.is_empty() {
596        return Err("rloopCallSlowPath* needs a target operand".to_string());
597    }
598    let target = raw_label_ident(&operands[0])?;
599    let args: Vec<String> = operands[1..]
600        .iter()
601        .map(canon_dump)
602        .collect::<Result<Vec<_>, _>>()?;
603    emit("{".to_string());
604    emit_synced_call(emit, "self.stack_set_sp(&mut *m);".to_string());
605    if void_result {
606        emit(format!("{}({});", target, args.join(", ")));
607    } else {
608        emit(format!("let __result = {}({});", target, args.join(", ")));
609        emit_synced_call(
610            emit,
611            "let (__t0, __t1) = self.native_result(&mut *m, __result);".to_string(),
612        );
613        emit("regs.t0 = __t0;".to_string());
614        emit("regs.t1 = __t1;".to_string());
615    }
616    emit("}".to_string());
617    Ok(())
618}
619
620fn emit_lea(source: &Node, dest: &Node, emit: &mut dyn FnMut(String)) -> Result<(), String> {
621    match source {
622        Node::Address { base, offset, .. } => {
623            let off = match offset.as_ref() {
624                Node::Immediate { value, .. } => *value,
625                other => {
626                    return Err(format!(
627                        "lea offset must be an immediate, got {}",
628                        other.dump()
629                    ))
630                }
631            };
632            let b = render_value(base, VoidPtr)?;
633            let expr = format!("(((({}) as i64) + ({})) as u64)", b, off);
634            emit_store(dest, expr, Uintptr, emit)
635        }
636        Node::BaseIndex {
637            base,
638            index,
639            offset,
640            ..
641        } => {
642            if !matches!(offset.as_ref(), Node::Immediate { value: 0, .. }) {
643                return Err(format!(
644                    "malformed BaseIndex for lea, offset should be zero at {}",
645                    source.origin_string()
646                ));
647            }
648            let shift = source.scale_shift()?;
649            let b = render_value(base, VoidPtr)?;
650            let idx = render_value(index, Intptr)?;
651            let expr = format!("(((({}) as i64) + (({}) << {})) as u64)", b, idx, shift);
652            emit_store(dest, expr, Uintptr, emit)
653        }
654        Node::LabelReference { label, offset, .. } => {
655            let fname = rust_ident(label);
656            emit_store(dest, opcode_to_u64(&fname), Uintptr, emit)?;
657            if *offset != 0 {
658                match as_reg_ident(dest) {
659                    Some(ident) => {
660                        let ident = ident?;
661                        emit(format!(
662                            "{} = ((({} as i64) + ({})) as u64);",
663                            ident, ident, offset
664                        ));
665                        Ok(())
666                    }
667                    None => {
668                        Err("lea destination with a label + offset must be a register".to_string())
669                    }
670                }
671            } else {
672                Ok(())
673            }
674        }
675        other => Err(format!(
676            "cannot take the address of {} in rloop",
677            other.dump()
678        )),
679    }
680}
681
682const ARITH_OP: &[(&str, RTy, &str)] = &[
683    ("addi", Int32, "+"),
684    ("addq", Int64, "+"),
685    ("addp", Intptr, "+"),
686    ("andi", Int32, "&"),
687    ("andq", Int64, "&"),
688    ("andp", Intptr, "&"),
689    ("ori", Int32, "|"),
690    ("orq", Int64, "|"),
691    ("orp", Intptr, "|"),
692    ("orh", Int16, "|"),
693    ("xori", Int32, "^"),
694    ("xorq", Int64, "^"),
695    ("xorp", Intptr, "^"),
696    ("muli", Int32, "*"),
697    ("mulq", Int64, "*"),
698    ("mulp", Intptr, "*"),
699    ("subi", Int32, "-"),
700    ("subq", Int64, "-"),
701    ("subp", Intptr, "-"),
702    ("addd", Double, "+"),
703    ("divd", Double, "/"),
704    ("subd", Double, "-"),
705    ("muld", Double, "*"),
706];
707
708const UNARY_OP: &[(&str, RTy, &str)] = &[
709    ("negi", Int32, "-"),
710    ("negq", Int64, "-"),
711    ("negp", Intptr, "-"),
712    ("noti", Int32, "~"),
713];
714
715const SHIFT_OP: &[(&str, RTy, &str)] = &[
716    ("lshifti", Int32, "<<"),
717    ("lshiftq", Int64, "<<"),
718    ("lshiftp", Intptr, "<<"),
719    ("rshifti", Int32, ">>"),
720    ("rshiftq", Int64, ">>"),
721    ("rshiftp", Intptr, ">>"),
722    ("urshifti", Uint32, ">>"),
723    ("urshiftq", Uint64, ">>"),
724    ("urshiftp", Uintptr, ">>"),
725];
726
727const CMP_BRANCH: &[(&str, RTy, &str)] = &[
728    ("bbeq", Int8, "=="),
729    ("bieq", Int32, "=="),
730    ("bqeq", Int64, "=="),
731    ("bpeq", Intptr, "=="),
732    ("bbneq", Int8, "!="),
733    ("bineq", Int32, "!="),
734    ("bqneq", Int64, "!="),
735    ("bpneq", Intptr, "!="),
736    ("bba", Uint8, ">"),
737    ("bia", Uint32, ">"),
738    ("bqa", Uint64, ">"),
739    ("bpa", Uintptr, ">"),
740    ("bbaeq", Uint8, ">="),
741    ("biaeq", Uint32, ">="),
742    ("bqaeq", Uint64, ">="),
743    ("bpaeq", Uintptr, ">="),
744    ("bbb", Uint8, "<"),
745    ("bib", Uint32, "<"),
746    ("bqb", Uint64, "<"),
747    ("bpb", Uintptr, "<"),
748    ("bbbeq", Uint8, "<="),
749    ("bibeq", Uint32, "<="),
750    ("bqbeq", Uint64, "<="),
751    ("bpbeq", Uintptr, "<="),
752    ("bbgt", Int8, ">"),
753    ("bigt", Int32, ">"),
754    ("bqgt", Int64, ">"),
755    ("bpgt", Intptr, ">"),
756    ("bbgteq", Int8, ">="),
757    ("bigteq", Int32, ">="),
758    ("bqgteq", Int64, ">="),
759    ("bpgteq", Intptr, ">="),
760    ("bblt", Int8, "<"),
761    ("bilt", Int32, "<"),
762    ("bqlt", Int64, "<"),
763    ("bplt", Intptr, "<"),
764    ("bblteq", Int8, "<="),
765    ("bilteq", Int32, "<="),
766    ("bqlteq", Int64, "<="),
767    ("bplteq", Intptr, "<="),
768];
769
770const DBL_BRANCH: &[(&str, &str)] = &[
771    ("bdeq", "=="),
772    ("bdneq", "!="),
773    ("bdgt", ">"),
774    ("bdgteq", ">="),
775    ("bdlt", "<"),
776    ("bdlteq", "<="),
777];
778
779const DBL_NAN_BRANCH: &[(&str, &str)] = &[
780    ("bdequn", "=="),
781    ("bdnequn", "!="),
782    ("bdgtun", ">"),
783    ("bdgtequn", ">="),
784    ("bdltun", "<"),
785    ("bdltequn", "<="),
786];
787
788const TEST_BRANCH: &[(&str, RTy, &str)] = &[
789    ("btbz", Int8, "== 0"),
790    ("btiz", Int32, "== 0"),
791    ("btqz", Int64, "== 0"),
792    ("btpz", Intptr, "== 0"),
793    ("btbnz", Int8, "!= 0"),
794    ("btinz", Int32, "!= 0"),
795    ("btqnz", Int64, "!= 0"),
796    ("btpnz", Intptr, "!= 0"),
797    ("btbs", Int8, "< 0"),
798    ("btis", Int32, "< 0"),
799    ("btqs", Int64, "< 0"),
800    ("btps", Intptr, "< 0"),
801];
802
803const CMP_SET: &[(&str, RTy, &str)] = &[
804    ("cbeq", Uint8, "=="),
805    ("cieq", Uint32, "=="),
806    ("cqeq", Uint64, "=="),
807    ("cpeq", Uintptr, "=="),
808    ("cbneq", Uint8, "!="),
809    ("cineq", Uint32, "!="),
810    ("cqneq", Uint64, "!="),
811    ("cpneq", Uintptr, "!="),
812    ("cba", Uint8, ">"),
813    ("cia", Uint32, ">"),
814    ("cqa", Uint64, ">"),
815    ("cpa", Uintptr, ">"),
816    ("cbaeq", Uint8, ">="),
817    ("ciaeq", Uint32, ">="),
818    ("cqaeq", Uint64, ">="),
819    ("cpaeq", Uintptr, ">="),
820    ("cbb", Uint8, "<"),
821    ("cib", Uint32, "<"),
822    ("cqb", Uint64, "<"),
823    ("cpb", Uintptr, "<"),
824    ("cbbeq", Uint8, "<="),
825    ("cibeq", Uint32, "<="),
826    ("cqbeq", Uint64, "<="),
827    ("cpbeq", Uintptr, "<="),
828    ("cbgt", Int8, ">"),
829    ("cigt", Int32, ">"),
830    ("cqgt", Int64, ">"),
831    ("cpgt", Intptr, ">"),
832    ("cdgt", Double, ">"),
833    ("cbgteq", Int8, ">="),
834    ("cigteq", Int32, ">="),
835    ("cqgteq", Int64, ">="),
836    ("cpgteq", Intptr, ">="),
837    ("cdgteq", Double, ">="),
838    ("cblt", Int8, "<"),
839    ("cilt", Int32, "<"),
840    ("cqlt", Int64, "<"),
841    ("cplt", Intptr, "<"),
842    ("cdlt", Double, "<"),
843    ("cblteq", Int8, "<="),
844    ("cilteq", Int32, "<="),
845    ("cqlteq", Int64, "<="),
846    ("cplteq", Intptr, "<="),
847    ("cdlteq", Double, "<="),
848];
849
850const TEST_SET: &[(&str, RTy, &str)] = &[
851    ("tbs", Int8, "< 0"),
852    ("tis", Int32, "< 0"),
853    ("tqs", Int64, "< 0"),
854    ("tps", Intptr, "< 0"),
855    ("tbz", Int8, "== 0"),
856    ("tiz", Int32, "== 0"),
857    ("tqz", Int64, "== 0"),
858    ("tpz", Intptr, "== 0"),
859    ("tbnz", Int8, "!= 0"),
860    ("tinz", Int32, "!= 0"),
861    ("tqnz", Int64, "!= 0"),
862    ("tpnz", Intptr, "!= 0"),
863];
864
865const OP_BRANCH: &[(&str, &str, RTy, &str)] = &[
866    ("baddis", "+", Int32, "< 0"),
867    ("baddiz", "+", Int32, "== 0"),
868    ("baddinz", "+", Int32, "!= 0"),
869    ("baddqs", "+", Int64, "< 0"),
870    ("baddqz", "+", Int64, "== 0"),
871    ("baddqnz", "+", Int64, "!= 0"),
872    ("baddps", "+", Intptr, "< 0"),
873    ("baddpz", "+", Intptr, "== 0"),
874    ("baddpnz", "+", Intptr, "!= 0"),
875    ("bsubqs", "-", Int64, "< 0"),
876    ("borqs", "|", Int64, "< 0"),
877    ("bsubqz", "-", Int64, "== 0"),
878    ("borqz", "|", Int64, "== 0"),
879    ("bsubqnz", "-", Int64, "!= 0"),
880    ("borqnz", "|", Int64, "!= 0"),
881    ("bsubps", "-", Intptr, "< 0"),
882    ("borps", "|", Intptr, "< 0"),
883    ("bsubpz", "-", Intptr, "== 0"),
884    ("borpz", "|", Intptr, "== 0"),
885    ("bsubpnz", "-", Intptr, "!= 0"),
886    ("borpnz", "|", Intptr, "!= 0"),
887    ("bsubis", "-", Int32, "< 0"),
888    ("bsubiz", "-", Int32, "== 0"),
889    ("bsubinz", "-", Int32, "!= 0"),
890    ("borris", "|", Int32, "< 0"),
891    ("borriz", "|", Int32, "== 0"),
892    ("borrinz", "|", Int32, "!= 0"),
893];
894
895const OVERFLOW_BRANCH: &[(&str, &str)] = &[("baddio", "+"), ("bsubio", "-"), ("bmulio", "*")];
896
897fn to_dest_last(op: &str, operands: &[Node]) -> Option<Vec<Node>> {
898    let lookup =
899        |table: &'_ [(&'static str, RTy, &'static str)]| table.iter().any(|(o, _, _)| *o == op);
900
901    if lookup(ARITH_OP) {
902        return match operands {
903            [dst, rhs] => Some(vec![rhs.clone(), dst.clone()]),
904            [dst, lhs, rhs] => Some(vec![lhs.clone(), rhs.clone(), dst.clone()]),
905            _ => None,
906        };
907    }
908
909    if lookup(SHIFT_OP) {
910        return match operands {
911            [dst, cnt] => Some(vec![cnt.clone(), dst.clone()]),
912            [dst, cnt, value] => Some(vec![cnt.clone(), value.clone(), dst.clone()]),
913            _ => None,
914        };
915    }
916
917    if lookup(CMP_SET) {
918        return match operands {
919            [dst, lhs, rhs] => Some(vec![lhs.clone(), rhs.clone(), dst.clone()]),
920            _ => None,
921        };
922    }
923
924    if lookup(TEST_SET) {
925        return match operands {
926            [dst, value] => Some(vec![value.clone(), dst.clone()]),
927            [dst, value, mask] => Some(vec![value.clone(), mask.clone(), dst.clone()]),
928            _ => None,
929        };
930    }
931
932    let is_op_branch = OP_BRANCH.iter().any(|(o, _, _, _)| *o == op)
933        || OVERFLOW_BRANCH.iter().any(|(o, _)| *o == op);
934    if is_op_branch {
935        return match operands {
936            [dst, rhs, lbl] => Some(vec![rhs.clone(), dst.clone(), lbl.clone()]),
937            [dst, lhs, rhs, lbl] => Some(vec![lhs.clone(), rhs.clone(), dst.clone(), lbl.clone()]),
938            _ => None,
939        };
940    }
941
942    let two_op = matches!(
943        op,
944        "move"
945            | "sxi2q"
946            | "zxi2q"
947            | "sxb2i"
948            | "sxh2i"
949            | "sxb2q"
950            | "sxb2p"
951            | "sxh2q"
952            | "zxb2i"
953            | "zxh2i"
954            | "zxb2q"
955            | "zxh2q"
956            | "loadi"
957            | "loadis"
958            | "loadq"
959            | "loadp"
960            | "loadb"
961            | "loadbsi"
962            | "loadbsq"
963            | "loadh"
964            | "loadhsi"
965            | "loadhsq"
966            | "loadd"
967            | "storei"
968            | "storeq"
969            | "storep"
970            | "storeb"
971            | "storeh"
972            | "stored"
973            | "transferi"
974            | "transferq"
975            | "transferp"
976            | "ci2ds"
977            | "td2i"
978            | "fq2d"
979            | "fd2q"
980            | "leai"
981            | "leap"
982    );
983    if two_op && operands.len() == 2 {
984        return Some(vec![operands[1].clone(), operands[0].clone()]);
985    }
986
987    if matches!(op, "cmov" | "cmovz" | "movz" | "movn") && operands.len() == 3 {
988        return Some(vec![
989            operands[2].clone(),
990            operands[1].clone(),
991            operands[0].clone(),
992        ]);
993    }
994
995    if matches!(op, "bcd2i" | "btd2i") && operands.len() == 3 {
996        return Some(vec![
997            operands[1].clone(),
998            operands[0].clone(),
999            operands[2].clone(),
1000        ]);
1001    }
1002    None
1003}
1004
1005fn is_block_terminal(op: &str) -> bool {
1006    matches!(
1007        op,
1008        "ret" | "jmp" | "rloopCrash" | "break" | "call" | "rloopCallNativeFunction"
1009    )
1010}
1011
1012fn emit_call_js_function(
1013    operands: &[Node],
1014    cont_name: &str,
1015    emit: &mut dyn FnMut(String),
1016) -> Result<(), String> {
1017    if operands.len() != 1 {
1018        return Err(format!(
1019            "rloopCallNativeFunction expects 1 operand, got {}",
1020            operands.len()
1021        ));
1022    }
1023    let target = render_value(&operands[0], Opcode)?;
1024    emit(format!("regs.lr = {};", opcode_to_u64(cont_name)));
1025    emit(threaded_return(&u64_to_opcode(&target)));
1026    Ok(())
1027}
1028
1029/// Lowers `call target` with `cont_name` as the return point.
1030///
1031/// A label target is an interpreted call: `lr` gets the continuation and
1032/// control moves to the label, and the callee's `ret` comes back. A
1033/// register or memory target is checked at run time. An `Opcode` number
1034/// (what `leap` of a label yields) is an interpreted call as well; anything
1035/// else is a native function address, called through the host's
1036/// `call_native` thunk with `t0`/`t1` as arguments and its result split into
1037/// `t0`/`t1` by `native_result`.
1038fn emit_call(
1039    operands: &[Node],
1040    cont_name: &str,
1041    local_strings: &HashMap<String, String>,
1042    emit: &mut dyn FnMut(String),
1043) -> Result<(), String> {
1044    if operands.len() != 1 {
1045        return Err(format!("call expects 1 operand, got {}", operands.len()));
1046    }
1047    let cont = opcode_to_u64(cont_name);
1048    match &operands[0] {
1049        Node::LabelReference { .. } | Node::LocalLabelReference { .. } => {
1050            let target = branch_target(&operands[0], local_strings)?;
1051            emit(format!("regs.lr = {};", cont));
1052            emit(threaded_return(&target));
1053        }
1054        other => {
1055            let target = render_value(other, Uintptr)?;
1056            emit(format!("let __target: u64 = {};", target));
1057            emit(format!("match {} {{", u64_to_opcode("__target")));
1058            emit("Opcode::Done => {".to_string());
1059            emit_synced_call(emit, "self.stack_set_sp(&mut *m);".to_string());
1060            emit("let (__a0, __a1): (u64, u64) = (regs.t0, regs.t1);".to_string());
1061            emit_synced_call(
1062                emit,
1063                "let __r: u64 = unsafe { self.call_native(&mut *m, __target, __a0, __a1) };"
1064                    .to_string(),
1065            );
1066            emit_synced_call(
1067                emit,
1068                "let (__t0, __t1) = self.native_result(&mut *m, __r);".to_string(),
1069            );
1070            emit("regs.t0 = __t0;".to_string());
1071            emit("regs.t1 = __t1;".to_string());
1072            emit(threaded_return(&format!("Opcode::{}", cont_name)));
1073            emit("}".to_string());
1074            emit("__callee => {".to_string());
1075            emit(format!("regs.lr = {};", cont));
1076            emit(threaded_return("__callee"));
1077            emit("}".to_string());
1078            emit("}".to_string());
1079        }
1080    }
1081    Ok(())
1082}
1083
1084struct Segment {
1085    name: String,
1086    doc: String,
1087    body: Vec<Node>,
1088}
1089
1090impl Segment {
1091    fn mnemonics(&self) -> Vec<&str> {
1092        let mut names: Vec<&str> = Vec::new();
1093        for node in &self.body {
1094            if let Node::Instruction { opcode, .. } = node {
1095                let name = opcode.as_str();
1096                if !names.contains(&name) {
1097                    names.push(name);
1098                }
1099            }
1100        }
1101        names
1102    }
1103
1104    fn doc_tail(&self) -> Vec<String> {
1105        let names = self.mnemonics();
1106        if names.is_empty() {
1107            return Vec::new();
1108        }
1109        let list = names
1110            .iter()
1111            .map(|name| format!("`{}`", name))
1112            .collect::<Vec<String>>()
1113            .join(", ");
1114        vec![
1115            String::new(),
1116            format!("Emitted mnemonics: {}.", list),
1117            "Per-mnemonic syntax and semantics: see the instruction reference in the `offlineasm` crate's `reference` module.".to_string(),
1118        ]
1119    }
1120}
1121
1122pub fn lower_sequence(
1123    list: &[Node],
1124    ctx: &mut LowerCtx,
1125    label_flags: LabelFlags<'_>,
1126) -> Result<(), String> {
1127    let mut segments: Vec<Segment> = Vec::new();
1128    let mut cur_body: Vec<Node> = Vec::new();
1129    let mut cur_name = "__rloop_init".to_string();
1130    let mut cur_doc = "Init segment: label-less prologue statements.".to_string();
1131    let mut uid = 0usize;
1132
1133    for node in list {
1134        match node {
1135            Node::Label { name, .. } => {
1136                segments.push(Segment {
1137                    name: cur_name.clone(),
1138                    doc: cur_doc.clone(),
1139                    body: std::mem::take(&mut cur_body),
1140                });
1141                let (_global, export, _aligned, _extern, _align_to) = label_flags(name);
1142                cur_name = rust_ident(name);
1143                cur_doc = format!(
1144                    "Global label `{}`{}.",
1145                    name,
1146                    if export { " (exported)" } else { "" }
1147                );
1148            }
1149            Node::LocalLabel { name, origin } => {
1150                segments.push(Segment {
1151                    name: cur_name.clone(),
1152                    doc: cur_doc.clone(),
1153                    body: std::mem::take(&mut cur_body),
1154                });
1155                let interned = ctx
1156                    .local_strings
1157                    .get(name)
1158                    .cloned()
1159                    .unwrap_or_else(|| LowerCtx::local_label_string(name, origin));
1160                cur_name = rust_ident(&interned);
1161                cur_doc = format!("Local label `{}`.", name);
1162            }
1163            Node::Skip { .. } => {}
1164            Node::Instruction { opcode, .. } => {
1165                cur_body.push(node.clone());
1166                if matches!(normalize_op(opcode), "rloopCallNativeFunction" | "call") {
1167                    segments.push(Segment {
1168                        name: cur_name.clone(),
1169                        doc: cur_doc.clone(),
1170                        body: std::mem::take(&mut cur_body),
1171                    });
1172                    uid += 1;
1173                    cur_name = format!("rloop_resume_after_call_{}", uid);
1174                    cur_doc = format!("Continuation after host call #{}.", uid);
1175                }
1176            }
1177            other => {
1178                return Err(format!(
1179                    "Unexpected {} at {} (validate first)",
1180                    other.dump(),
1181                    other.origin_string()
1182                ))
1183            }
1184        }
1185    }
1186    segments.push(Segment {
1187        name: cur_name,
1188        doc: cur_doc,
1189        body: cur_body,
1190    });
1191
1192    let start = if segments[0].name == "__rloop_init" && segments[0].body.is_empty() {
1193        1
1194    } else {
1195        0
1196    };
1197    if start >= segments.len() {
1198        return Err("rloop: program has no code".to_string());
1199    }
1200
1201    emit_opcode_enum(ctx, &segments[start..]);
1202    emit_trait_header(ctx, &segments[start..]);
1203    emit_run_method(ctx, &segments[start..]);
1204
1205    for i in start..segments.len() {
1206        let next_name = segments.get(i + 1).map(|s| s.name.clone());
1207        let seg = &segments[i];
1208        ctx.emitter.put_rust(&format!("    /// {}", seg.doc));
1209        for line in seg.doc_tail() {
1210            if line.is_empty() {
1211                ctx.emitter.put_rust("    ///");
1212            } else {
1213                ctx.emitter.put_rust(&format!("    /// {}", line));
1214            }
1215        }
1216        ctx.emitter.put_rust("    #[allow(non_snake_case)]");
1217        ctx.emitter.put_rust(&format!(
1218            "    extern \"rust-preserve-none\" fn {}(&mut self, m: &mut Machine, mut regs: Registers) -> (Opcode, Registers) {{",
1219            seg.name
1220        ));
1221
1222        let mut terminal = false;
1223        for instr in &seg.body {
1224            match instr {
1225                Node::Instruction {
1226                    opcode, operands, ..
1227                } => {
1228                    let normalized = normalize_op(opcode);
1229                    if matches!(normalized, "rloopCallNativeFunction" | "call") {
1230                        let cont = next_name
1231                            .clone()
1232                            .ok_or_else(|| format!("{normalized} with no continuation segment"))?;
1233                        let mut lines: Vec<String> = Vec::new();
1234                        if normalized == "call" {
1235                            emit_call(operands, &cont, &ctx.local_strings, &mut |s| lines.push(s))?;
1236                        } else {
1237                            emit_call_js_function(operands, &cont, &mut |s| lines.push(s))?;
1238                        }
1239                        for l in lines {
1240                            ctx.emitter.put_rust(&format!("            {}", l));
1241                        }
1242                    } else {
1243                        lower_instruction(instr, ctx, label_flags)?;
1244                    }
1245                    terminal = is_block_terminal(normalized);
1246                }
1247                other => return Err(format!("unexpected {} in rloop segment body", other.dump())),
1248            }
1249        }
1250        if !terminal {
1251            match &next_name {
1252                Some(n) => ctx
1253                    .emitter
1254                    .put_rust(&format!("            return (Opcode::{}, regs);", n)),
1255                None => ctx
1256                    .emitter
1257                    .put_rust("            return (Opcode::Done, regs);"),
1258            }
1259        }
1260        ctx.emitter.put_rust("    }");
1261    }
1262
1263    ctx.emitter.put_rust("}");
1264    Ok(())
1265}
1266
1267fn emit_opcode_enum(ctx: &mut LowerCtx, segments: &[Segment]) {
1268    ctx.emitter
1269        .put_rust("/// Every label the generated program can dispatch to.");
1270    ctx.emitter.put_rust("///");
1271    ctx.emitter
1272        .put_rust("/// `Done` is the value a label reference never has: it is what `ret`");
1273    ctx.emitter
1274        .put_rust("/// dispatches to when `lr` is unset, i.e. \"return to the caller\".");
1275    ctx.emitter
1276        .put_rust("#[derive(Clone, Copy, Debug, PartialEq, Eq)]");
1277    ctx.emitter.put_rust("#[allow(non_camel_case_types)]");
1278    ctx.emitter.put_rust("pub enum Opcode {");
1279    ctx.emitter.put_rust("    Done = 0,");
1280    for (index, seg) in segments.iter().enumerate() {
1281        ctx.emitter.put_rust(&format!("    /// {}", seg.doc));
1282        for line in seg.doc_tail() {
1283            if line.is_empty() {
1284                ctx.emitter.put_rust("    ///");
1285            } else {
1286                ctx.emitter.put_rust(&format!("    /// {}", line));
1287            }
1288        }
1289        ctx.emitter
1290            .put_rust(&format!("    {} = {},", seg.name, index + 1));
1291    }
1292    ctx.emitter.put_rust("}");
1293    ctx.emitter.put_rust("");
1294    ctx.emitter.put_rust("impl Opcode {");
1295    ctx.emitter
1296        .put_rust("    /// The raw value stored in `lr`/`pc`.");
1297    ctx.emitter.put_rust("    pub fn as_u64(self) -> u64 {");
1298    ctx.emitter.put_rust("        self as u64");
1299    ctx.emitter.put_rust("    }");
1300    ctx.emitter.put_rust("");
1301    ctx.emitter
1302        .put_rust("    /// Reverses `Opcode::as_u64`. An unknown value (including 0) is");
1303    ctx.emitter
1304        .put_rust("    /// `Done`, i.e. return to the caller.");
1305    ctx.emitter
1306        .put_rust("    pub fn from_u64(value: u64) -> Opcode {");
1307    ctx.emitter.put_rust("        match value {");
1308    for (index, seg) in segments.iter().enumerate() {
1309        ctx.emitter.put_rust(&format!(
1310            "            {} => Opcode::{},",
1311            index + 1,
1312            seg.name
1313        ));
1314    }
1315    ctx.emitter.put_rust("            _ => Opcode::Done,");
1316    ctx.emitter.put_rust("        }");
1317    ctx.emitter.put_rust("    }");
1318    ctx.emitter.put_rust("}");
1319    ctx.emitter.put_rust("");
1320}
1321
1322fn emit_trait_header(ctx: &mut LowerCtx, _segments: &[Segment]) {
1323    ctx.emitter.put_rust("/// A compiled offlineasm program.");
1324    ctx.emitter.put_rust("///");
1325    ctx.emitter
1326        .put_rust("/// The generated program is the *provided* half of this trait: one method");
1327    ctx.emitter
1328        .put_rust("/// per label plus `InterpretedCode::run`. Implement the hooks for");
1329    ctx.emitter
1330        .put_rust("/// whatever the program is embedded in: a JS engine, a bytecode VM, or");
1331    ctx.emitter
1332        .put_rust("/// an ordinary Rust program. Nothing here assumes a host calling");
1333    ctx.emitter
1334        .put_rust("/// convention: every method takes the owned machine state as");
1335    ctx.emitter
1336        .put_rust("/// `m: &mut Machine`, so an implementation can inline the hooks instead");
1337    ctx.emitter
1338        .put_rust("/// of looking interpreter state up indirectly.");
1339    ctx.emitter.put_rust("///");
1340    ctx.emitter
1341        .put_rust("/// Only `call_native` must be implemented: the other hooks have default");
1342    ctx.emitter
1343        .put_rust("/// bodies that serve the machine's own value stack.");
1344    ctx.emitter.put_rust("///");
1345    ctx.emitter
1346        .put_rust("/// The `Ctx` parameter is free for the implementor, so one generated");
1347    ctx.emitter
1348        .put_rust("/// program can be shared by differently-parameterised interpreters.");
1349    ctx.emitter.put_rust("#[allow(non_snake_case)]");
1350    ctx.emitter
1351        .put_rust("pub trait InterpretedCode<Ctx = ()> {");
1352    ctx.emitter
1353        .put_rust("    /// Records the frame the program is running on. The default is a");
1354    ctx.emitter
1355        .put_rust("    /// no-op: the portable backend keeps the frame in `m.regs.sp`, so");
1356    ctx.emitter
1357        .put_rust("    /// there is nothing else to record.");
1358    ctx.emitter
1359        .put_rust("    fn stack_set_sp(&mut self, _m: &mut Machine) {}");
1360    ctx.emitter
1361        .put_rust("    /// Calls the native function at `target` with the two arguments the");
1362    ctx.emitter
1363        .put_rust("    /// calling sequence left in `t0`/`t1`, returning its result. The");
1364    ctx.emitter
1365        .put_rust("    /// machine is passed mutably: a hook that arranges a return address");
1366    ctx.emitter
1367        .put_rust("    /// or resumes elsewhere can update `m.regs` in place.");
1368    ctx.emitter.put_rust("    ///");
1369    ctx.emitter.put_rust("    /// # Safety");
1370    ctx.emitter
1371        .put_rust("    /// `target` must be a valid `extern \"C\" fn(u64, u64) -> u64`.");
1372    ctx.emitter.put_rust("    unsafe fn call_native(");
1373    ctx.emitter.put_rust("        &mut self,");
1374    ctx.emitter.put_rust("        m: &mut Machine,");
1375    ctx.emitter.put_rust("        target: u64,");
1376    ctx.emitter.put_rust("        arg0: u64,");
1377    ctx.emitter.put_rust("        arg1: u64,");
1378    ctx.emitter.put_rust("    ) -> u64;");
1379    ctx.emitter
1380        .put_rust("    /// Splits a native call's return value across the `(t0, t1)` register");
1381    ctx.emitter
1382        .put_rust("    /// pair the program expects. A host with a two-word value");
1383    ctx.emitter
1384        .put_rust("    /// representation (a JS engine, say) decodes it here. A plain Rust");
1385    ctx.emitter
1386        .put_rust("    /// program can keep the default `(value, 0)`.");
1387    ctx.emitter
1388        .put_rust("    fn native_result(&mut self, _m: &mut Machine, value: u64) -> (u64, u64) {");
1389    ctx.emitter.put_rust("        (value, 0)");
1390    ctx.emitter.put_rust("    }");
1391    ctx.emitter.put_rust("");
1392}
1393
1394/// Emits the generated dispatch loop as a provided method.
1395fn emit_run_method(ctx: &mut LowerCtx, segments: &[Segment]) {
1396    ctx.emitter
1397        .put_rust("    /// Runs the program from its first label until it returns to the");
1398    ctx.emitter
1399        .put_rust("    /// caller (`ret` with no return address). The register file is");
1400    ctx.emitter
1401        .put_rust("    /// cached in a local at entry and written back on exit. Each label");
1402    ctx.emitter
1403        .put_rust("    /// takes the cache as an argument and threads it through its return,");
1404    ctx.emitter
1405        .put_rust("    /// so inter-label calls preserve nothing. The machine is owned by");
1406    ctx.emitter
1407        .put_rust("    /// the caller and passed in. Running the same program on two");
1408    ctx.emitter.put_rust("    /// machines shares nothing.");
1409    ctx.emitter.put_rust(
1410        "    extern \"rust-preserve-none\" fn run(&mut self, m: &mut Machine) -> Opcode {",
1411    );
1412    ctx.emitter.put_rust("        let mut regs = m.regs;");
1413    ctx.emitter.put_rust(&format!(
1414        "        let mut opcode = Opcode::{};",
1415        segments[0].name
1416    ));
1417    ctx.emitter.put_rust("        loop {");
1418    ctx.emitter.put_rust("            opcode = match opcode {");
1419    ctx.emitter.put_rust("                Opcode::Done => {");
1420    ctx.emitter.put_rust("                    m.regs = regs;");
1421    ctx.emitter.put_rust("                    return opcode;");
1422    ctx.emitter.put_rust("                }");
1423    for seg in segments {
1424        ctx.emitter
1425            .put_rust(&format!("                Opcode::{} => {{", seg.name));
1426        ctx.emitter.put_rust(&format!(
1427            "                    let (op, nr) = self.{}(&mut *m, regs);",
1428            seg.name
1429        ));
1430        ctx.emitter.put_rust("                    regs = nr;");
1431        ctx.emitter.put_rust("                    op");
1432        ctx.emitter.put_rust("                }");
1433    }
1434    ctx.emitter.put_rust("            };");
1435    ctx.emitter.put_rust("        }");
1436    ctx.emitter.put_rust("    }");
1437    ctx.emitter.put_rust("");
1438}
1439
1440pub fn lower_instruction(
1441    node: &Node,
1442    ctx: &mut LowerCtx,
1443    _flags: LabelFlags<'_>,
1444) -> Result<(), String> {
1445    let (opcode, operands) = match node {
1446        Node::Instruction {
1447            opcode, operands, ..
1448        } => (opcode.as_str(), operands.as_slice()),
1449        _ => return Err("not an instruction".to_string()),
1450    };
1451
1452    for operand in operands {
1453        if let Node::LabelReference { label, .. } = operand {
1454            if ctx.declared_externs.contains_key(label) {
1455                ctx.emitter.put_rust(&format!(
1456                    "    ::core::compile_error!(\"offlineasm: rloop does not support external symbol `{}` (in `{}`)\");",
1457                    label, opcode
1458                ));
1459                return Ok(());
1460            }
1461        }
1462    }
1463    let op = normalize_op(opcode);
1464    let reordered = to_dest_last(op, operands);
1465    let operands: &[Node] = match &reordered {
1466        Some(v) => v.as_slice(),
1467        None => operands,
1468    };
1469
1470    let local_strings = ctx.local_strings.clone();
1471    let mut emit = |s: String| ctx.emitter.put_rust(&format!("    {}", s));
1472
1473    if let Some((_, ty, sym)) = ARITH_OP.iter().find(|(o, _, _)| *o == op) {
1474        return emit_operation(operands, *ty, sym, &mut emit)
1475            .map_err(|e| format!("{} (in `{}`)", e, opcode));
1476    }
1477    if let Some((_, ty, sym)) = UNARY_OP.iter().find(|(o, _, _)| *o == op) {
1478        return emit_unary_operation(operands, *ty, sym, &mut emit)
1479            .map_err(|e| format!("{} (in `{}`)", e, opcode));
1480    }
1481    if let Some((_, ty, sym)) = SHIFT_OP.iter().find(|(o, _, _)| *o == op) {
1482        return emit_shift_operation(operands, *ty, sym, &mut emit)
1483            .map_err(|e| format!("{} (in `{}`)", e, opcode));
1484    }
1485    if let Some((_, ty, cmp)) = CMP_BRANCH.iter().find(|(o, _, _)| *o == op) {
1486        return emit_compare_and_branch(operands, *ty, cmp, &local_strings, &mut emit)
1487            .map_err(|e| format!("{} (in `{}`)", e, opcode));
1488    }
1489    if let Some((_, cmp)) = DBL_BRANCH.iter().find(|(o, _)| *o == op) {
1490        return emit_compare_and_branch(operands, Double, cmp, &local_strings, &mut emit)
1491            .map_err(|e| format!("{} (in `{}`)", e, opcode));
1492    }
1493    if let Some((_, cmp)) = DBL_NAN_BRANCH.iter().find(|(o, _)| *o == op) {
1494        return emit_compare_double_nan_branch(operands, cmp, &local_strings, &mut emit)
1495            .map_err(|e| format!("{} (in `{}`)", e, opcode));
1496    }
1497    if let Some((_, ty, tail)) = TEST_BRANCH.iter().find(|(o, _, _)| *o == op) {
1498        return emit_test_and_branch_if(operands, *ty, tail, &local_strings, &mut emit)
1499            .map_err(|e| format!("{} (in `{}`)", e, opcode));
1500    }
1501    if let Some((_, ty, cmp)) = CMP_SET.iter().find(|(o, _, _)| *o == op) {
1502        return emit_compare_and_set(operands, *ty, cmp, &mut emit)
1503            .map_err(|e| format!("{} (in `{}`)", e, opcode));
1504    }
1505    if let Some((_, ty, tail)) = TEST_SET.iter().find(|(o, _, _)| *o == op) {
1506        return emit_test_set(operands, *ty, tail, &mut emit)
1507            .map_err(|e| format!("{} (in `{}`)", e, opcode));
1508    }
1509    if let Some((_, sym, ty, tail)) = OP_BRANCH.iter().find(|(o, _, _, _)| *o == op) {
1510        return emit_op_and_branch(operands, sym, *ty, tail, &local_strings, &mut emit)
1511            .map_err(|e| format!("{} (in `{}`)", e, opcode));
1512    }
1513    if let Some((_, sym)) = OVERFLOW_BRANCH.iter().find(|(o, _)| *o == op) {
1514        return emit_op_and_branch_if_overflow(operands, sym, &local_strings, &mut emit)
1515            .map_err(|e| format!("{} (in `{}`)", e, opcode));
1516    }
1517
1518    match op {
1519        "localAnnotation" | "globalAnnotation" => Ok(()),
1520        "emit" => {
1521            emit(operands.iter().map(|o| o.dump()).collect::<String>());
1522            Ok(())
1523        }
1524        "tagCodePtr" | "tagReturnAddress" | "untagReturnAddress" | "removeCodePtrTag" | "untagArrayPtr" | "removeArrayPtrTag" => {
1525            Ok(())
1526        }
1527
1528        "move" => {
1529            if operands.len() != 2 {
1530                return Err(format!("move takes 2 operands, got {}", operands.len()));
1531            }
1532            let v = render_value(&operands[0], Int64)?;
1533            emit_store(&operands[1], v, Int64, &mut emit)
1534        }
1535        "sxi2q" => emit_store(&operands[1], render_value(&operands[0], Int32)?, Int64, &mut emit),
1536        "zxi2q" => emit_store(&operands[1], render_value(&operands[0], Uint32)?, Uint64, &mut emit),
1537        "sxb2i" => emit_store(&operands[1], render_value(&operands[0], Int8)?, Int32, &mut emit),
1538        "sxh2i" => emit_store(&operands[1], render_value(&operands[0], Int16)?, Int32, &mut emit),
1539        "sxb2q" | "sxb2p" => emit_store(&operands[1], render_value(&operands[0], Int8)?, Int64, &mut emit),
1540        "sxh2q" => emit_store(&operands[1], render_value(&operands[0], Int16)?, Int64, &mut emit),
1541        "zxb2i" => emit_store(&operands[1], render_value(&operands[0], Uint8)?, Int32, &mut emit),
1542        "zxh2i" => emit_store(&operands[1], render_value(&operands[0], Uint16)?, Int32, &mut emit),
1543        "zxb2q" => emit_store(&operands[1], render_value(&operands[0], Uint8)?, Int64, &mut emit),
1544        "zxh2q" => emit_store(&operands[1], render_value(&operands[0], Uint16)?, Int64, &mut emit),
1545        "cmov" | "cmovz" => {
1546            if operands.len() != 3 {
1547                return Err(format!("{op} needs 3 GPR operands"));
1548            }
1549            let cmp = if op == "cmov" { "!=" } else { "==" };
1550            let t = render_value(&operands[0], Int64)?;
1551            let s = render_value(&operands[1], Int64)?;
1552            let d = render_value(&operands[2], Int64)?;
1553            emit_store(
1554                &operands[2],
1555                format!("if {t} {cmp} 0 {{ {s} }} else {{ {d} }}"),
1556                Int64,
1557                &mut emit,
1558            )
1559        }
1560
1561        "nop" => {
1562            emit("// nop".to_string());
1563            Ok(())
1564        }
1565
1566        "memfence" => Ok(()),
1567
1568        "loadi" => { let v = mem_read(&operands[0], Uint32)?; emit_store(&operands[1], v, Uint32, &mut emit) }
1569        "loadis" => { let v = mem_read(&operands[0], Int32)?; emit_store(&operands[1], v, Int32, &mut emit) }
1570        "loadq" => { let v = mem_read(&operands[0], Int64)?; emit_store(&operands[1], v, Int64, &mut emit) }
1571        "loadp" => { let v = mem_read(&operands[0], Uintptr)?; emit_store(&operands[1], v, Uintptr, &mut emit) }
1572        "loadb" => { let v = mem_read(&operands[0], Uint8)?; emit_store(&operands[1], v, Uint8, &mut emit) }
1573        "loadbsi" => {
1574            let raw = mem_read(&operands[0], Int8)?;
1575            emit_store(&operands[1], format!("(({}) as i32)", raw), Uint32, &mut emit)
1576        }
1577        "loadbsq" => {
1578            let raw = mem_read(&operands[0], Int8)?;
1579            emit_store(&operands[1], format!("(({}) as i64)", raw), Uint64, &mut emit)
1580        }
1581        "loadh" => { let v = mem_read(&operands[0], Uint16)?; emit_store(&operands[1], v, Uint16, &mut emit) }
1582        "loadhsi" => {
1583            let raw = mem_read(&operands[0], Int16)?;
1584            emit_store(&operands[1], format!("(({}) as i32)", raw), Uint32, &mut emit)
1585        }
1586        "loadhsq" => {
1587            let raw = mem_read(&operands[0], Int16)?;
1588            emit_store(&operands[1], format!("(({}) as i64)", raw), Uint64, &mut emit)
1589        }
1590        "loadd" => { let v = mem_read(&operands[0], Double)?; emit_store(&operands[1], v, Double, &mut emit) }
1591
1592        "storei" => { let v = render_value(&operands[0], Int32)?; emit_store(&operands[1], v, Int32, &mut emit) }
1593        "storeq" => { let v = render_value(&operands[0], Int64)?; emit_store(&operands[1], v, Int64, &mut emit) }
1594        "storep" => { let v = render_value(&operands[0], Intptr)?; emit_store(&operands[1], v, Intptr, &mut emit) }
1595        "storeb" => { let v = render_value(&operands[0], Int8)?; emit_store(&operands[1], v, Int8, &mut emit) }
1596        "storeh" => { let v = render_value(&operands[0], Int16)?; emit_store(&operands[1], v, Int16, &mut emit) }
1597        "stored" => { let v = render_value(&operands[0], Double)?; emit_store(&operands[1], v, Double, &mut emit) }
1598
1599        "transferi" => { let v = mem_read(&operands[0], Uint32)?; emit_store(&operands[1], v, Uint32, &mut emit) }
1600
1601        "transferq" => { let v = mem_read(&operands[0], Uint64)?; emit_store(&operands[1], v, Uint64, &mut emit) }
1602        "transferp" => { let v = mem_read(&operands[0], Uintptr)?; emit_store(&operands[1], v, Uintptr, &mut emit) }
1603
1604        "ci2ds" => {
1605            let v = render_value(&operands[0], Int32)?;
1606            emit_store(&operands[1], format!("(({}) as f64)", v), Double, &mut emit)
1607        }
1608        "td2i" => {
1609            let d = render_value(&operands[0], Double)?;
1610            emit_store(&operands[1], format!("(({}) as i64)", d), Uint32, &mut emit)
1611        }
1612        "bcd2i" => {
1613            if operands.len() != 3 {
1614                return Err(format!("bcd2i expects 3 operands, got {}", operands.len()));
1615            }
1616            let d = render_value(&operands[0], Double)?;
1617            let target = branch_target(&operands[2], &local_strings)?;
1618            emit("{".to_string());
1619            emit(format!("let __d: f64 = {};", d));
1620            emit("let __as_i32: i32 = __d as i32;".to_string());
1621            emit(format!(
1622                "if (__as_i32 as f64) != __d || (__as_i32 == 0 && __d.is_sign_negative()) {{ {} }}",
1623                threaded_return(&target)
1624            ));
1625            emit_store(&operands[1], "__as_i32".to_string(), Uint32, &mut emit)?;
1626            emit("}".to_string());
1627            Ok(())
1628        }
1629        "fq2d" => {
1630            let v = render_value(&operands[0], BitsAsDouble)?;
1631            emit_store(&operands[1], v, Double, &mut emit)
1632        }
1633        "fd2q" => {
1634            let v = render_value(&operands[0], BitsAsInt64)?;
1635            emit_store(&operands[1], v, Int64, &mut emit)
1636        }
1637
1638        "leai" | "leap" => {
1639            if operands.len() != 2 {
1640                return Err(format!("{} expects 2 operands, got {}", op, operands.len()));
1641            }
1642            emit_lea(&operands[0], &operands[1], &mut emit)
1643        }
1644
1645        "cdqi" => {
1646            emit("{".to_string());
1647            emit("let __temp: i64 = ((regs.t0 as u32) as i32) as i64;".to_string());
1648            emit("regs.t0 = (((__temp as u32) as u64));".to_string());
1649            emit("regs.t1 = ((((__temp >> 32) as u32) as u64));".to_string());
1650            emit("}".to_string());
1651            Ok(())
1652        }
1653        "idivi" => {
1654            if operands.len() != 1 {
1655                return Err(format!("idivi expects 1 operand, got {}", operands.len()));
1656            }
1657            let divisor = render_value(&operands[0], Intptr)?;
1658            emit("{".to_string());
1659            emit("let __dividend: i64 = (((((regs.t1 as u32) as u64) << 32) | ((regs.t0 as u32) as u64)) as i64);".to_string());
1660            emit(format!("let __divisor: i64 = {};", divisor));
1661            emit("regs.t1 = (((__dividend % __divisor) as u32) as u64);".to_string());
1662            emit("regs.t0 = (((__dividend / __divisor) as u32) as u64);".to_string());
1663            emit("}".to_string());
1664            Ok(())
1665        }
1666
1667        "jmp" => {
1668            if operands.is_empty() {
1669                return Err("jmp takes at least 1 operand".to_string());
1670            }
1671            match &operands[0] {
1672                Node::LabelReference { .. } | Node::LocalLabelReference { .. } => {
1673                    let t = branch_target(&operands[0], &local_strings)?;
1674                    emit(threaded_return(&t));
1675                    Ok(())
1676                }
1677                other => {
1678                    let v = render_value(other, Opcode)?;
1679                    emit(threaded_return(&u64_to_opcode(&v)));
1680                    Ok(())
1681                }
1682            }
1683        }
1684        "call" => Err(
1685            "internal: call must be lowered via lower_sequence's segment splitting".to_string(),
1686        ),
1687        "break" => {
1688            emit("panic!(\"rloop: break is not implemented\");".to_string());
1689            Ok(())
1690        }
1691        "ret" => {
1692            emit(threaded_return(&u64_to_opcode("regs.lr")));
1693            Ok(())
1694        }
1695
1696        "push" => {
1697            for o in operands {
1698                let v = render_value(o, Uintptr)?;
1699                emit(format!("let __push_value: u64 = {};", v));
1700                emit("regs.sp = regs.sp.wrapping_sub(8);".to_string());
1701                emit("unsafe { *(regs.sp as *mut u64) = __push_value; }".to_string());
1702            }
1703            Ok(())
1704        }
1705        "pushCalleeSaves" | "popCalleeSaves" => {
1706            for o in operands {
1707                if !matches!(o, Node::Register { .. }) {
1708                    return Err(format!("{op} takes GPRs only"));
1709                }
1710            }
1711            if op == "pushCalleeSaves" {
1712                for o in operands {
1713                    let v = render_value(o, Uintptr)?;
1714                    emit(format!("let __push_value: u64 = {};", v));
1715                    emit("regs.sp = regs.sp.wrapping_sub(8);".to_string());
1716                    emit("unsafe { *(regs.sp as *mut u64) = __push_value; }".to_string());
1717                }
1718            } else {
1719                // Unwinds in the reverse of `pushCalleeSaves`'s order.
1720                for o in operands.iter().rev() {
1721                    emit("let __pop_value: u64 = unsafe { *(regs.sp as *const u64) };".to_string());
1722                    emit("regs.sp = regs.sp.wrapping_add(8);".to_string());
1723                    emit_store(o, "__pop_value".to_string(), Uintptr, &mut emit)?;
1724                }
1725            }
1726            Ok(())
1727        }
1728        "pop" => {
1729            for o in operands.iter().rev() {
1730                emit("let __pop_value: u64 = unsafe { *(regs.sp as *const u64) };".to_string());
1731                emit("regs.sp = regs.sp.wrapping_add(8);".to_string());
1732                emit_store(o, "__pop_value".to_string(), Uintptr, &mut emit)?;
1733            }
1734            Ok(())
1735        }
1736
1737        "rloopCrash" => {
1738            emit("panic!(\"rloop crash\");".to_string());
1739            Ok(())
1740        }
1741        "rloopCallNativeFunction" => {
1742            Err("internal: rloopCallNativeFunction must be lowered via lower_sequence's segment splitting".to_string())
1743        }
1744        "rloopCallNative" => {
1745            if operands.len() != 1 {
1746                return Err(format!("rloopCallNative expects 1 operand, got {}", operands.len()));
1747            }
1748            let target = render_value(&operands[0], NativeFunc)?;
1749            emit("{".to_string());
1750            emit_synced_call(&mut emit, "self.stack_set_sp(&mut *m);".to_string());
1751            emit(format!("let __target: u64 = {};", target));
1752            emit("let __a0: u64 = regs.t0;".to_string());
1753            emit("let __a1: u64 = regs.t1;".to_string());
1754            emit_synced_call(
1755                &mut emit,
1756                "let __r: u64 = unsafe { self.call_native(&mut *m, __target, __a0, __a1) };".to_string(),
1757            );
1758            emit("regs.t0 = __r;".to_string());
1759            emit("}".to_string());
1760            Ok(())
1761        }
1762        "rloopCallSlowPath" | "rloopCallSlowPath3" | "rloopCallSlowPath4" => emit_call_slow_path(operands, false, &mut emit),
1763        "rloopCallSlowPathVoid" => emit_call_slow_path(operands, true, &mut emit),
1764        "rloopDo" => {
1765            if let Node::Instruction { annotation, .. } = node {
1766                emit(annotation.clone().unwrap_or_default());
1767            }
1768            Ok(())
1769        }
1770
1771        _ => Err(format!(
1772            "Unhandled opcode {} for rloop at {}",
1773            opcode,
1774            node.origin_string()
1775        )),
1776    }
1777}