<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" encoding="UTF-8" href="iform.xsl" version="1.0"?>
<!DOCTYPE instructionsection PUBLIC "-//ARM//DTD instructionsection //EN" "iform-p.dtd">
<!-- Copyright (c) 2010-2022 Arm Limited or its affiliates. All rights reserved. -->
<!-- This document is Non-Confidential. This document may only be used and distributed in accordance with the terms of the agreement entered into by Arm and the party that Arm delivered this document to. -->

<instructionsection id="BFM" title="BFM -- A64" type="instruction">
  <docvars>
    <docvar key="bitfield-fill" value="nofill" />
    <docvar key="instr-class" value="general" />
    <docvar key="isa" value="A64" />
    <docvar key="mnemonic" value="BFM" />
  </docvars>
  <heading>BFM</heading>
  <desc>
    <brief>
      <para>Bitfield Move</para>
    </brief>
    <authored>
      <para>Bitfield Move is usually accessed via one of its aliases, which are always preferred for disassembly.</para>
      <para>If <syntax>&lt;imms&gt;</syntax> is greater than or equal to <syntax>&lt;immr&gt;</syntax>, this copies a bitfield of (<syntax>&lt;imms&gt;</syntax>-<syntax>&lt;immr&gt;</syntax>+1) bits starting from bit position <syntax>&lt;immr&gt;</syntax> in the source register to the least significant bits of the destination register.</para>
      <para>If <syntax>&lt;imms&gt;</syntax> is less than <syntax>&lt;immr&gt;</syntax>, this copies a bitfield of (<syntax>&lt;imms&gt;</syntax>+1) bits from the least significant bits of the source register to bit position (regsize-<syntax>&lt;immr&gt;</syntax>) of the destination register, where regsize is the destination register size of 32 or 64 bits.</para>
      <para>In both cases the other bits of the destination register remain unchanged.</para>
    </authored>
  </desc>
  <operationalnotes>
    <para>If PSTATE.DIT is 1:</para>
    <list type="unordered">
      <listitem><content>The execution time of this instruction is independent of:<list type="unordered"><listitem><content>The values of the data supplied in any of its registers.</content></listitem><listitem><content>The values of the NZCV flags.</content></listitem></list></content></listitem>
      <listitem><content>The response of this instruction to asynchronous exceptions does not vary based on:<list type="unordered"><listitem><content>The values of the data supplied in any of its registers.</content></listitem><listitem><content>The values of the NZCV flags.</content></listitem></list></content></listitem>
    </list>
  </operationalnotes>
  <alias_list howmany="3">
    <alias_list_intro>This instruction is used by the aliases </alias_list_intro>
    <aliasref aliaspageid="BFC_BFM" aliasfile="bfc_bfm.xml" hover="Bitfield Clear" punct=", ">
      <text>BFC</text>
      <aliaspref>Rn == '11111' &amp;&amp; <a link="impl-shared.UInt.1" file="shared_pseudocode.xml" hover="function: integer UInt(bits(N) x)">UInt</a>(imms) &lt; <a link="impl-shared.UInt.1" file="shared_pseudocode.xml" hover="function: integer UInt(bits(N) x)">UInt</a>(immr)</aliaspref>
    </aliasref>
    <aliasref aliaspageid="BFI_BFM" aliasfile="bfi_bfm.xml" hover="Bitfield insert" punct=" and ">
      <text>BFI</text>
      <aliaspref>Rn != '11111' &amp;&amp; <a link="impl-shared.UInt.1" file="shared_pseudocode.xml" hover="function: integer UInt(bits(N) x)">UInt</a>(imms) &lt; <a link="impl-shared.UInt.1" file="shared_pseudocode.xml" hover="function: integer UInt(bits(N) x)">UInt</a>(immr)</aliaspref>
    </aliasref>
    <aliasref aliaspageid="BFXIL_BFM" aliasfile="bfxil_bfm.xml" hover="Bitfield extract and insert at low end" punct=".">
      <text>BFXIL</text>
      <aliaspref><a link="impl-shared.UInt.1" file="shared_pseudocode.xml" hover="function: integer UInt(bits(N) x)">UInt</a>(imms) &gt;= <a link="impl-shared.UInt.1" file="shared_pseudocode.xml" hover="function: integer UInt(bits(N) x)">UInt</a>(immr)</aliaspref>
    </aliasref>
    <alias_list_outro>
      <text>  See </text>
      <aliastablelink />
      <text> below for details of when each alias is preferred.</text>
    </alias_list_outro>
  </alias_list>
  <classes>
    <iclass name="Leaving other bits unchanged" oneof="1" id="iclass_nofill" no_encodings="2" isa="A64">
      <docvars>
        <docvar key="bitfield-fill" value="nofill" />
        <docvar key="instr-class" value="general" />
        <docvar key="isa" value="A64" />
        <docvar key="mnemonic" value="BFM" />
      </docvars>
      <iclassintro count="2"></iclassintro>
      <regdiagram form="32" psname="aarch64/instrs/integer/bitfield" tworows="1">
        <box hibit="31" name="sf" usename="1">
          <c></c>
        </box>
        <box hibit="30" width="2" name="opc" usename="1" settings="2" psbits="xx">
          <c>0</c>
          <c>1</c>
        </box>
        <box hibit="28" width="6" settings="6">
          <c>1</c>
          <c>0</c>
          <c>0</c>
          <c>1</c>
          <c>1</c>
          <c>0</c>
        </box>
        <box hibit="22" name="N" usename="1">
          <c></c>
        </box>
        <box hibit="21" width="6" name="immr" usename="1">
          <c colspan="6"></c>
        </box>
        <box hibit="15" width="6" name="imms" usename="1">
          <c colspan="6"></c>
        </box>
        <box hibit="9" width="5" name="Rn" usename="1">
          <c colspan="5"></c>
        </box>
        <box hibit="4" width="5" name="Rd" usename="1">
          <c colspan="5"></c>
        </box>
      </regdiagram>
      <encoding name="BFM_32M_bitfield" oneofinclass="2" oneof="2" label="32-bit" bitdiffs="sf == 0 &amp;&amp; N == 0">
        <docvars>
          <docvar key="bitfield-fill" value="nofill" />
          <docvar key="datatype" value="32" />
          <docvar key="instr-class" value="general" />
          <docvar key="isa" value="A64" />
          <docvar key="mnemonic" value="BFM" />
        </docvars>
        <box hibit="31" width="1" name="sf">
          <c>0</c>
        </box>
        <box hibit="22" width="1" name="N">
          <c>0</c>
        </box>
        <asmtemplate><text>BFM  </text><a link="sa_wd" hover="32-bit general-purpose destination register (field &quot;Rd&quot;)">&lt;Wd&gt;</a><text>, </text><a link="sa_wn" hover="32-bit general-purpose source register (field &quot;Rn&quot;)">&lt;Wn&gt;</a><text>, #</text><a link="sa_immr" hover="Right rotate amount [0-31] (field &quot;immr&quot;)">&lt;immr&gt;</a><text>, #</text><a link="sa_imms" hover="Leftmost bit number to be moved from the source [0-31] (field &quot;imms&quot;)">&lt;imms&gt;</a></asmtemplate>
      </encoding>
      <encoding name="BFM_64M_bitfield" oneofinclass="2" oneof="2" label="64-bit" bitdiffs="sf == 1 &amp;&amp; N == 1">
        <docvars>
          <docvar key="bitfield-fill" value="nofill" />
          <docvar key="datatype" value="64" />
          <docvar key="instr-class" value="general" />
          <docvar key="isa" value="A64" />
          <docvar key="mnemonic" value="BFM" />
        </docvars>
        <box hibit="31" width="1" name="sf">
          <c>1</c>
        </box>
        <box hibit="22" width="1" name="N">
          <c>1</c>
        </box>
        <asmtemplate><text>BFM  </text><a link="sa_xd" hover="64-bit general-purpose destination register (field &quot;Rd&quot;)">&lt;Xd&gt;</a><text>, </text><a link="sa_xn" hover="64-bit general-purpose source register (field &quot;Rn&quot;)">&lt;Xn&gt;</a><text>, #</text><a link="sa_immr_1" hover="Right rotate amount [0-63] (field &quot;immr&quot;)">&lt;immr&gt;</a><text>, #</text><a link="sa_imms_1" hover="Leftmost bit number to be moved from the source [0-63] (field &quot;imms&quot;)">&lt;imms&gt;</a></asmtemplate>
      </encoding>
      <ps_section howmany="1">
        <ps name="aarch64/instrs/integer/bitfield" mylink="aarch64.instrs.integer.bitfield" enclabels="" sections="1" secttype="noheading">
          <pstext mayhavelinks="1" section="Decode" rep_section="decode">integer d = <a link="impl-shared.UInt.1" file="shared_pseudocode.xml" hover="function: integer UInt(bits(N) x)">UInt</a>(Rd);
integer n = <a link="impl-shared.UInt.1" file="shared_pseudocode.xml" hover="function: integer UInt(bits(N) x)">UInt</a>(Rn);
integer datasize = if sf == '1' then 64 else 32;

boolean inzero;
boolean extend;
integer r;
integer s;
bits(datasize) wmask;
bits(datasize) tmask;

case opc of
    when '00' inzero = TRUE;  extend = TRUE;    // SBFM
    when '01' inzero = FALSE; extend = FALSE;   // BFM
    when '10' inzero = TRUE;  extend = FALSE;   // UBFM
    when '11' UNDEFINED;

if sf == '1' &amp;&amp; N != '1' then UNDEFINED;
if sf == '0' &amp;&amp; (N != '0' || immr&lt;5&gt; != '0' || imms&lt;5&gt; != '0') then UNDEFINED;

r = <a link="impl-shared.UInt.1" file="shared_pseudocode.xml" hover="function: integer UInt(bits(N) x)">UInt</a>(immr);
s = <a link="impl-shared.UInt.1" file="shared_pseudocode.xml" hover="function: integer UInt(bits(N) x)">UInt</a>(imms);
(wmask, tmask) = <a link="impl-aarch64.DecodeBitMasks.5" file="shared_pseudocode.xml" hover="function: (bits(M), bits(M)) DecodeBitMasks(bit immN, bits(6) imms, bits(6) immr, boolean immediate, integer M)">DecodeBitMasks</a>(N, imms, immr, FALSE, datasize);</pstext>
        </ps>
      </ps_section>
    </iclass>
  </classes>
  <explanations scope="all">
    <explanation enclist="BFM_32M_bitfield" symboldefcount="1">
      <symbol link="sa_wd">&lt;Wd&gt;</symbol>
      <account encodedin="Rd">
        <intro>
          <para>Is the 32-bit name of the general-purpose destination register, encoded in the "Rd" field.</para>
        </intro>
      </account>
    </explanation>
    <explanation enclist="BFM_32M_bitfield" symboldefcount="1">
      <symbol link="sa_wn">&lt;Wn&gt;</symbol>
      <account encodedin="Rn">
        <intro>
          <para>Is the 32-bit name of the general-purpose source register, encoded in the "Rn" field.</para>
        </intro>
      </account>
    </explanation>
    <explanation enclist="BFM_64M_bitfield" symboldefcount="1">
      <symbol link="sa_xd">&lt;Xd&gt;</symbol>
      <account encodedin="Rd">
        <intro>
          <para>Is the 64-bit name of the general-purpose destination register, encoded in the "Rd" field.</para>
        </intro>
      </account>
    </explanation>
    <explanation enclist="BFM_64M_bitfield" symboldefcount="1">
      <symbol link="sa_xn">&lt;Xn&gt;</symbol>
      <account encodedin="Rn">
        <intro>
          <para>Is the 64-bit name of the general-purpose source register, encoded in the "Rn" field.</para>
        </intro>
      </account>
    </explanation>
    <explanation enclist="BFM_32M_bitfield" symboldefcount="1">
      <symbol link="sa_immr">&lt;immr&gt;</symbol>
      <account encodedin="immr">
        <docvars>
          <docvar key="datatype" value="32" />
        </docvars>
        <intro>
          <para>For the 32-bit variant: is the right rotate amount, in the range 0 to 31, encoded in the "immr" field.</para>
        </intro>
      </account>
    </explanation>
    <explanation enclist="BFM_64M_bitfield" symboldefcount="2">
      <symbol link="sa_immr_1">&lt;immr&gt;</symbol>
      <account encodedin="immr">
        <docvars>
          <docvar key="datatype" value="64" />
        </docvars>
        <intro>
          <para>For the 64-bit variant: is the right rotate amount, in the range 0 to 63, encoded in the "immr" field.</para>
        </intro>
      </account>
    </explanation>
    <explanation enclist="BFM_32M_bitfield" symboldefcount="1">
      <symbol link="sa_imms">&lt;imms&gt;</symbol>
      <account encodedin="imms">
        <docvars>
          <docvar key="datatype" value="32" />
        </docvars>
        <intro>
          <para>For the 32-bit variant: is the leftmost bit number to be moved from the source, in the range 0 to 31, encoded in the "imms" field.</para>
        </intro>
      </account>
    </explanation>
    <explanation enclist="BFM_64M_bitfield" symboldefcount="2">
      <symbol link="sa_imms_1">&lt;imms&gt;</symbol>
      <account encodedin="imms">
        <docvars>
          <docvar key="datatype" value="64" />
        </docvars>
        <intro>
          <para>For the 64-bit variant: is the leftmost bit number to be moved from the source, in the range 0 to 63, encoded in the "imms" field.</para>
        </intro>
      </account>
    </explanation>
  </explanations>
  <aliastablehook anchor="aliasconditions">Alias Conditions</aliastablehook>
  <ps_section howmany="1">
    <ps name="aarch64/instrs/integer/bitfield" mylink="execute" enclabels="" sections="1" secttype="Operation">
      <pstext mayhavelinks="1" section="Execute" rep_section="execute">bits(datasize) dst = if inzero then <a link="impl-shared.Zeros.1" file="shared_pseudocode.xml" hover="function: bits(N) Zeros(integer N)">Zeros</a>(datasize) else <a link="impl-aarch64.X.read.2" file="shared_pseudocode.xml" hover="accessor: bits(width) X[integer n, integer width]">X</a>[d, datasize];
bits(datasize) src = <a link="impl-aarch64.X.read.2" file="shared_pseudocode.xml" hover="accessor: bits(width) X[integer n, integer width]">X</a>[n, datasize];

// perform bitfield move on low bits
bits(datasize) bot = (dst AND NOT(wmask)) OR (<a link="impl-shared.ROR.2" file="shared_pseudocode.xml" hover="function: bits(N) ROR(bits(N) x, integer shift)">ROR</a>(src, r) AND wmask);

// determine extension bits (sign, zero or dest register)
bits(datasize) top = if extend then <a link="impl-shared.Replicate.2" file="shared_pseudocode.xml" hover="function: bits(M*N) Replicate(bits(M) x, integer N)">Replicate</a>(src&lt;s&gt;, datasize) else dst;

// combine extension bits and result bits
<a link="impl-aarch64.X.write.2" file="shared_pseudocode.xml" hover="accessor: X[integer n, integer width] = bits(width) value">X</a>[d, datasize] = (top AND NOT(tmask)) OR (bot AND tmask);</pstext>
    </ps>
  </ps_section>
</instructionsection>
