Debian Codegraph @

src/org/tukaani/xz/simple/RISCVDecoder.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 decoder for RISC-V instructions
10 //
11 // See the comments in RISCVEncoder.java. Most of them aren't duplicated here.
12 public final class RISCVDecoder implements SimpleFilter {
13 private int pos;
14
15 public RISCVDecoder(int startPos) {
16 pos = startPos;
17 }
18
19 @Override
20 public int code(byte[] buf, int off, int len) {
21 int end = off + len - 8;
22 int i;
23
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 final int b2 = buf[i + 2] & 0xFF;
36 final int b3 = buf[i + 3] & 0xFF;
37 final int pc = pos + i - off;
38
39 // | b3 | b2 | b1 |
40 // | 31 30 29 28 27 26 25 24 | 23 22 21 20 19 18 17 16 | 15 14 13 12 x x x x |
41 // | 20 10 9 8 7 6 5 4 | 3 2 1 11 19 18 17 16 | 15 14 13 12 x x x x |
42 // | 7 6 5 4 3 2 1 0 | 7 6 5 4 3 2 1 0 | 7 6 5 4 x x x x |
43
44 int addr = ((b1 & 0xF0) << 13) | (b2 << 9) | (b3 << 1);
45
46 addr -= pc;
47
48 buf[i + 1] = (byte)((b1 & 0x0F) | ((addr >>> 8) & 0xF0));
49
50 buf[i + 2] = (byte)(((addr >>> 16) & 0x0F)
51 | ((addr >>> 7) & 0x10)
52 | ((addr << 4) & 0xE0));
53
54 buf[i + 3] = (byte)(((addr >>> 4) & 0x7F)
55 | ((addr >>> 13) & 0x80));
56
57 i += 4 - 2;
58
59 } else if ((inst & 0x7F) == 0x17) {
60 // AUIPC
61 int inst2;
62
63 inst |= (buf[i + 1] & 0xFF) << 8;
64 inst |= (buf[i + 2] & 0xFF) << 16;
65 inst |= (buf[i + 3] & 0xFF) << 24;
66
67 if ((inst & 0xE80) != 0) {
68 // AUIPC's rd doesn't equal x0 or x2.
69
70 // Check if it is a "fake" AUIPC+inst2 pair.
71 inst2 = ByteArrayView.getIntLE(buf, i + 4);
72
73 if ((((inst << 8) ^ inst2) & 0xF8003) != 3) {
74 i += 6 - 2;
75 continue;
76 }
77
78 // Decode (or more like re-encode) the "fake" pair.
79 // The "fake" format doesn't do sign-extension,
80 // address conversion, or use big endian.
81 int addr = (inst & 0xFFFFF000) + (inst2 >>> 20);
82
83 inst = 0x17 | (2 << 7) | (inst2 << 12);
84 inst2 = addr;
85 } else {
86 // AUIPC's rd equals x0 or x2.
87
88 // Check if inst matches the special format
89 // used by the encoder.
90 final int inst2Rs1 = inst >>> 27;
91
92 if (((inst - 0x3100) & 0x3F80) >= (inst2Rs1 & 0x1D)) {
93 i += 4 - 2;
94 continue;
95 }
96
97 // Decode the "real" pair.
98 int addr = ByteArrayView.getIntBE(buf, i + 4);
99
100 addr -= pos + i - off;
101
102 // The second instruction:
103 // - Get the lowest 20 bits from inst.
104 // - Add the lowest 12 bits of the address
105 // as the immediate field.
106 inst2 = (inst >>> 12) | (addr << 20);
107
108 // AUIPC:
109 // - rd is the same as inst2_rs1.
110 // - The sign extension of the lowest 12 bits
111 // must be taken into account.
112 inst = 0x17 | (inst2Rs1 << 7)
113 | ((addr + 0x800) & 0xFFFFF000);
114 }
115
116 // Both decoder branches write in little endian order.
117 ByteArrayView.setIntLE(buf, i, inst);
118 ByteArrayView.setIntLE(buf, i + 4, inst2);
119
120 i += 8 - 2;
121 }
122 }
123
124 i -= off;
125 pos += i;
126 return i;
127 }
128 }