xref: /freebsd-src/contrib/xz/src/liblzma/simple/riscv.c (revision 3b35e7ee8de9b0260149a2b77e87a2b9c7a36244)
1*3b35e7eeSXin LI // SPDX-License-Identifier: 0BSD
2*3b35e7eeSXin LI 
3*3b35e7eeSXin LI ///////////////////////////////////////////////////////////////////////////////
4*3b35e7eeSXin LI //
5*3b35e7eeSXin LI /// \file       riscv.c
6*3b35e7eeSXin LI /// \brief      Filter for 32-bit/64-bit little/big endian RISC-V binaries
7*3b35e7eeSXin LI ///
8*3b35e7eeSXin LI /// This converts program counter relative addresses in function calls
9*3b35e7eeSXin LI /// (JAL, AUIPC+JALR), address calculation of functions and global
10*3b35e7eeSXin LI /// variables (AUIPC+ADDI), loads (AUIPC+load), and stores (AUIPC+store).
11*3b35e7eeSXin LI ///
12*3b35e7eeSXin LI /// For AUIPC+inst2 pairs, the paired instruction checking is fairly relaxed.
13*3b35e7eeSXin LI /// The paired instruction opcode must only have its lowest two bits set,
14*3b35e7eeSXin LI /// meaning it will convert any paired instruction that is not a 16-bit
15*3b35e7eeSXin LI /// compressed instruction. This was shown to be enough to keep the number
16*3b35e7eeSXin LI /// of false matches low while improving code size and speed.
17*3b35e7eeSXin LI //
18*3b35e7eeSXin LI //  Authors:    Lasse Collin
19*3b35e7eeSXin LI //              Jia Tan
20*3b35e7eeSXin LI //
21*3b35e7eeSXin LI //  Special thanks:
22*3b35e7eeSXin LI //
23*3b35e7eeSXin LI //    - Chien Wong <m@xv97.com> provided a few early versions of RISC-V
24*3b35e7eeSXin LI //      filter variants along with test files and benchmark results.
25*3b35e7eeSXin LI //
26*3b35e7eeSXin LI //    - Igor Pavlov helped a lot in the filter design, getting it both
27*3b35e7eeSXin LI //      faster and smaller. The implementation here is still independently
28*3b35e7eeSXin LI //      written, not based on LZMA SDK.
29*3b35e7eeSXin LI //
30*3b35e7eeSXin LI ///////////////////////////////////////////////////////////////////////////////
31*3b35e7eeSXin LI 
32*3b35e7eeSXin LI /*
33*3b35e7eeSXin LI 
34*3b35e7eeSXin LI RISC-V filtering
35*3b35e7eeSXin LI ================
36*3b35e7eeSXin LI 
37*3b35e7eeSXin LI     RV32I and RV64I, possibly combined with extensions C, Zfh, F, D,
38*3b35e7eeSXin LI     and Q, are identical enough that the same filter works for both.
39*3b35e7eeSXin LI 
40*3b35e7eeSXin LI     The instruction encoding is always little endian, even on systems
41*3b35e7eeSXin LI     with big endian data access. Thus the same filter works for both
42*3b35e7eeSXin LI     endiannesses.
43*3b35e7eeSXin LI 
44*3b35e7eeSXin LI     The following instructions have program counter relative
45*3b35e7eeSXin LI     (pc-relative) behavior:
46*3b35e7eeSXin LI 
47*3b35e7eeSXin LI JAL
48*3b35e7eeSXin LI ---
49*3b35e7eeSXin LI 
50*3b35e7eeSXin LI     JAL is used for function calls (including tail calls) and
51*3b35e7eeSXin LI     unconditional jumps within functions. Jumps within functions
52*3b35e7eeSXin LI     aren't useful to filter because the absolute addresses often
53*3b35e7eeSXin LI     appear only once or at most a few times. Tail calls and jumps
54*3b35e7eeSXin LI     within functions look the same to a simple filter so neither
55*3b35e7eeSXin LI     are filtered, that is, JAL x0 is ignored (the ABI name of the
56*3b35e7eeSXin LI     register x0 is "zero").
57*3b35e7eeSXin LI 
58*3b35e7eeSXin LI     Almost all calls store the return address to register x1 (ra)
59*3b35e7eeSXin LI     or x5 (t0). To reduce false matches when the filter is applied
60*3b35e7eeSXin LI     to non-code data, only the JAL instructions that use x1 or x5
61*3b35e7eeSXin LI     are converted. JAL has pc-relative range of +/-1 MiB so longer
62*3b35e7eeSXin LI     calls and jumps need another method (AUIPC+JALR).
63*3b35e7eeSXin LI 
64*3b35e7eeSXin LI C.J and C.JAL
65*3b35e7eeSXin LI -------------
66*3b35e7eeSXin LI 
67*3b35e7eeSXin LI     C.J and C.JAL have pc-relative range of +/-2 KiB.
68*3b35e7eeSXin LI 
69*3b35e7eeSXin LI     C.J is for tail calls and jumps within functions and isn't
70*3b35e7eeSXin LI     filtered for the reasons mentioned for JAL x0.
71*3b35e7eeSXin LI 
72*3b35e7eeSXin LI     C.JAL is an RV32C-only instruction. Its encoding overlaps with
73*3b35e7eeSXin LI     RV64C-only C.ADDIW which is a common instruction. So if filtering
74*3b35e7eeSXin LI     C.JAL was useful (it wasn't tested) then a separate filter would
75*3b35e7eeSXin LI     be needed for RV32 and RV64. Also, false positives would be a
76*3b35e7eeSXin LI     significant problem when the filter is applied to non-code data
77*3b35e7eeSXin LI     because C.JAL needs only five bits to match. Thus, this filter
78*3b35e7eeSXin LI     doesn't modify C.JAL instructions.
79*3b35e7eeSXin LI 
80*3b35e7eeSXin LI BEQ, BNE, BLT, BGE, BLTU, BGEU, C.BEQZ, and C.BNEZ
81*3b35e7eeSXin LI --------------------------------------------------
82*3b35e7eeSXin LI 
83*3b35e7eeSXin LI     These are conditional branches with pc-relative range
84*3b35e7eeSXin LI     of +/-4 KiB (+/-256 B for C.*). The absolute addresses often
85*3b35e7eeSXin LI     appear only once and very short distances are the most common,
86*3b35e7eeSXin LI     so filtering these instructions would make compression worse.
87*3b35e7eeSXin LI 
88*3b35e7eeSXin LI AUIPC with rd != x0
89*3b35e7eeSXin LI -------------------
90*3b35e7eeSXin LI 
91*3b35e7eeSXin LI     AUIPC is paired with a second instruction (inst2) to do
92*3b35e7eeSXin LI     pc-relative jumps, calls, loads, stores, and for taking
93*3b35e7eeSXin LI     an address of a symbol. AUIPC has a 20-bit immediate and
94*3b35e7eeSXin LI     the possible inst2 choices have a 12-bit immediate.
95*3b35e7eeSXin LI 
96*3b35e7eeSXin LI     AUIPC stores pc + 20-bit signed immediate to a register.
97*3b35e7eeSXin LI     The immediate encodes a multiple of 4 KiB so AUIPC itself
98*3b35e7eeSXin LI     has a pc-relative range of +/-2 GiB. AUIPC does *NOT* set
99*3b35e7eeSXin LI     the lowest 12 bits of the result to zero! This means that
100*3b35e7eeSXin LI     the 12-bit immediate in inst2 cannot just include the lowest
101*3b35e7eeSXin LI     12 bits of the absolute address as is; the immediate has to
102*3b35e7eeSXin LI     compensate for the lowest 12 bits that AUIPC copies from the
103*3b35e7eeSXin LI     program counter. This means that a good filter has to convert
104*3b35e7eeSXin LI     not only AUIPC but also the paired inst2.
105*3b35e7eeSXin LI 
106*3b35e7eeSXin LI     A strict filter would focus on filtering the following
107*3b35e7eeSXin LI     AUIPC+inst2 pairs:
108*3b35e7eeSXin LI 
109*3b35e7eeSXin LI       - AUIPC+JALR: Function calls, including tail calls.
110*3b35e7eeSXin LI 
111*3b35e7eeSXin LI       - AUIPC+ADDI: Calculating the address of a function
112*3b35e7eeSXin LI         or a global variable.
113*3b35e7eeSXin LI 
114*3b35e7eeSXin LI       - AUIPC+load/store from the base instruction sets
115*3b35e7eeSXin LI         (RV32I, RV64I) or from the floating point extensions
116*3b35e7eeSXin LI         Zfh, F, D, and Q:
117*3b35e7eeSXin LI           * RV32I: LB, LH, LW, LBU, LHU, SB, SH, SW
118*3b35e7eeSXin LI           * RV64I has also: LD, LWU, SD
119*3b35e7eeSXin LI           * Zfh: FLH, FSH
120*3b35e7eeSXin LI           * F: FLW, FSW
121*3b35e7eeSXin LI           * D: FLD, FSD
122*3b35e7eeSXin LI           * Q: FLQ, FSQ
123*3b35e7eeSXin LI 
124*3b35e7eeSXin LI     NOTE: AUIPC+inst2 can only be a pair if AUIPC's rd specifies
125*3b35e7eeSXin LI     the same register as inst2's rs1.
126*3b35e7eeSXin LI 
127*3b35e7eeSXin LI     Instead of strictly accepting only the above instructions as inst2,
128*3b35e7eeSXin LI     this filter uses a much simpler condition: the lowest two bits of
129*3b35e7eeSXin LI     inst2 must be set, that is, inst2 must not be a 16-bit compressed
130*3b35e7eeSXin LI     instruction. So this will accept all 32-bit and possible future
131*3b35e7eeSXin LI     extended instructions as a pair to AUIPC if the bits in AUIPC's
132*3b35e7eeSXin LI     rd [11:7] match the bits [19:15] in inst2 (the bits that I-type and
133*3b35e7eeSXin LI     S-type instructions use for rs1). Testing showed that this relaxed
134*3b35e7eeSXin LI     condition for inst2 did not consistently or significantly affect
135*3b35e7eeSXin LI     compression ratio but it reduced code size and improved speed.
136*3b35e7eeSXin LI 
137*3b35e7eeSXin LI     Additionally, the paired instruction is always treated as an I-type
138*3b35e7eeSXin LI     instruction. The S-type instructions used by stores (SB, SH, SW,
139*3b35e7eeSXin LI     etc.) place the lowest 5 bits of the immediate in a different
140*3b35e7eeSXin LI     location than I-type instructions. AUIPC+store pairs are less
141*3b35e7eeSXin LI     common than other pairs, and testing showed that the extra
142*3b35e7eeSXin LI     code required to handle S-type instructions was not worth the
143*3b35e7eeSXin LI     compression ratio gained.
144*3b35e7eeSXin LI 
145*3b35e7eeSXin LI     AUIPC+inst2 don't necessarily appear sequentially next to each
146*3b35e7eeSXin LI     other although very often they do. Especially AUIPC+JALR are
147*3b35e7eeSXin LI     sequential as that may allow instruction fusion in processors
148*3b35e7eeSXin LI     (and perhaps help branch prediction as a fused AUIPC+JALR is
149*3b35e7eeSXin LI     a direct branch while JALR alone is an indirect branch).
150*3b35e7eeSXin LI 
151*3b35e7eeSXin LI     Clang 16 can generate code where AUIPC+inst2 is split:
152*3b35e7eeSXin LI 
153*3b35e7eeSXin LI       - AUIPC is outside a loop and inst2 (load/store) is inside
154*3b35e7eeSXin LI         the loop. This way the AUIPC instruction needs to be
155*3b35e7eeSXin LI         executed only once.
156*3b35e7eeSXin LI 
157*3b35e7eeSXin LI       - Load-modify-store may have AUIPC for the load and the same
158*3b35e7eeSXin LI         AUIPC-result is used for the store too. This may get combined
159*3b35e7eeSXin LI         with AUIPC being outside the loop.
160*3b35e7eeSXin LI 
161*3b35e7eeSXin LI       - AUIPC is before a conditional branch and inst2 is hundreds
162*3b35e7eeSXin LI         of bytes away at the branch target.
163*3b35e7eeSXin LI 
164*3b35e7eeSXin LI       - Inner and outer pair:
165*3b35e7eeSXin LI 
166*3b35e7eeSXin LI             auipc   a1,0x2f
167*3b35e7eeSXin LI             auipc   a2,0x3d
168*3b35e7eeSXin LI             ld      a2,-500(a2)
169*3b35e7eeSXin LI             addi    a1,a1,-233
170*3b35e7eeSXin LI 
171*3b35e7eeSXin LI       - Many split pairs with an untaken conditional branch between:
172*3b35e7eeSXin LI 
173*3b35e7eeSXin LI             auipc   s9,0x1613   # Pair 1
174*3b35e7eeSXin LI             auipc   s4,0x1613   # Pair 2
175*3b35e7eeSXin LI             auipc   s6,0x1613   # Pair 3
176*3b35e7eeSXin LI             auipc   s10,0x1613  # Pair 4
177*3b35e7eeSXin LI             beqz    a5,a3baae
178*3b35e7eeSXin LI             ld      a0,0(a6)
179*3b35e7eeSXin LI             ld      a6,246(s9)  # Pair 1
180*3b35e7eeSXin LI             ld      a1,250(s4)  # Pair 2
181*3b35e7eeSXin LI             ld      a3,254(s6)  # Pair 3
182*3b35e7eeSXin LI             ld      a4,258(s10) # Pair 4
183*3b35e7eeSXin LI 
184*3b35e7eeSXin LI     It's not possible to find all split pairs in a filter like this.
185*3b35e7eeSXin LI     At least in 2024, simple sequential pairs are 99 % of AUIPC uses
186*3b35e7eeSXin LI     so filtering only such pairs gives good results and makes the
187*3b35e7eeSXin LI     filter simpler. However, it's possible that future compilers will
188*3b35e7eeSXin LI     produce different code where sequential pairs aren't as common.
189*3b35e7eeSXin LI 
190*3b35e7eeSXin LI     This filter doesn't convert AUIPC instructions alone because:
191*3b35e7eeSXin LI 
192*3b35e7eeSXin LI     (1) The conversion would be off-by-one (or off-by-4096) half the
193*3b35e7eeSXin LI         time because the lowest 12 bits from inst2 (inst2_imm12)
194*3b35e7eeSXin LI         aren't known. We only know that the absolute address is
195*3b35e7eeSXin LI         pc + AUIPC_imm20 + [-2048, +2047] but there is no way to
196*3b35e7eeSXin LI         know the exact 4096-byte multiple (or 4096 * n + 2048):
197*3b35e7eeSXin LI         there are always two possibilities because AUIPC copies
198*3b35e7eeSXin LI         the 12 lowest bits from pc instead of zeroing them.
199*3b35e7eeSXin LI 
200*3b35e7eeSXin LI         NOTE: The sign-extension of inst2_imm12 adds a tiny bit
201*3b35e7eeSXin LI         of extra complexity to AUIPC math in general but it's not
202*3b35e7eeSXin LI         the reason for this problem. The sign-extension only changes
203*3b35e7eeSXin LI         the relative position of the pc-relative 4096-byte window.
204*3b35e7eeSXin LI 
205*3b35e7eeSXin LI     (2) Matching AUIPC instruction alone requires only seven bits.
206*3b35e7eeSXin LI         When the filter is applied to non-code data, that leads
207*3b35e7eeSXin LI         to many false positives which make compression worse.
208*3b35e7eeSXin LI         As long as most AUIPC+inst2 pairs appear as two consecutive
209*3b35e7eeSXin LI         instructions, converting only such pairs gives better results.
210*3b35e7eeSXin LI 
211*3b35e7eeSXin LI     In assembly, AUIPC+inst2 tend to look like this:
212*3b35e7eeSXin LI 
213*3b35e7eeSXin LI         # Call:
214*3b35e7eeSXin LI         auipc   ra, 0x12345
215*3b35e7eeSXin LI         jalr    ra, -42(ra)
216*3b35e7eeSXin LI 
217*3b35e7eeSXin LI         # Tail call:
218*3b35e7eeSXin LI         auipc   t1, 0x12345
219*3b35e7eeSXin LI         jalr    zero, -42(t1)
220*3b35e7eeSXin LI 
221*3b35e7eeSXin LI         # Getting the absolute address:
222*3b35e7eeSXin LI         auipc   a0, 0x12345
223*3b35e7eeSXin LI         addi    a0, a0, -42
224*3b35e7eeSXin LI 
225*3b35e7eeSXin LI         # rd of inst2 isn't necessarily the same as rs1 even
226*3b35e7eeSXin LI         # in cases where there is no reason to preserve rs1.
227*3b35e7eeSXin LI         auipc   a0, 0x12345
228*3b35e7eeSXin LI         addi    a1, a0, -42
229*3b35e7eeSXin LI 
230*3b35e7eeSXin LI     As of 2024, 16-bit instructions from the C extension don't
231*3b35e7eeSXin LI     appear as inst2. The RISC-V psABI doesn't list AUIPC+C.* as
232*3b35e7eeSXin LI     a linker relaxation type explicitly but it's not disallowed
233*3b35e7eeSXin LI     either. Usefulness is limited as most of the time the lowest
234*3b35e7eeSXin LI     12 bits won't fit in a C instruction. This filter doesn't
235*3b35e7eeSXin LI     support AUIPC+C.* combinations because this makes the filter
236*3b35e7eeSXin LI     simpler, there are no test files, and it hopefully will never
237*3b35e7eeSXin LI     be needed anyway.
238*3b35e7eeSXin LI 
239*3b35e7eeSXin LI     (Compare AUIPC to ARM64 where ADRP does set the lowest 12 bits
240*3b35e7eeSXin LI     to zero. The paired instruction has the lowest 12 bits of the
241*3b35e7eeSXin LI     absolute address as is in a zero-extended immediate. Thus the
242*3b35e7eeSXin LI     ARM64 filter doesn't need to care about the instructions that
243*3b35e7eeSXin LI     are paired with ADRP. An off-by-4096 issue can still occur if
244*3b35e7eeSXin LI     the code section isn't aligned with the filter's start offset.
245*3b35e7eeSXin LI     It's not a problem with standalone ELF files but Windows PE
246*3b35e7eeSXin LI     files need start_offset=3072 for best results. Also, a .tar
247*3b35e7eeSXin LI     stores files with 512-byte alignment so most of the time it
248*3b35e7eeSXin LI     won't be the best for ARM64.)
249*3b35e7eeSXin LI 
250*3b35e7eeSXin LI AUIPC with rd == x0
251*3b35e7eeSXin LI -------------------
252*3b35e7eeSXin LI 
253*3b35e7eeSXin LI     AUIPC instructions with rd=x0 are reserved for HINTs in the base
254*3b35e7eeSXin LI     instruction set. Such AUIPC instructions are never filtered.
255*3b35e7eeSXin LI 
256*3b35e7eeSXin LI     As of January 2024, it seems likely that AUIPC with rd=x0 will
257*3b35e7eeSXin LI     be used for landing pads (pseudoinstruction LPAD). LPAD is used
258*3b35e7eeSXin LI     to mark valid targets for indirect jumps (for JALR), for example,
259*3b35e7eeSXin LI     beginnings of functions. The 20-bit immediate in LPAD instruction
260*3b35e7eeSXin LI     is a label, not a pc-relative address. Thus it would be
261*3b35e7eeSXin LI     counterproductive to convert AUIPC instructions with rd=x0.
262*3b35e7eeSXin LI 
263*3b35e7eeSXin LI     Often the next instruction after LPAD won't have rs1=x0 and thus
264*3b35e7eeSXin LI     the filtering would be skipped for that reason alone. However,
265*3b35e7eeSXin LI     it's not good to rely on this. For example, consider a function
266*3b35e7eeSXin LI     that begins like this:
267*3b35e7eeSXin LI 
268*3b35e7eeSXin LI         int foo(int i)
269*3b35e7eeSXin LI         {
270*3b35e7eeSXin LI             if (i <= 234) {
271*3b35e7eeSXin LI                 ...
272*3b35e7eeSXin LI             }
273*3b35e7eeSXin LI 
274*3b35e7eeSXin LI     A compiler may generate something like this:
275*3b35e7eeSXin LI 
276*3b35e7eeSXin LI         lpad    0x54321
277*3b35e7eeSXin LI         li      a5, 234
278*3b35e7eeSXin LI         bgt     a0, a5, .L2
279*3b35e7eeSXin LI 
280*3b35e7eeSXin LI     Converting the pseudoinstructions to raw instructions:
281*3b35e7eeSXin LI 
282*3b35e7eeSXin LI         auipc   x0, 0x54321
283*3b35e7eeSXin LI         addi    x15, x0, 234
284*3b35e7eeSXin LI         blt     x15, x10, .L2
285*3b35e7eeSXin LI 
286*3b35e7eeSXin LI     In this case the filter would undesirably convert the AUIPC+ADDI
287*3b35e7eeSXin LI     pair if the filter didn't explicitly skip AUIPC instructions
288*3b35e7eeSXin LI     that have rd=x0.
289*3b35e7eeSXin LI 
290*3b35e7eeSXin LI */
291*3b35e7eeSXin LI 
292*3b35e7eeSXin LI 
293*3b35e7eeSXin LI #include "simple_private.h"
294*3b35e7eeSXin LI 
295*3b35e7eeSXin LI 
296*3b35e7eeSXin LI // This checks two conditions at once:
297*3b35e7eeSXin LI //    - AUIPC rd == inst2 rs1.
298*3b35e7eeSXin LI //    - inst2 opcode has the lowest two bits set.
299*3b35e7eeSXin LI //
300*3b35e7eeSXin LI // The 8 bit left shift aligns the rd of AUIPC with the rs1 of inst2.
301*3b35e7eeSXin LI // By XORing the registers, any non-zero value in those bits indicates the
302*3b35e7eeSXin LI // registers are not equal and thus not an AUIPC pair. Subtracting 3 from
303*3b35e7eeSXin LI // inst2 will zero out the first two opcode bits only when they are set.
304*3b35e7eeSXin LI // The mask tests if any of the register or opcode bits are set (and thus
305*3b35e7eeSXin LI // not an AUIPC pair).
306*3b35e7eeSXin LI //
307*3b35e7eeSXin LI // Alternative expression: (((((auipc) << 8) ^ (inst2)) & 0xF8003) != 3)
308*3b35e7eeSXin LI #define NOT_AUIPC_PAIR(auipc, inst2) \
309*3b35e7eeSXin LI 	((((auipc) << 8) ^ ((inst2) - 3)) & 0xF8003)
310*3b35e7eeSXin LI 
311*3b35e7eeSXin LI // This macro checks multiple conditions:
312*3b35e7eeSXin LI //   (1) AUIPC rd [11:7] == x2 (special rd value).
313*3b35e7eeSXin LI //   (2) AUIPC bits 12 and 13 set (the lowest two opcode bits of packed inst2).
314*3b35e7eeSXin LI //   (3) inst2_rs1 doesn't equal x0 or x2 because the opposite
315*3b35e7eeSXin LI //       conversion is only done when
316*3b35e7eeSXin LI //       auipc_rd != x0 &&
317*3b35e7eeSXin LI //       auipc_rd != x2 &&
318*3b35e7eeSXin LI //       auipc_rd == inst2_rs1.
319*3b35e7eeSXin LI //
320*3b35e7eeSXin LI // The left-hand side takes care of (1) and (2).
321*3b35e7eeSXin LI //   (a) The lowest 7 bits are already known to be AUIPC so subtracting 0x17
322*3b35e7eeSXin LI //       makes those bits zeros.
323*3b35e7eeSXin LI //   (b) If AUIPC rd equals x2, subtracting 0x100 makes bits [11:7] zeros.
324*3b35e7eeSXin LI //       If rd doesn't equal x2, then there will be at least one non-zero bit
325*3b35e7eeSXin LI //       and the next step (c) is irrelevant.
326*3b35e7eeSXin LI //   (c) If the lowest two opcode bits of the packed inst2 are set in [13:12],
327*3b35e7eeSXin LI //       then subtracting 0x3000 will make those bits zeros. Otherwise there
328*3b35e7eeSXin LI //       will be at least one non-zero bit.
329*3b35e7eeSXin LI //
330*3b35e7eeSXin LI // The shift by 18 removes the high bits from the final '>=' comparison and
331*3b35e7eeSXin LI // ensures that any non-zero result will be larger than any possible result
332*3b35e7eeSXin LI // from the right-hand side of the comparison. The cast ensures that the
333*3b35e7eeSXin LI // left-hand side didn't get promoted to a larger type than uint32_t.
334*3b35e7eeSXin LI //
335*3b35e7eeSXin LI // On the right-hand side, inst2_rs1 & 0x1D will be non-zero as long as
336*3b35e7eeSXin LI // inst2_rs1 is not x0 or x2.
337*3b35e7eeSXin LI //
338*3b35e7eeSXin LI // The final '>=' comparison will make the expression true if:
339*3b35e7eeSXin LI //   - The subtraction caused any bits to be set (special AUIPC rd value not
340*3b35e7eeSXin LI //     used or inst2 opcode bits not set). (non-zero >= non-zero or 0)
341*3b35e7eeSXin LI //   - The subtraction did not cause any bits to be set but inst2_rs1 was
342*3b35e7eeSXin LI //     x0 or x2. (0 >= 0)
343*3b35e7eeSXin LI #define NOT_SPECIAL_AUIPC(auipc, inst2_rs1) \
344*3b35e7eeSXin LI 	((uint32_t)(((auipc) - 0x3117) << 18) >= ((inst2_rs1) & 0x1D))
345*3b35e7eeSXin LI 
346*3b35e7eeSXin LI 
347*3b35e7eeSXin LI // The encode and decode functions are split for this filter because of the
348*3b35e7eeSXin LI // AUIPC+inst2 filtering. This filter design allows a decoder-only
349*3b35e7eeSXin LI // implementation to be smaller than alternative designs.
350*3b35e7eeSXin LI 
351*3b35e7eeSXin LI #ifdef HAVE_ENCODER_RISCV
352*3b35e7eeSXin LI static size_t
riscv_encode(void * simple lzma_attribute ((__unused__)),uint32_t now_pos,bool is_encoder lzma_attribute ((__unused__)),uint8_t * buffer,size_t size)353*3b35e7eeSXin LI riscv_encode(void *simple lzma_attribute((__unused__)),
354*3b35e7eeSXin LI 		uint32_t now_pos,
355*3b35e7eeSXin LI 		bool is_encoder lzma_attribute((__unused__)),
356*3b35e7eeSXin LI 		uint8_t *buffer, size_t size)
357*3b35e7eeSXin LI {
358*3b35e7eeSXin LI 	// Avoid using i + 8 <= size in the loop condition.
359*3b35e7eeSXin LI 	//
360*3b35e7eeSXin LI 	// NOTE: If there is a JAL in the last six bytes of the stream, it
361*3b35e7eeSXin LI 	// won't be converted. This is intentional to keep the code simpler.
362*3b35e7eeSXin LI 	if (size < 8)
363*3b35e7eeSXin LI 		return 0;
364*3b35e7eeSXin LI 
365*3b35e7eeSXin LI 	size -= 8;
366*3b35e7eeSXin LI 
367*3b35e7eeSXin LI 	size_t i;
368*3b35e7eeSXin LI 
369*3b35e7eeSXin LI 	// The loop is advanced by 2 bytes every iteration since the
370*3b35e7eeSXin LI 	// instruction stream may include 16-bit instructions (C extension).
371*3b35e7eeSXin LI 	for (i = 0; i <= size; i += 2) {
372*3b35e7eeSXin LI 		uint32_t inst = buffer[i];
373*3b35e7eeSXin LI 
374*3b35e7eeSXin LI 		if (inst == 0xEF) {
375*3b35e7eeSXin LI 			// JAL
376*3b35e7eeSXin LI 			const uint32_t b1 = buffer[i + 1];
377*3b35e7eeSXin LI 
378*3b35e7eeSXin LI 			// Only filter rd=x1(ra) and rd=x5(t0).
379*3b35e7eeSXin LI 			if ((b1 & 0x0D) != 0)
380*3b35e7eeSXin LI 				continue;
381*3b35e7eeSXin LI 
382*3b35e7eeSXin LI 			// The 20-bit immediate is in four pieces.
383*3b35e7eeSXin LI 			// The encoder stores it in big endian form
384*3b35e7eeSXin LI 			// since it improves compression slightly.
385*3b35e7eeSXin LI 			const uint32_t b2 = buffer[i + 2];
386*3b35e7eeSXin LI 			const uint32_t b3 = buffer[i + 3];
387*3b35e7eeSXin LI 			const uint32_t pc = now_pos + (uint32_t)i;
388*3b35e7eeSXin LI 
389*3b35e7eeSXin LI // The following chart shows the highest three bytes of JAL, focusing on
390*3b35e7eeSXin LI // the 20-bit immediate field [31:12]. The first row of numbers is the
391*3b35e7eeSXin LI // bit position in a 32-bit little endian instruction. The second row of
392*3b35e7eeSXin LI // numbers shows the order of the immediate field in a J-type instruction.
393*3b35e7eeSXin LI // The last row is the bit number in each byte.
394*3b35e7eeSXin LI //
395*3b35e7eeSXin LI // To determine the amount to shift each bit, subtract the value in
396*3b35e7eeSXin LI // the last row from the value in the second last row. If the number
397*3b35e7eeSXin LI // is positive, shift left. If negative, shift right.
398*3b35e7eeSXin LI //
399*3b35e7eeSXin LI // For example, at the rightmost side of the chart, the bit 4 in b1 is
400*3b35e7eeSXin LI // the bit 12 of the address. Thus that bit needs to be shifted left
401*3b35e7eeSXin LI // by 12 - 4 = 8 bits to put it in the right place in the addr variable.
402*3b35e7eeSXin LI //
403*3b35e7eeSXin LI // NOTE: The immediate of a J-type instruction holds bits [20:1] of
404*3b35e7eeSXin LI // the address. The bit [0] is always 0 and not part of the immediate.
405*3b35e7eeSXin LI //
406*3b35e7eeSXin LI // |          b3             |          b2             |          b1         |
407*3b35e7eeSXin LI // | 31 30 29 28 27 26 25 24 | 23 22 21 20 19 18 17 16 | 15 14 13 12 x x x x |
408*3b35e7eeSXin LI // | 20 10  9  8  7  6  5  4 |  3  2  1 11 19 18 17 16 | 15 14 13 12 x x x x |
409*3b35e7eeSXin LI // |  7  6  5  4  3  2  1  0 |  7  6  5  4  3  2  1  0 |  7  6  5  4 x x x x |
410*3b35e7eeSXin LI 
411*3b35e7eeSXin LI 			uint32_t addr = ((b1 & 0xF0) << 8)
412*3b35e7eeSXin LI 					| ((b2 & 0x0F) << 16)
413*3b35e7eeSXin LI 					| ((b2 & 0x10) << 7)
414*3b35e7eeSXin LI 					| ((b2 & 0xE0) >> 4)
415*3b35e7eeSXin LI 					| ((b3 & 0x7F) << 4)
416*3b35e7eeSXin LI 					| ((b3 & 0x80) << 13);
417*3b35e7eeSXin LI 
418*3b35e7eeSXin LI 			addr += pc;
419*3b35e7eeSXin LI 
420*3b35e7eeSXin LI 			buffer[i + 1] = (uint8_t)((b1 & 0x0F)
421*3b35e7eeSXin LI 					| ((addr >> 13) & 0xF0));
422*3b35e7eeSXin LI 
423*3b35e7eeSXin LI 			buffer[i + 2] = (uint8_t)(addr >> 9);
424*3b35e7eeSXin LI 			buffer[i + 3] = (uint8_t)(addr >> 1);
425*3b35e7eeSXin LI 
426*3b35e7eeSXin LI 			// The "-2" is included because the for-loop will
427*3b35e7eeSXin LI 			// always increment by 2. In this case, we want to
428*3b35e7eeSXin LI 			// skip an extra 2 bytes since we used 4 bytes
429*3b35e7eeSXin LI 			// of input.
430*3b35e7eeSXin LI 			i += 4 - 2;
431*3b35e7eeSXin LI 
432*3b35e7eeSXin LI 		} else if ((inst & 0x7F) == 0x17) {
433*3b35e7eeSXin LI 			// AUIPC
434*3b35e7eeSXin LI 			inst |= (uint32_t)buffer[i + 1] << 8;
435*3b35e7eeSXin LI 			inst |= (uint32_t)buffer[i + 2] << 16;
436*3b35e7eeSXin LI 			inst |= (uint32_t)buffer[i + 3] << 24;
437*3b35e7eeSXin LI 
438*3b35e7eeSXin LI 			// Branch based on AUIPC's rd. The bitmask test does
439*3b35e7eeSXin LI 			// the same thing as this:
440*3b35e7eeSXin LI 			//
441*3b35e7eeSXin LI 			//     const uint32_t auipc_rd = (inst >> 7) & 0x1F;
442*3b35e7eeSXin LI 			//     if (auipc_rd != 0 && auipc_rd != 2) {
443*3b35e7eeSXin LI  			if (inst & 0xE80) {
444*3b35e7eeSXin LI 				// AUIPC's rd doesn't equal x0 or x2.
445*3b35e7eeSXin LI 
446*3b35e7eeSXin LI 				// Check if AUIPC+inst2 are a pair.
447*3b35e7eeSXin LI 				uint32_t inst2 = read32le(buffer + i + 4);
448*3b35e7eeSXin LI 
449*3b35e7eeSXin LI 				if (NOT_AUIPC_PAIR(inst, inst2)) {
450*3b35e7eeSXin LI 					// The NOT_AUIPC_PAIR macro allows
451*3b35e7eeSXin LI 					// a false AUIPC+AUIPC pair if the
452*3b35e7eeSXin LI 					// bits [19:15] (where rs1 would be)
453*3b35e7eeSXin LI 					// in the second AUIPC match the rd
454*3b35e7eeSXin LI 					// of the first AUIPC.
455*3b35e7eeSXin LI 					//
456*3b35e7eeSXin LI 					// We must skip enough forward so
457*3b35e7eeSXin LI 					// that the first two bytes of the
458*3b35e7eeSXin LI 					// second AUIPC cannot get converted.
459*3b35e7eeSXin LI 					// Such a conversion could make the
460*3b35e7eeSXin LI 					// current pair become a valid pair
461*3b35e7eeSXin LI 					// which would desync the decoder.
462*3b35e7eeSXin LI 					//
463*3b35e7eeSXin LI 					// Skipping six bytes is enough even
464*3b35e7eeSXin LI 					// though the above condition looks
465*3b35e7eeSXin LI 					// at the lowest four bits of the
466*3b35e7eeSXin LI 					// buffer[i + 6] too. This is safe
467*3b35e7eeSXin LI 					// because this filter never changes
468*3b35e7eeSXin LI 					// those bits if a conversion at
469*3b35e7eeSXin LI 					// that position is done.
470*3b35e7eeSXin LI 					i += 6 - 2;
471*3b35e7eeSXin LI 					continue;
472*3b35e7eeSXin LI 				}
473*3b35e7eeSXin LI 
474*3b35e7eeSXin LI 				// Convert AUIPC+inst2 to a special format:
475*3b35e7eeSXin LI 				//
476*3b35e7eeSXin LI 				//   - The lowest 7 bits [6:0] retain the
477*3b35e7eeSXin LI 				//     AUIPC opcode.
478*3b35e7eeSXin LI 				//
479*3b35e7eeSXin LI 				//   - The rd [11:7] is set to x2(sp). x2 is
480*3b35e7eeSXin LI 				//     used as the stack pointer so AUIPC with
481*3b35e7eeSXin LI 				//     rd=x2 should be very rare in real-world
482*3b35e7eeSXin LI 				//     executables.
483*3b35e7eeSXin LI 				//
484*3b35e7eeSXin LI 				//   - The remaining 20 bits [31:12] (that
485*3b35e7eeSXin LI 				//     normally hold the pc-relative immediate)
486*3b35e7eeSXin LI 				//     are used to store the lowest 20 bits of
487*3b35e7eeSXin LI 				//     inst2. That is, the 12-bit immediate of
488*3b35e7eeSXin LI 				//     inst2 is not included.
489*3b35e7eeSXin LI 				//
490*3b35e7eeSXin LI 				//   - The location of the original inst2 is
491*3b35e7eeSXin LI 				//     used to store the 32-bit absolute
492*3b35e7eeSXin LI 				//     address in big endian format. Compared
493*3b35e7eeSXin LI 				//     to the 20+12-bit split encoding, this
494*3b35e7eeSXin LI 				//     results in a longer uninterrupted
495*3b35e7eeSXin LI 				//     sequence of identical common bytes
496*3b35e7eeSXin LI 				//     when the same address is referred
497*3b35e7eeSXin LI 				//     with different instruction pairs
498*3b35e7eeSXin LI 				//     (like AUIPC+LD vs. AUIPC+ADDI) or
499*3b35e7eeSXin LI 				//     when the occurrences of the same
500*3b35e7eeSXin LI 				//     pair use different registers. When
501*3b35e7eeSXin LI 				//     referring to adjacent memory locations
502*3b35e7eeSXin LI 				//     (like function calls that go via the
503*3b35e7eeSXin LI 				//     ELF PLT), in big endian order only the
504*3b35e7eeSXin LI 				//     last 1-2 bytes differ; in little endian
505*3b35e7eeSXin LI 				//     the differing 1-2 bytes would be in the
506*3b35e7eeSXin LI 				//     middle of the 8-byte sequence.
507*3b35e7eeSXin LI 				//
508*3b35e7eeSXin LI 				// When reversing the transformation, the
509*3b35e7eeSXin LI 				// original rd of AUIPC can be restored
510*3b35e7eeSXin LI 				// from inst2's rs1 as they are required to
511*3b35e7eeSXin LI 				// be the same.
512*3b35e7eeSXin LI 
513*3b35e7eeSXin LI 				// Arithmetic right shift makes sign extension
514*3b35e7eeSXin LI 				// trivial but (1) it's implementation-defined
515*3b35e7eeSXin LI 				// behavior (C99/C11/C23 6.5.7-p5) and so is
516*3b35e7eeSXin LI 				// (2) casting unsigned to signed (6.3.1.3-p3).
517*3b35e7eeSXin LI 				//
518*3b35e7eeSXin LI 				// One can check for (1) with
519*3b35e7eeSXin LI 				//
520*3b35e7eeSXin LI 				//     if ((-1 >> 1) == -1) ...
521*3b35e7eeSXin LI 				//
522*3b35e7eeSXin LI 				// but (2) has to be checked from the
523*3b35e7eeSXin LI 				// compiler docs. GCC promises that (1)
524*3b35e7eeSXin LI 				// and (2) behave in the common expected
525*3b35e7eeSXin LI 				// way and thus
526*3b35e7eeSXin LI 				//
527*3b35e7eeSXin LI 				//     addr += (uint32_t)(
528*3b35e7eeSXin LI 				//             (int32_t)inst2 >> 20);
529*3b35e7eeSXin LI 				//
530*3b35e7eeSXin LI 				// does the same as the code below. But since
531*3b35e7eeSXin LI 				// the 100 % portable way is only a few bytes
532*3b35e7eeSXin LI 				// bigger code and there is no real speed
533*3b35e7eeSXin LI 				// difference, let's just use that, especially
534*3b35e7eeSXin LI 				// since the decoder doesn't need this at all.
535*3b35e7eeSXin LI 				uint32_t addr = inst & 0xFFFFF000;
536*3b35e7eeSXin LI 				addr += (inst2 >> 20)
537*3b35e7eeSXin LI 						- ((inst2 >> 19) & 0x1000);
538*3b35e7eeSXin LI 
539*3b35e7eeSXin LI 				addr += now_pos + (uint32_t)i;
540*3b35e7eeSXin LI 
541*3b35e7eeSXin LI 				// Construct the first 32 bits:
542*3b35e7eeSXin LI 				//   [6:0]    AUIPC opcode
543*3b35e7eeSXin LI 				//   [11:7]   Special AUIPC rd = x2
544*3b35e7eeSXin LI 				//   [31:12]  The lowest 20 bits of inst2
545*3b35e7eeSXin LI 				inst = 0x17 | (2 << 7) | (inst2 << 12);
546*3b35e7eeSXin LI 
547*3b35e7eeSXin LI 				write32le(buffer + i, inst);
548*3b35e7eeSXin LI 
549*3b35e7eeSXin LI 				// The second 32 bits store the absolute
550*3b35e7eeSXin LI 				// address in big endian order.
551*3b35e7eeSXin LI 				write32be(buffer + i + 4, addr);
552*3b35e7eeSXin LI 			} else {
553*3b35e7eeSXin LI 				// AUIPC's rd equals x0 or x2.
554*3b35e7eeSXin LI 				//
555*3b35e7eeSXin LI 				// x0 indicates a landing pad (LPAD).
556*3b35e7eeSXin LI 				// It's always skipped.
557*3b35e7eeSXin LI 				//
558*3b35e7eeSXin LI 				// AUIPC with rd == x2 is used for the special
559*3b35e7eeSXin LI 				// format as explained above. When the input
560*3b35e7eeSXin LI 				// contains a byte sequence that matches the
561*3b35e7eeSXin LI 				// special format, "fake" decoding must be
562*3b35e7eeSXin LI 				// done to keep the filter bijective (that
563*3b35e7eeSXin LI 				// is, safe to apply on arbitrary data).
564*3b35e7eeSXin LI 				//
565*3b35e7eeSXin LI 				// See the "x0 or x2" section in riscv_decode()
566*3b35e7eeSXin LI 				// for how the "real" decoding is done. The
567*3b35e7eeSXin LI 				// "fake" decoding is a simplified version
568*3b35e7eeSXin LI 				// of "real" decoding with the following
569*3b35e7eeSXin LI 				// differences (these reduce code size of
570*3b35e7eeSXin LI 				// the decoder):
571*3b35e7eeSXin LI 				// (1) The lowest 12 bits aren't sign-extended.
572*3b35e7eeSXin LI 				// (2) No address conversion is done.
573*3b35e7eeSXin LI 				// (3) Big endian format isn't used (the fake
574*3b35e7eeSXin LI 				//     address is in little endian order).
575*3b35e7eeSXin LI 
576*3b35e7eeSXin LI 				// Check if inst matches the special format.
577*3b35e7eeSXin LI 				const uint32_t fake_rs1 = inst >> 27;
578*3b35e7eeSXin LI 
579*3b35e7eeSXin LI 				if (NOT_SPECIAL_AUIPC(inst, fake_rs1)) {
580*3b35e7eeSXin LI 					i += 4 - 2;
581*3b35e7eeSXin LI 					continue;
582*3b35e7eeSXin LI 				}
583*3b35e7eeSXin LI 
584*3b35e7eeSXin LI 				const uint32_t fake_addr =
585*3b35e7eeSXin LI 						read32le(buffer + i + 4);
586*3b35e7eeSXin LI 
587*3b35e7eeSXin LI 				// Construct the second 32 bits:
588*3b35e7eeSXin LI 				//   [19:0]   Upper 20 bits from AUIPC
589*3b35e7eeSXin LI 				//   [31:20]  The lowest 12 bits of fake_addr
590*3b35e7eeSXin LI 				const uint32_t fake_inst2 = (inst >> 12)
591*3b35e7eeSXin LI 						| (fake_addr << 20);
592*3b35e7eeSXin LI 
593*3b35e7eeSXin LI 				// Construct new first 32 bits from:
594*3b35e7eeSXin LI 				//   [6:0]   AUIPC opcode
595*3b35e7eeSXin LI 				//   [11:7]  Fake AUIPC rd = fake_rs1
596*3b35e7eeSXin LI 				//   [31:12] The highest 20 bits of fake_addr
597*3b35e7eeSXin LI 				inst = 0x17 | (fake_rs1 << 7)
598*3b35e7eeSXin LI 					| (fake_addr & 0xFFFFF000);
599*3b35e7eeSXin LI 
600*3b35e7eeSXin LI 				write32le(buffer + i, inst);
601*3b35e7eeSXin LI 				write32le(buffer + i + 4, fake_inst2);
602*3b35e7eeSXin LI 			}
603*3b35e7eeSXin LI 
604*3b35e7eeSXin LI 			i += 8 - 2;
605*3b35e7eeSXin LI 		}
606*3b35e7eeSXin LI 	}
607*3b35e7eeSXin LI 
608*3b35e7eeSXin LI 	return i;
609*3b35e7eeSXin LI }
610*3b35e7eeSXin LI 
611*3b35e7eeSXin LI 
612*3b35e7eeSXin LI extern lzma_ret
lzma_simple_riscv_encoder_init(lzma_next_coder * next,const lzma_allocator * allocator,const lzma_filter_info * filters)613*3b35e7eeSXin LI lzma_simple_riscv_encoder_init(lzma_next_coder *next,
614*3b35e7eeSXin LI 		const lzma_allocator *allocator,
615*3b35e7eeSXin LI 		const lzma_filter_info *filters)
616*3b35e7eeSXin LI {
617*3b35e7eeSXin LI 	return lzma_simple_coder_init(next, allocator, filters,
618*3b35e7eeSXin LI 			&riscv_encode, 0, 8, 2, true);
619*3b35e7eeSXin LI }
620*3b35e7eeSXin LI #endif
621*3b35e7eeSXin LI 
622*3b35e7eeSXin LI 
623*3b35e7eeSXin LI #ifdef HAVE_DECODER_RISCV
624*3b35e7eeSXin LI static size_t
riscv_decode(void * simple lzma_attribute ((__unused__)),uint32_t now_pos,bool is_encoder lzma_attribute ((__unused__)),uint8_t * buffer,size_t size)625*3b35e7eeSXin LI riscv_decode(void *simple lzma_attribute((__unused__)),
626*3b35e7eeSXin LI 		uint32_t now_pos,
627*3b35e7eeSXin LI 		bool is_encoder lzma_attribute((__unused__)),
628*3b35e7eeSXin LI 		uint8_t *buffer, size_t size)
629*3b35e7eeSXin LI {
630*3b35e7eeSXin LI 	if (size < 8)
631*3b35e7eeSXin LI 		return 0;
632*3b35e7eeSXin LI 
633*3b35e7eeSXin LI 	size -= 8;
634*3b35e7eeSXin LI 
635*3b35e7eeSXin LI 	size_t i;
636*3b35e7eeSXin LI 	for (i = 0; i <= size; i += 2) {
637*3b35e7eeSXin LI 		uint32_t inst = buffer[i];
638*3b35e7eeSXin LI 
639*3b35e7eeSXin LI 		if (inst == 0xEF) {
640*3b35e7eeSXin LI 			// JAL
641*3b35e7eeSXin LI 			const uint32_t b1 = buffer[i + 1];
642*3b35e7eeSXin LI 
643*3b35e7eeSXin LI 			// Only filter rd=x1(ra) and rd=x5(t0).
644*3b35e7eeSXin LI 			if ((b1 & 0x0D) != 0)
645*3b35e7eeSXin LI 				continue;
646*3b35e7eeSXin LI 
647*3b35e7eeSXin LI 			const uint32_t b2 = buffer[i + 2];
648*3b35e7eeSXin LI 			const uint32_t b3 = buffer[i + 3];
649*3b35e7eeSXin LI 			const uint32_t pc = now_pos + (uint32_t)i;
650*3b35e7eeSXin LI 
651*3b35e7eeSXin LI // |          b3             |          b2             |          b1         |
652*3b35e7eeSXin LI // | 31 30 29 28 27 26 25 24 | 23 22 21 20 19 18 17 16 | 15 14 13 12 x x x x |
653*3b35e7eeSXin LI // | 20 10  9  8  7  6  5  4 |  3  2  1 11 19 18 17 16 | 15 14 13 12 x x x x |
654*3b35e7eeSXin LI // |  7  6  5  4  3  2  1  0 |  7  6  5  4  3  2  1  0 |  7  6  5  4 x x x x |
655*3b35e7eeSXin LI 
656*3b35e7eeSXin LI 			uint32_t addr = ((b1 & 0xF0) << 13)
657*3b35e7eeSXin LI 					| (b2 << 9) | (b3 << 1);
658*3b35e7eeSXin LI 
659*3b35e7eeSXin LI 			addr -= pc;
660*3b35e7eeSXin LI 
661*3b35e7eeSXin LI 			buffer[i + 1] = (uint8_t)((b1 & 0x0F)
662*3b35e7eeSXin LI 					| ((addr >> 8) & 0xF0));
663*3b35e7eeSXin LI 
664*3b35e7eeSXin LI 			buffer[i + 2] = (uint8_t)(((addr >> 16) & 0x0F)
665*3b35e7eeSXin LI 					| ((addr >> 7) & 0x10)
666*3b35e7eeSXin LI 					| ((addr << 4) & 0xE0));
667*3b35e7eeSXin LI 
668*3b35e7eeSXin LI 			buffer[i + 3] = (uint8_t)(((addr >> 4) & 0x7F)
669*3b35e7eeSXin LI 					| ((addr >> 13) & 0x80));
670*3b35e7eeSXin LI 
671*3b35e7eeSXin LI 			i += 4 - 2;
672*3b35e7eeSXin LI 
673*3b35e7eeSXin LI 		} else if ((inst & 0x7F) == 0x17) {
674*3b35e7eeSXin LI 			// AUIPC
675*3b35e7eeSXin LI 			uint32_t inst2;
676*3b35e7eeSXin LI 
677*3b35e7eeSXin LI 			inst |= (uint32_t)buffer[i + 1] << 8;
678*3b35e7eeSXin LI 			inst |= (uint32_t)buffer[i + 2] << 16;
679*3b35e7eeSXin LI 			inst |= (uint32_t)buffer[i + 3] << 24;
680*3b35e7eeSXin LI 
681*3b35e7eeSXin LI 			if (inst & 0xE80) {
682*3b35e7eeSXin LI 				// AUIPC's rd doesn't equal x0 or x2.
683*3b35e7eeSXin LI 
684*3b35e7eeSXin LI 				// Check if it is a "fake" AUIPC+inst2 pair.
685*3b35e7eeSXin LI 				inst2 = read32le(buffer + i + 4);
686*3b35e7eeSXin LI 
687*3b35e7eeSXin LI 				if (NOT_AUIPC_PAIR(inst, inst2)) {
688*3b35e7eeSXin LI 					i += 6 - 2;
689*3b35e7eeSXin LI 					continue;
690*3b35e7eeSXin LI 				}
691*3b35e7eeSXin LI 
692*3b35e7eeSXin LI 				// Decode (or more like re-encode) the "fake"
693*3b35e7eeSXin LI 				// pair. The "fake" format doesn't do
694*3b35e7eeSXin LI 				// sign-extension, address conversion, or
695*3b35e7eeSXin LI 				// use big endian. (The use of little endian
696*3b35e7eeSXin LI 				// allows sharing the write32le() calls in
697*3b35e7eeSXin LI 				// the decoder to reduce code size when
698*3b35e7eeSXin LI 				// unaligned access isn't supported.)
699*3b35e7eeSXin LI 				uint32_t addr = inst & 0xFFFFF000;
700*3b35e7eeSXin LI 				addr += inst2 >> 20;
701*3b35e7eeSXin LI 
702*3b35e7eeSXin LI 				inst = 0x17 | (2 << 7) | (inst2 << 12);
703*3b35e7eeSXin LI 				inst2 = addr;
704*3b35e7eeSXin LI 			} else {
705*3b35e7eeSXin LI 				// AUIPC's rd equals x0 or x2.
706*3b35e7eeSXin LI 
707*3b35e7eeSXin LI 				// Check if inst matches the special format
708*3b35e7eeSXin LI 				// used by the encoder.
709*3b35e7eeSXin LI 				const uint32_t inst2_rs1 = inst >> 27;
710*3b35e7eeSXin LI 
711*3b35e7eeSXin LI 				if (NOT_SPECIAL_AUIPC(inst, inst2_rs1)) {
712*3b35e7eeSXin LI 					i += 4 - 2;
713*3b35e7eeSXin LI 					continue;
714*3b35e7eeSXin LI 				}
715*3b35e7eeSXin LI 
716*3b35e7eeSXin LI 				// Decode the "real" pair.
717*3b35e7eeSXin LI 				uint32_t addr = read32be(buffer + i + 4);
718*3b35e7eeSXin LI 
719*3b35e7eeSXin LI 				addr -= now_pos + (uint32_t)i;
720*3b35e7eeSXin LI 
721*3b35e7eeSXin LI 				// The second instruction:
722*3b35e7eeSXin LI 				//   - Get the lowest 20 bits from inst.
723*3b35e7eeSXin LI 				//   - Add the lowest 12 bits of the address
724*3b35e7eeSXin LI 				//     as the immediate field.
725*3b35e7eeSXin LI 				inst2 = (inst >> 12) | (addr << 20);
726*3b35e7eeSXin LI 
727*3b35e7eeSXin LI 				// AUIPC:
728*3b35e7eeSXin LI 				//   - rd is the same as inst2_rs1.
729*3b35e7eeSXin LI 				//   - The sign extension of the lowest 12 bits
730*3b35e7eeSXin LI 				//     must be taken into account.
731*3b35e7eeSXin LI 				inst = 0x17 | (inst2_rs1 << 7)
732*3b35e7eeSXin LI 					| ((addr + 0x800) & 0xFFFFF000);
733*3b35e7eeSXin LI 			}
734*3b35e7eeSXin LI 
735*3b35e7eeSXin LI 			// Both decoder branches write in little endian order.
736*3b35e7eeSXin LI 			write32le(buffer + i, inst);
737*3b35e7eeSXin LI 			write32le(buffer + i + 4, inst2);
738*3b35e7eeSXin LI 
739*3b35e7eeSXin LI 			i += 8 - 2;
740*3b35e7eeSXin LI 		}
741*3b35e7eeSXin LI 	}
742*3b35e7eeSXin LI 
743*3b35e7eeSXin LI 	return i;
744*3b35e7eeSXin LI }
745*3b35e7eeSXin LI 
746*3b35e7eeSXin LI 
747*3b35e7eeSXin LI extern lzma_ret
lzma_simple_riscv_decoder_init(lzma_next_coder * next,const lzma_allocator * allocator,const lzma_filter_info * filters)748*3b35e7eeSXin LI lzma_simple_riscv_decoder_init(lzma_next_coder *next,
749*3b35e7eeSXin LI 		const lzma_allocator *allocator,
750*3b35e7eeSXin LI 		const lzma_filter_info *filters)
751*3b35e7eeSXin LI {
752*3b35e7eeSXin LI 	return lzma_simple_coder_init(next, allocator, filters,
753*3b35e7eeSXin LI 			&riscv_decode, 0, 8, 2, false);
754*3b35e7eeSXin LI }
755*3b35e7eeSXin LI #endif
756