Skip to content

ICMP destination register is hardcoded to R0 #6

Description

@SuperInstance

Audit Finding

Severity: Medium
File(s): opcodes.py (ICMP definition), VM execution engine

Problem

The ICMP (Integer Compare) instruction always writes its result to register R0, regardless of the operands:

# Current behavior (simplified):
def execute_icmp(rs1, rs2):
    if REGS[rs1] < REGS[rs2]:
        REGS[0] = 0x01   # always writes to R0
    ...

This means:

  • You cannot use ICMP results in general computation without clobbering R0
  • Chained comparisons are impossible without saving/restoring R0
  • It violates the RISC principle that most ALU ops should support a destination register

Impact

  • Any compiler or hand-written code that needs to compare values while preserving R0 will produce incorrect results
  • Makes register allocation significantly harder for any FLUX compiler

Suggested Fix

Redesign ICMP to use a 3-register format (like ADD/SUB):

ICMP rd, rs1, rs2   ; rd = compare(rs1, rs2)  → result flags in rd

If backward compatibility is required, introduce ICMP3 as the new variant and deprecate the old ICMP.

Alternatively, if the intent is to set global flags (like x86 FLAGS register), document this explicitly and provide a mechanism to read flags into an arbitrary register.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions