<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/151453>151453</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [MC][MIPS]`llvm-mc` Crashes with SIGSEGV When Parsing Invalid MIPS beql Instructions with `--arch=mips`
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          venkyqz
      </td>
    </tr>
</table>

<pre>
    # Description
When using the latest trunk version `Clang` with the `--arch=mips` option to assemble MIPS assembly code containing invalid `beql` instructions (e.g., using floating-point registers, memory references, or invalid syntax as operands), the assembler crashes with a `SIGSEGV` due to an assertion failure in `MipsAsmParser::expandBranchImm`. This issue was discovered in Compiler Explorer using `x86-64 clang (assertions trunk)`.


# Interesting Facts

+ **Clang Versions Before 4.0.1**: In Clang versions before `4.0.1`, `Clang` does not crash and correctly reports invalid MIPS instructions with errors like "invalid operand for instruction". This suggests the crash is a regression or issue introduced in later versions.

+ **Crash Triggered by `--arch=mips`**: The crash only occurs when the `--arch=mips` flag is explicitly set. The MIPS backend seems to fail to handle invalid operands, leading to a SIGSEGV.

+ **No Crash When Architecture Is Inferred**: Without the `--arch=mips flag`, `Clang` infers the architecture itself and does not crash, instead reporting errors like "invalid instruction mnemonic 'beql'".

# Related Issues
This issue may be related to [#22763](https://github.com/llvm/llvm-project/issues/22763), which addressed incomplete support for immediate branch pseudo-instructions in the MIPS integrated assembler.

# Steps to Reproduce
Here is the comprehensive reproduce link in compiler explorer: https://godbolt.org/z/xf4WavzxE

<img width="3000" height="1577" alt="Image" src="https://github.com/user-attachments/assets/136a169e-b0ef-43ee-98d6-1871dfe22121" />
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJycVktv2zgQ_jX0ZWBDoizZOfjgPNz1oYuiKdozRY4kbihSJSkn7q9fDKU4bpvu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