Skip to content

RISC-V inline asm: lone I/J/K immediate constraint now evaluates provably-constant operands instead of unconditional refusal #28

Description

@awto-au

Summary

translate_machine_constraint for RISC-V unconditionally returned None for a lone "I"/"J"/"K" immediate-only constraint (no "r" GPR fallback), correctly refusing to guess when the operand's constant-ness can't be verified — but this also refused genuinely compile-time-constant expressions (integer literals and shift/mask arithmetic over #defined constants, produced by macro substitution — confirmed via direct AST dump that Clang does not constant-fold these before c2rust's own AST processing, even under -Os).

Real, live example

arch/riscv/include/asm/errata_list.h's ALT_SVPBMT macro, used by riscv_page_mtmask()/riscv_page_io() (arch/riscv/include/asm/pgtable-64.h), called from drivers/tty/serial/8250/8250_port.c (found while investigating whether that file — the real source behind this project's existing hand-split 8250_helpers/io/irq/startup_rs.rs translations — can be cleanly transpiled end-to-end by current c2rust). Both functions previously fell back to unresolvable extern "C" fn stubs despite being genuinely called.

Fix

New narrow try_eval_const_int evaluator (integer literals + shift/mask/add/sub arithmetic only, not a general C constant-expression evaluator) recognizes provably-constant operands and emits them as real Rust asm! const operands. Existing refusal behavior preserved byte-for-byte for anything not provably constant.

Verification

  • Direct reproduction: riscv_page_mtmask()/riscv_page_io() now emit real asm! bodies with correctly evaluated const operands (hand-verified against the _SVPBMT/_THEAD macro definitions). Full diff against baseline shows exactly these two functions changed in the 13.7k-line file, nothing else.
  • Full corpus baseline: 591/591 clean, matching pre-fix exactly.
  • Decl-level regression check: 0 regressed, 0 removed.
  • Corpus-wide impact: 15 files show reduced warning counts (new const-eval successes), 0 files regressed.

Commit: 1045f867d (merge of 3777500da), landed on master.

Related/superseded: this extends the constraint-mapping work from #10 (closed) — that fixed the general A/I/J/K unmapped-constraint-letter crash; this fixes the specific "correctly-mapped-but-conservatively-refused" case for provably-constant I/J/K operands.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions