Apf: rename addLoad16(R0, x) -> addLoad16intoR0(x)

This is to allow 'LDH R=1' to be repurposed in the future.

Generated via:
  f=(src/android/net/apf/ApfFilter.java src/android/net/apf/ApfV4GeneratorBase.java tests/unit/src/android/net/apf/ApfGeneratorTest.kt tests/unit/src/android/net/apf/ApfStandaloneTest.kt tests/unit/src/android/net/apf/ApfTest.java)
  for i in ${f[@]}; do echo $i; sed -i -r 's@addLoad16[(]R0, @addLoad16intoR0(@' "$i"; done
  git grep 'addLoad16[(]'
  mcedit src/android/net/apf/ApfV4GeneratorBase.java:121

+ addSwap().addLoad16(x).addSwap() fix for one spot where we did use R1

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I164797b53dd26c836c13199889a632168de255e4
diff --git a/src/android/net/apf/ApfFilter.java b/src/android/net/apf/ApfFilter.java
index a99742a..a3a59c5 100644
--- a/src/android/net/apf/ApfFilter.java
+++ b/src/android/net/apf/ApfFilter.java
@@ -1445,7 +1445,7 @@
                 } else {
                     switch (section.length) {
                         // length asserted to be either 2 or 4 on PacketSection construction
-                        case 2: gen.addLoad16(R0, section.start); break;
+                        case 2: gen.addLoad16intoR0(section.start); break;
                         case 4: gen.addLoad32intoR0(section.start); break;
                     }
 
@@ -1579,7 +1579,7 @@
             gen.addLoadFromMemory(R0, MemorySlot.IPV4_HEADER_SIZE);
             gen.addAdd(UDP_HEADER_LEN);
             gen.addSwap();
-            gen.addLoad16(R0, IPV4_TOTAL_LENGTH_OFFSET);
+            gen.addLoad16intoR0(IPV4_TOTAL_LENGTH_OFFSET);
             gen.addNeg(R1);
             gen.addAddR1ToR0();
             gen.addJumpIfR0NotEquals(1, nextFilterLabel);
@@ -1702,7 +1702,9 @@
             // R0 += R1 -> R0 contains TCP + IP headers length
             gen.addAddR1ToR0();
             // Load IPv4 total length
-            gen.addLoad16(R1, IPV4_TOTAL_LENGTH_OFFSET);
+            gen.addSwap();
+            gen.addLoad16intoR0(IPV4_TOTAL_LENGTH_OFFSET);
+            gen.addSwap();
             gen.addNeg(R0);
             gen.addAddR1ToR0();
             gen.addJumpIfR0NotEquals(0, nextFilterLabel);
@@ -1816,7 +1818,7 @@
 
         final short checkArpRequest = gen.getUniqueLabel();
 
-        gen.addLoad16(R0, ARP_OPCODE_OFFSET);
+        gen.addLoad16intoR0(ARP_OPCODE_OFFSET);
         gen.addJumpIfR0Equals(ARP_OPCODE_REQUEST, checkArpRequest); // Skip to arp request check.
         // Drop if unknown ARP opcode.
         gen.addCountAndDropIfR0NotEquals(ARP_OPCODE_REPLY, DROPPED_ARP_UNKNOWN);
@@ -1971,7 +1973,7 @@
                 .addJumpIfR0LessThan(
                         ETH_HEADER_LEN + IPV4_HEADER_MIN_LEN + UDP_HEADER_LEN + DNS_HEADER_LEN,
                         skipMdnsFilter)
-                .addLoad16(R0, IPV4_UDP_DESTINATION_PORT_NO_OPTIONS_OFFSET)
+                .addLoad16intoR0(IPV4_UDP_DESTINATION_PORT_NO_OPTIONS_OFFSET)
                 .addJumpIfR0NotEquals(MDNS_PORT, skipMdnsFilter);
 
         // If the destination MAC address is not 01:00:5e:00:00:fb (the mDNS multicast MAC
@@ -2005,7 +2007,7 @@
 
         // If the packet contains questions, check the query payload. Otherwise, check the
         // reply payload.
-        gen.addLoad16(R0, IPV4_DNS_QDCOUNT_NO_OPTIONS_OFFSET)
+        gen.addLoad16intoR0(IPV4_DNS_QDCOUNT_NO_OPTIONS_OFFSET)
                 // Set the UDP payload offset in R1 before potentially jumping to the payload
                 // check logic.
                 .addLoadImmediate(R1, IPV4_UDP_PAYLOAD_NO_OPTIONS_OFFSET)
@@ -2342,7 +2344,7 @@
                 .addCountAndDropIfR0NotEquals(255, DROPPED_IPV6_NS_INVALID);
 
         // payload length < 24 (8 bytes ICMP6 header + 16 bytes target address) -> drop
-        v6Gen.addLoad16(R0, IPV6_PAYLOAD_LEN_OFFSET)
+        v6Gen.addLoad16intoR0(IPV6_PAYLOAD_LEN_OFFSET)
                 .addCountAndDropIfR0LessThan(24, DROPPED_IPV6_NS_INVALID);
 
         // ICMPv6 code not 0 -> drop
@@ -2383,7 +2385,7 @@
         // For option-less NUD packets or NUD/Address resolution packets where
         // the first option is not SLLA, pass them to the kernel for handling.
         // if payload len < 32 -> pass
-        v6Gen.addLoad16(R0, IPV6_PAYLOAD_LEN_OFFSET)
+        v6Gen.addLoad16intoR0(IPV6_PAYLOAD_LEN_OFFSET)
                 .addCountAndPassIfR0LessThan(32, PASSED_IPV6_NS_NO_SLLA_OPTION);
 
         // if the first option is not SLLA -> pass
@@ -2429,7 +2431,7 @@
                 .addJumpIfR0LessThan(
                         ETH_HEADER_LEN + IPV6_HEADER_LEN + UDP_HEADER_LEN + DNS_HEADER_LEN,
                         skipMdnsFilter)
-                .addLoad16(R0, IPV6_UDP_DESTINATION_PORT_OFFSET)
+                .addLoad16intoR0(IPV6_UDP_DESTINATION_PORT_OFFSET)
                 .addJumpIfR0NotEquals(MDNS_PORT, skipMdnsFilter);
 
         // If the destination MAC address is not 33:33:00:00:00:fb (the mDNS multicast MAC
@@ -2460,7 +2462,7 @@
 
         // If the packet contains questions, check the query payload. Otherwise, check the
         // reply payload.
-        gen.addLoad16(R0, IPV6_DNS_QDCOUNT_OFFSET)
+        gen.addLoad16intoR0(IPV6_DNS_QDCOUNT_OFFSET)
                 // Set the UDP payload offset in R1 before potentially jumping to the payload
                 // check logic.
                 .addLoadImmediate(R1, IPv6_UDP_PAYLOAD_OFFSET)
@@ -2903,7 +2905,7 @@
 
         // Calculate the IPv4 payload length: (total length - IPv4 header length).
         // Memory slot 0 is occupied temporarily to store the length.
-        v6Gen.addLoad16(R0, IPV4_TOTAL_LENGTH_OFFSET)
+        v6Gen.addLoad16intoR0(IPV4_TOTAL_LENGTH_OFFSET)
                 .addLoadFromMemory(R1, MemorySlot.IPV4_HEADER_SIZE)
                 .addNeg(R1)
                 .addAddR1ToR0()
@@ -3200,7 +3202,7 @@
         // has not yet acquired a valid link-local address.
         // Its OK to not check :: here since we also drop MLD reports.
         // If the source address is a not a link-local address, then drop.
-        gen.addLoad16(R0, IPV6_SRC_ADDR_OFFSET)
+        gen.addLoad16intoR0(IPV6_SRC_ADDR_OFFSET)
                 .addCountAndDropIfR0NotEquals(0xfe80, DROPPED_IPV6_MLD_INVALID);
 
         // If hop limit is not 1, then drop.
@@ -3252,7 +3254,7 @@
         gen.addJumpIfR0NotEquals(IPPROTO_TCP, skipPort7V4Filter);
 
         // Check it's not a fragment or is the initial fragment.
-        gen.addLoad16(R0, IPV4_FRAGMENT_OFFSET_OFFSET);
+        gen.addLoad16intoR0(IPV4_FRAGMENT_OFFSET_OFFSET);
         gen.addJumpIfR0AnyBitsSet(IPV4_FRAGMENT_OFFSET_MASK, skipPort7V4Filter);
 
         // Check it's destination port 7.
@@ -3378,7 +3380,7 @@
                 gen.addCountAndPass(PASSED_MDNS);
             } else {
                 if (enableMdns4 && enableMdns6) {
-                    gen.addLoad16(R0, ETH_ETHERTYPE_OFFSET)
+                    gen.addLoad16intoR0(ETH_ETHERTYPE_OFFSET)
                             .addJumpIfR0NotEquals(ETH_P_IP, offloadIPv6Mdns);
                 }
 
@@ -3518,7 +3520,7 @@
         gen.addLoadImmediate(R0, ETHER_SRC_ADDR_OFFSET);
         gen.addCountAndPassIfBytesAtR0Equal(mHardwareAddress, PASSED_ETHER_OUR_SRC_MAC);
 
-        gen.addLoad16(R0, ETH_ETHERTYPE_OFFSET);
+        gen.addLoad16intoR0(ETH_ETHERTYPE_OFFSET);
         if (SdkLevel.isAtLeastV()) {
             // IPv4, ARP, IPv6, EAPOL, WAPI
             gen.addCountAndDropIfR0IsNoneOf(Set.of(0x0800L, 0x0806L, 0x86DDL, 0x888EL, 0x88B4L),
@@ -3544,7 +3546,7 @@
         generateArpFilter(gen);
         gen.defineLabel(skipArpFiltersLabel);
 
-        gen.addLoad16(R0, ETH_ETHERTYPE_OFFSET);
+        gen.addLoad16intoR0(ETH_ETHERTYPE_OFFSET);
 
         // Add IPv4 filters:
         short skipIPv4FiltersLabel = gen.getUniqueLabel();
diff --git a/src/android/net/apf/ApfV4GeneratorBase.java b/src/android/net/apf/ApfV4GeneratorBase.java
index 1fa17ca..122929a 100644
--- a/src/android/net/apf/ApfV4GeneratorBase.java
+++ b/src/android/net/apf/ApfV4GeneratorBase.java
@@ -118,8 +118,8 @@
      * Add an instruction to the end of the program to load 16-bits at offset {@code offset}
      * bytes from the beginning of the packet into {@code register}.
      */
-    public final Type addLoad16(Register r, int ofs) {
-        return append(new Instruction(Opcodes.LDH, r).addPacketOffset(ofs));
+    public final Type addLoad16intoR0(int ofs) {
+        return append(new Instruction(Opcodes.LDH, R0).addPacketOffset(ofs));
     }
 
     /**
diff --git a/tests/unit/src/android/net/apf/ApfStandaloneTest.kt b/tests/unit/src/android/net/apf/ApfStandaloneTest.kt
index 7a6b5ca..d2f3c14 100644
--- a/tests/unit/src/android/net/apf/ApfStandaloneTest.kt
+++ b/tests/unit/src/android/net/apf/ApfStandaloneTest.kt
@@ -300,7 +300,7 @@
         gen.addStoreData(R0, 0)
 
         // ethtype filter
-        gen.addLoad16(R0, ETHER_TYPE_OFFSET)
+        gen.addLoad16intoR0(ETHER_TYPE_OFFSET)
         maybeSetupCounter(gen, Counter.DROPPED_ETHERTYPE_DENYLISTED)
         for (p in etherTypeDenyList) {
             gen.addJumpIfR0Equals(p.toLong(), countAndDropLabel)
@@ -309,7 +309,7 @@
         // dhcp request filters
 
         // Check IPv4
-        gen.addLoad16(R0, ETHER_TYPE_OFFSET)
+        gen.addLoad16intoR0(ETHER_TYPE_OFFSET)
         gen.addJumpIfR0NotEquals(ETH_P_IP.toLong(), endOfDhcpFilter)
 
         // Pass DHCP addressed to us.
@@ -335,7 +335,7 @@
         // rs filters
 
         // check IPv6
-        gen.addLoad16(R0, ETHER_TYPE_OFFSET)
+        gen.addLoad16intoR0(ETHER_TYPE_OFFSET)
         gen.addJumpIfR0NotEquals(OsConstants.ETH_P_IPV6.toLong(), endOfRsFilter)
         // check ICMP6 packet
         gen.addLoad8intoR0(IPV6_NEXT_HEADER_OFFSET)
@@ -353,7 +353,7 @@
             // ping filter
 
             // Check IPv4
-            gen.addLoad16(R0, ETHER_TYPE_OFFSET)
+            gen.addLoad16intoR0(ETHER_TYPE_OFFSET)
             gen.addJumpIfR0NotEquals(ETH_P_IP.toLong(), endOfPingFilter)
             // Check it's ICMP.
             gen.addLoad8intoR0(IPV4_PROTOCOL_OFFSET)
diff --git a/tests/unit/src/android/net/apf/ApfTest.java b/tests/unit/src/android/net/apf/ApfTest.java
index f813e61..4903926 100644
--- a/tests/unit/src/android/net/apf/ApfTest.java
+++ b/tests/unit/src/android/net/apf/ApfTest.java
@@ -545,7 +545,7 @@
 
         // Test half-word load.
         gen = new ApfV4Generator(APF_VERSION_2, mRamSize, mClampSize);
-        gen.addLoad16(R0, 1);
+        gen.addLoad16intoR0(1);
         gen.addJumpIfR0Equals((45 << 8) | 67, DROP_LABEL);
         assertDrop(gen, new byte[]{123,45,67,0,0,0,0,0,0,0,0,0,0,0,0}, 0);
 
@@ -2871,7 +2871,7 @@
         gen.addLoadData(R0, 0);
         gen.addAdd(1);
         gen.addStoreData(R0, 0);
-        gen.addLoad16(R0, 12);
+        gen.addLoad16intoR0(12);
         gen.addLoadImmediate(R1, -108);
         gen.addJumpIfR0LessThan(0x600, (short) -504);
         gen.addLoadImmediate(R1, -112);
@@ -2885,7 +2885,7 @@
         gen.addLoadImmediate(R0, 14);
         gen.addLoadImmediate(R1, -36);
         gen.addJumpIfBytesAtR0NotEqual(hexStringToByteArray("000108000604"), (short) -498);
-        gen.addLoad16(R0, 20);
+        gen.addLoad16intoR0(20);
         gen.addJumpIfR0Equals(0x1, (short) -102);
         gen.addLoadImmediate(R1, -40);
         gen.addJumpIfR0NotEquals(0x2, (short) -498);
@@ -2904,11 +2904,11 @@
         gen.addJump((short) -498);
 
         gen.defineLabel((short) -116);
-        gen.addLoad16(R0, 12);
+        gen.addLoad16intoR0(12);
         gen.addJumpIfR0NotEquals(0x800, (short) -207);
         gen.addLoad8intoR0(23);
         gen.addJumpIfR0NotEquals(0x11, (short) -159);
-        gen.addLoad16(R0, 20);
+        gen.addLoad16intoR0(20);
         gen.addJumpIfR0AnyBitsSet(0x1fff, (short) -159);
         gen.addLoadFromMemory(R1, MemorySlot.IPV4_HEADER_SIZE);
         gen.addLoad16R1IndexedIntoR0(16);
@@ -2973,7 +2973,7 @@
         gen.addJumpIfBytesAtR0NotEqual(hexStringToByteArray("00703afffe800000000000002a0079e10abc1539fe80000000000000e01250fffe7c63458600"), (short) -496);
         gen.addLoadImmediate(R0, 58);
         gen.addJumpIfBytesAtR0NotEqual(hexStringToByteArray("4000"), (short) -496);
-        gen.addLoad16(R0, 60);
+        gen.addLoad16intoR0(60);
         gen.addJumpIfR0LessThan(0x254, (short) -496);
         gen.addLoadImmediate(R0, 62);
         gen.addJumpIfBytesAtR0NotEqual(hexStringToByteArray("0000000000000000"), (short) -496);
@@ -3025,7 +3025,7 @@
         gen.addLoadData(R0, 0);
         gen.addAdd(1);
         gen.addStoreData(R0, 0);
-        gen.addLoad16(R0, 12);
+        gen.addLoad16intoR0(12);
         gen.addLoadImmediate(R1, -108);
         gen.addJumpIfR0LessThan(0x600, (short) -283);
         gen.addLoadImmediate(R1, -112);
@@ -3039,7 +3039,7 @@
         gen.addLoadImmediate(R0, 14);
         gen.addLoadImmediate(R1, -36);
         gen.addJumpIfBytesAtR0NotEqual(hexStringToByteArray("000108000604"), (short) -277);
-        gen.addLoad16(R0, 20);
+        gen.addLoad16intoR0(20);
         gen.addJumpIfR0Equals(0x1, (short) -94);
         gen.addLoadImmediate(R1, -40);
         gen.addJumpIfR0NotEquals(0x2, (short) -277);
@@ -3058,11 +3058,11 @@
         gen.addJump((short) -277);
 
         gen.defineLabel((short) -109);
-        gen.addLoad16(R0, 12);
+        gen.addLoad16intoR0(12);
         gen.addJumpIfR0NotEquals(0x800, (short) -204);
         gen.addLoad8intoR0(23);
         gen.addJumpIfR0NotEquals(0x11, (short) -151);
-        gen.addLoad16(R0, 20);
+        gen.addLoad16intoR0(20);
         gen.addJumpIfR0AnyBitsSet(0x1fff, (short) -151);
         gen.addLoadFromMemory(R1, MemorySlot.IPV4_HEADER_SIZE);
         gen.addLoad16R1IndexedIntoR0(16);
@@ -3146,7 +3146,7 @@
         gen.addLoadData(R0, 0);
         gen.addAdd(1);
         gen.addStoreData(R0, 0);
-        gen.addLoad16(R0, 12);
+        gen.addLoad16intoR0(12);
         gen.addCountAndDropIfR0LessThan(0x600, getCounterEnumFromOffset(-108));
         gen.addLoadImmediate(R1, -112);
         gen.addJumpIfR0Equals(0x88a2, gen.mCountAndDropLabel);
@@ -3158,7 +3158,7 @@
         gen.addJumpIfR0NotEquals(0x806, (short) -115);
         gen.addLoadImmediate(R0, 14);
         gen.addCountAndPassIfBytesAtR0NotEqual(hexStringToByteArray("000108000604"), getCounterEnumFromOffset(-36));
-        gen.addLoad16(R0, 20);
+        gen.addLoad16intoR0(20);
         gen.addJumpIfR0Equals(0x1, (short) -100);
         gen.addCountAndPassIfR0NotEquals(0x2, getCounterEnumFromOffset(-40));
         gen.addLoad32intoR0(28);
@@ -3172,11 +3172,11 @@
         gen.addCountAndPass(getCounterEnumFromOffset(-8));
 
         gen.defineLabel((short) -115);
-        gen.addLoad16(R0, 12);
+        gen.addLoad16intoR0(12);
         gen.addJumpIfR0NotEquals(0x800, (short) -263);
         gen.addLoad8intoR0(23);
         gen.addJumpIfR0NotEquals(0x11, (short) -157);
-        gen.addLoad16(R0, 20);
+        gen.addLoad16intoR0(20);
         gen.addJumpIfR0AnyBitsSet(0x1fff, (short) -157);
         gen.addLoadFromMemory(R1, MemorySlot.IPV4_HEADER_SIZE);
         gen.addLoad16R1IndexedIntoR0(16);
@@ -3201,7 +3201,7 @@
         gen.addLoadFromMemory(R0, MemorySlot.IPV4_HEADER_SIZE);
         gen.addAdd(8);
         gen.addSwap();
-        gen.addLoad16(R0, 16);
+        gen.addLoad16intoR0(16);
         gen.addNeg(R1);
         gen.addAddR1ToR0();
         gen.addJumpIfR0NotEquals(0x1, (short) -243);
@@ -3261,7 +3261,7 @@
         gen.addLoadCounter(R0, getCounterEnumFromOffset(-8));
         gen.addAdd(1);
         gen.addStoreData(R0, 0);
-        gen.addLoad16(R0, 12);
+        gen.addLoad16intoR0(12);
         gen.addCountAndDropIfR0LessThan(0x600, getCounterEnumFromOffset(-120));
         gen.addLoadImmediate(R1, -124);
         gen.addJumpIfR0Equals(0x88a2, gen.mCountAndDropLabel);
@@ -3273,7 +3273,7 @@
         gen.addJumpIfR0NotEquals(0x806, (short) -122);
         gen.addLoadImmediate(R0, 14);
         gen.addCountAndDropIfBytesAtR0NotEqual(hexStringToByteArray("000108000604"), getCounterEnumFromOffset(-152));
-        gen.addLoad16(R0, 20);
+        gen.addLoad16intoR0(20);
         gen.addJumpIfR0Equals(0x1, (short) -104);
         gen.addCountAndDropIfR0NotEquals(0x2, getCounterEnumFromOffset(-156));
         gen.addLoad32intoR0(28);
@@ -3287,11 +3287,11 @@
         gen.addCountAndPass(getCounterEnumFromOffset(-20));
 
         gen.defineLabel((short) -122);
-        gen.addLoad16(R0, 12);
+        gen.addLoad16intoR0(12);
         gen.addJumpIfR0NotEquals(0x800, (short) -249);
         gen.addLoad8intoR0(23);
         gen.addJumpIfR0NotEquals(0x11, (short) -165);
-        gen.addLoad16(R0, 20);
+        gen.addLoad16intoR0(20);
         gen.addJumpIfR0AnyBitsSet(0x1fff, (short) -165);
         gen.addLoadFromMemory(R1, MemorySlot.IPV4_HEADER_SIZE);
         gen.addLoad16R1IndexedIntoR0(16);
@@ -3311,7 +3311,7 @@
         gen.addCountAndDropIfR0Equals(0xc0a801ff, getCounterEnumFromOffset(-92));
         gen.addLoad8intoR0(23);
         gen.addJumpIfR0NotEquals(0x6, (short) -225);
-        gen.addLoad16(R0, 20);
+        gen.addLoad16intoR0(20);
         gen.addJumpIfR0AnyBitsSet(0x1fff, (short) -225);
         gen.addLoadFromMemory(R1, MemorySlot.IPV4_HEADER_SIZE);
         gen.addLoad16R1IndexedIntoR0(16);
@@ -3361,7 +3361,7 @@
         gen.addJumpIfBytesAtR0NotEqual(hexStringToByteArray("8600"), (short) -574);
         gen.addLoadImmediate(R0, 58);
         gen.addJumpIfBytesAtR0NotEqual(hexStringToByteArray("40c0"), (short) -574);
-        gen.addLoad16(R0, 60);
+        gen.addLoad16intoR0(60);
         gen.addJumpIfR0Equals(0x0, (short) -574);
         gen.addJumpIfR0LessThan(0xb4, (short) -421);
         gen.addJumpIfR0LessThan(0x91e, (short) -574);