Debian Codegraph @

src/org/tukaani/xz/simple/RISCVEncoder.java

raw symbols SCIP
1 // SPDX-License-Identifier: 0BSD
2 // SPDX-FileCopyrightText: The XZ for Java authors and contributors
3 // SPDX-FileContributor: Lasse Collin <[email protected]>
4
5 package org.tukaani.xz.simple;
6
7 import org.tukaani.xz.common.ByteArrayView;
8
9 // BCJ filter encoder for RISC-V instructions
10 public final class RISCVEncoder implements SimpleFilter {
11 private int pos;
12
13 public RISCVEncoder(int startPos) {
14 pos = startPos;
15 }
16
17 @Override
18 public int code(byte[] buf, int off, int len) {
19 int end = off + len - 8;
20 int i;
21
22 // The loop is advanced by 2 bytes every iteration since the
23 // instruction stream may include 16-bit instructions (C extension).
24 for (i = off; i <= end; i += 2) {
25 int inst = buf[i] & 0xFF;
26
27 if (inst == 0xEF) {
28 // JAL
29 final int b1 = buf[i + 1] & 0xFF;
30
31 // Only filter rd=x1(ra) and rd=x5(t0).
32 if ((b1 & 0x0D) != 0)
33 continue;
34
35 // The 20-bit immediate is in four pieces.
36 // The encoder stores it in big endian form
37 // since it improves compression slightly.
38 final int b2 = buf[i + 2] & 0xFF;
39 final int b3 = buf[i + 3] & 0xFF;
40 final int pc = pos + i - off;
41
42 // The following chart shows the highest three bytes of JAL, focusing on
43 // the 20-bit immediate field [31:12]. The first row of numbers is the
44 // bit position in a 32-bit little endian instruction. The second row of
45 // numbers shows the order of the immediate field in a J-type instruction.
46 // The last row is the bit number in each byte.
47 //
48 // To determine the amount to shift each bit, subtract the value in
49 // the last row from the value in the second last row. If the number
50 // is positive, shift left. If negative, shift right.
51 //
52 // For example, at the rightmost side of the chart, the bit 4 in b1 is
53 // the bit 12 of the address. Thus that bit needs to be shifted left
54 // by 12 - 4 = 8 bits to put it in the right place in the addr variable.
55 //
56 // NOTE: The immediate of a J-type instruction holds bits [20:1] of
57 // the address. The bit [0] is always 0 and not part of the immediate.
58 //
59 // | b3 | b2 | b1 |
60 // | 31 30 29 28 27 26 25 24 | 23 22 21 20 19 18 17 16 | 15 14 13 12 x x x x |
61 // | 20 10 9 8 7 6 5 4 | 3 2 1 11 19 18 17 16 | 15 14 13 12 x x x x |
62 // | 7 6 5 4 3 2 1 0 | 7 6 5 4 3 2 1 0 | 7 6 5 4 x x x x |
63
64 int addr = ((b1 & 0xF0) << 8)
65 | ((b2 & 0x0F) << 16)
66 | ((b2 & 0x10) << 7)
67 | ((b2 & 0xE0) >>> 4)
68 | ((b3 & 0x7F) << 4)
69 | ((b3 & 0x80) << 13);
70
71 addr += pc;
72
73 buf[i + 1] = (byte)((b1 & 0x0F) | ((addr >>> 13) & 0xF0));
74 buf[i + 2] = (byte)(addr >>> 9);
75 buf[i + 3] = (byte)(addr >>> 1);
76
77 // The "-2" is included because the for-loop will always
78 // increment by 2. In this case, we want to skip an extra
79 // 2 bytes since we used 4 bytes of input.
80 i += 4 - 2;
81
82 } else if ((inst & 0x7F) == 0x17) {
83 // AUIPC
84 inst |= (buf[i + 1] & 0xFF) << 8;
85 inst |= (buf[i + 2] & 0xFF) << 16;
86 inst |= (buf[i + 3] & 0xFF) << 24;
87
88 // Branch based on AUIPC's rd. The bitmask test does
89 // the same thing as this:
90 //
91 // int auipcRd = (inst >>> 7) & 0x1F;
92 // if (auipcRd != 0 && auipcRd != 2) {
93 if ((inst & 0xE80) != 0) {
94 // AUIPC's rd doesn't equal x0 or x2.
95
96 // Check if AUIPC+inst2 are a pair.
97 int inst2 = ByteArrayView.getIntLE(buf, i + 4);
98
99 // This checks two conditions at once:
100 // - AUIPC rd == inst2 rs1.
101 // - inst2 opcode has the lowest two bits set.
102 //
103 // The 8 bit left shift aligns the rd of AUIPC with
104 // the rs1 of inst2. By XORing the registers, any
105 // non-zero value in those bits indicates the registers
106 // are not equal and thus not an AUIPC pair. The mask
107 // tests if any of the register or opcode bits are set.
108 // Only the lowest two opcode bits should be set if inst2
109 // is a pair to AUIPC.
110 if ((((inst << 8) ^ inst2) & 0xF8003) != 3) {
111 // The above check allows a false AUIPC+AUIPC pair
112 // if the bits [19:15] (where rs1 would be) in the
113 // second AUIPC match the rd of the first AUIPC.
114 //
115 // We must skip enough forward so that the first two
116 // bytes of the second AUIPC cannot get converted.
117 // Such a conversion could make the current pair
118 // become a valid pair which would desync the decoder.
119 //
120 // Skipping six bytes is enough even though the above
121 // condition looks at the lowest four bits of the
122 // buf[i + 6] too. This is safe because this filter
123 // never changes those bits if a conversion at
124 // that position is done.
125 i += 6 - 2;
126 continue;
127 }
128
129 // Convert AUIPC+inst2 to a special format:
130 //
131 // - The lowest 7 bits [6:0] retain the AUIPC opcode.
132 //
133 // - The rd [11:7] is set to x2(sp). x2 is used as
134 // the stack pointer so AUIPC with rd=x2 should be
135 // very rare in real-world executables.
136 //
137 // - The remaining 20 bits [31:12] (that normally hold
138 // the pc-relative immediate) are used to store the
139 // lowest 20 bits of inst2. That is, the 12-bit
140 // immediate of inst2 is not included.
141 //
142 // - The location of the original inst2 is used to store
143 // the 32-bit absolute address in big endian format.
144 // Compared to the 20+12-bit split encoding, this
145 // results in a longer uninterrupted sequence of
146 // identical common bytes when the same address is
147 // referred with different instruction pairs (like
148 // AUIPC+LD vs. AUIPC+ADDI) or when the occurrences
149 // of the same pair use different registers. When
150 // referring to adjacent memory locations (like
151 // function calls that go via the ELF PLT), in
152 // big endian order only the last 1-2 bytes differ;
153 // in little endian the differing 1-2 bytes would be
154 // in the middle of the 8-byte sequence.
155 //
156 // When reversing the transformation, the original rd
157 // of AUIPC can be restored from inst2's rs1 as they
158 // are required to be the same.
159
160 // Arithmetic right shift makes sign extension trivial.
161 int addr = (inst & 0xFFFFF000) + (inst2 >> 20);
162
163 addr += pos + i - off;
164
165 // Construct the first 32 bits:
166 // [6:0] AUIPC opcode
167 // [11:7] Special AUIPC rd = x2
168 // [31:12] The lowest 20 bits of inst2
169 inst = 0x17 | (2 << 7) | (inst2 << 12);
170
171 ByteArrayView.setIntLE(buf, i, inst);
172
173 // The second 32 bits store the absolute
174 // address in big endian order.
175 ByteArrayView.setIntBE(buf, i + 4, addr);
176 } else {
177 // AUIPC's rd equals x0 or x2.
178 //
179 // x0 indicates a landing pad (LPAD). It's always skipped.
180 //
181 // AUIPC with rd == x2 is used for the special format
182 // as explained above. When the input contains a byte
183 // sequence that matches the special format, "fake"
184 // decoding must be done to keep the filter bijective
185 // (that is, safe to apply on arbitrary data).
186 //
187 // See the "x0 or x2" section in RISCVDecoder for how
188 // the "real" decoding is done. The "fake" decoding is
189 // a simplified version of "real" decoding with the
190 // following differences:
191 // (1) The lowest 12 bits aren't sign-extended.
192 // (2) No address conversion is done.
193 // (3) Big endian format isn't used (the fake
194 // address is in little endian order).
195
196 // Check if inst matches the special format.
197 final int fakeRs1 = inst >>> 27;
198
199 // This checks multiple conditions:
200 // (1) AUIPC rd [11:7] == x2 (special rd value).
201 // (2) AUIPC bits 12 and 13 set (the lowest two
202 // opcode bits of packed inst2).
203 // (3) inst2 rs1 doesn't equal x0 or x2 because
204 // the opposite conversion is only done when
205 // AUIPC rd != x0 &&
206 // AUIPC rd != x2 &&
207 // AUIPC rd == inst2 rs1.
208 //
209 // The left-hand side takes care of (1) and (2).
210 // (a) If AUIPC rd equals x2, subtracting 0x100 makes
211 // bits [11:7] zeros. If rd doesn't equal x2,
212 // then there will be at least one non-zero bit
213 // and the next step (b) is irrelevant.
214 // (b) If the lowest two opcode bits of the packed inst2
215 // are set in [13:12], then subtracting 0x3000 will
216 // make those bits zeros. Otherwise there will be
217 // at least one non-zero bit.
218 //
219 // The bitwise-and removes the uninteresting bits from
220 // the final '>=' comparison and ensures that any
221 // non-zero result will be larger than any possible
222 // result from the right-hand side of the comparison.
223 //
224 // On the right-hand side, rs1 & 0x1D will be non-zero
225 // as long as rs1 is not x0 or x2.
226 //
227 // The final '>=' comparison will make the expression
228 // true if:
229 // - The subtraction caused any bits to be set
230 // (special AUIPC rd value not used or inst2
231 // opcode bits not set). (non-zero >= non-zero or 0)
232 // - The subtraction did not cause any bits to be set
233 // but rs1 was x0 or x2. (0 >= 0)
234 if (((inst - 0x3100) & 0x3F80) >= (fakeRs1 & 0x1D)) {
235 i += 4 - 2;
236 continue;
237 }
238
239 final int fakeAddr = ByteArrayView.getIntLE(buf, i + 4);
240
241 // Construct the second 32 bits:
242 // [19:0] Upper 20 bits from AUIPC
243 // [31:20] The lowest 12 bits of fakeAddr
244 final int fakeInst2 = (inst >>> 12) | (fakeAddr << 20);
245
246 // Construct new first 32 bits from:
247 // [6:0] AUIPC opcode
248 // [11:7] Fake AUIPC rd = fakeRs1
249 // [31:12] The highest 20 bits of fakeAddr
250 inst = 0x17 | (fakeRs1 << 7) | (fakeAddr & 0xFFFFF000);
251
252 ByteArrayView.setIntLE(buf, i, inst);
253 ByteArrayView.setIntLE(buf, i + 4, fakeInst2);
254 }
255
256 i += 8 - 2;
257 }
258 }
259
260 i -= off;
261 pos += i;
262 return i;
263 }
264 }