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

    <tr>
        <th>Summary</th>
        <td>
            Issues with udf when assembling for Cortex-M3
        </td>
    </tr>

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

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

    <tr>
      <th>Reporter</th>
      <td>
          mateusz-banaszek
      </td>
    </tr>
</table>

<pre>
    When using clang to assemble for ARM Cortex-M3, I've come across 2 issues with the `udf` (Permanently Undefined) instruction. To illustrate:

```asm
.syntax unified

@ Issue 1
udf     #256

@ Issue 2
it eq
udfeq   #20
```

An attempt to assemble that using clang v19.1.5:

```
clang -nodefaultlibs -mcpu=cortex-m3 --target=armv7m-none-eabi ./example.s
```

results in 2 errors:

```
./example.s:4:9: error: operand must be an immediate in the range [0,255]
udf     #256
        ^
./example.s:8:1: error: instruction 'udf' is not predicable, but condition code specified
udfeq #20
^
```

However, I don't see a reason to that. *Arm v7-M Architecture Reference Manual* shows 2 instruction encodings: T2 accepts `imm16`, and both accept a condition:

> Encoding T1: UDF\<c> #\<imm8>
> Encoding T2: UDF\<c>.W #\<imm16>

Surprisingly, the [`udf-thumb-2-diagnostics.s`](https://github.com/llvm/llvm-project/blob/main/llvm/test/MC/ARM/udf-thumb-2-diagnostics.s) test verifies that clang does generate these errors. However, even after reviewing the [`27351f2`](https://github.com/llvm/llvm-project/commit/27351f2022c56b830f91d7f526775693fd9043e9) commit which implemented `udf`, I still don't see a reason to that. I expect that the snippet is assembled to:

```
f7f0 a100   udf.w #256
bf08   it eq
de14   udfeq #20
```

So I expect that for Issue 1 clang chooses itself the wide encoding (as it does for, e.g., `adds r0, r1, #1024` vs. `adds r0, r1, #2`), whereas for Issue 2 clang assembles it as the instruction accepts the condition and it is predicable (*Arm v7-M Architecture Reference Manual*: *"`UNDEFINED` Indicates an instruction that generates an Undefined Instruction exception."*). GCC assembles it correctly: https://godbolt.org/z/Y8q7KbzKG.

Or is there something I don't understand?
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJycVluPozgT_TXOSynImBDCQx6YpDNfa9Tzreai0T4auwjeBTtjm6S7f_2qIN1JX0arWYTExWX71Kk65ZIhmL1FXLP8A8u3MznE1vl1LyMO4XFeSyvDI_49q51-WP9o0cIQjN2D6qTdQ3QgQ8C-7hAa56H6cgcb5yPez-8yJjZwy0RxRFCuR5DKuxBAgAlhwAAnE1uILQJb8kE3bMmBidUf6Htp0cbuAb5bjY2xqJkowdgQ_aCicTaBbw5M1w0hehmRZRXj473k0y1Dz3iVhAcb5T0M1jQG9dlmweGWAEDKeDXoBuhiIhP58rWFYLwyEfDnZIk_z5b8eq9pUmVBxoj9Ib4gJbYyvmDsmJZJmuRvITNeTRZz6zQ2cuhiZ-oA814dBpZt1URrn8F8HqXfY2TZVvr-WPRz6yzOUdYGEiZ2eC_7Q4dJeIvSYxi6GMBYEIDeOx_eRfJymaxasKwqWVZNc-jFHdBLq6EfQoQaQVowfY_ayIi0PMXVS7tHYPkHzsRG5DnLt-9RDueL5TfvbL1iWZW-2PoqEYCJglJHFGACWBfh4FEbJesOKf3qIYJyVpvRWDmNEA6ontJhiulzRMf9XzH2P3fCI_oxl0E7y0QRISCCBI8yOEvhpignwERV-R6OxfwOKq9aE1HFwSN8wQY9WoVwJ-0gOyYqCK07jVq4cgatctrYPbkN3wRIpfAQA-nD9H26JFBiA0R77WJ7Hgd5cfESzOwGbs7LwbeRv-_bHcs3LNsoGmQim75M369YdvNmjngzJ_nxYla6PE_j1dfBH7yhLO8eCOEo6vzDpOt5bIe-nou5NnJvXYhGhSSQL_mWiVUb42FMQrFjYrc3sR3qRLmeiV3XHZ8e84N3f6GKTOzqztVM7Hpp7MUmYqCxuw0Tu-rLHRO7X-8sSiBzOKKnRAiTSCfxaYcB9miR6gr5EfCskwSuUgGPaEE2ET14PBo8EWUXr0WR5Wkj_ruPyvW9o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