Debian Codegraph @

rpython/jit/backend/riscv/instructions.py

raw symbols SCIP
1 #!/usr/bin/env python
2
3 # Each instruction consists of:
4 # (mnemonic, instr_type, operand_spec, type_specific_fields)
5 #
6 # Operand specification:
7 # R: General purpose register
8 # RB: General purpose register used as base address
9 # F: Floating point register
10 # I12: 12-bit immediate
11 # I13: 13-bit even immediate (used in B-type instructions)
12 # U20: 20-bit unsigned immediate
13 # I21: 21-bit even immediate (used in J-type instructions)
14 # SH5: 5-bit shift amount
15 # SH6: 6-bit shift amount
16 # RM: Rounding mode
17 # FMO: Fence instruction memory order (see FMO_ constants below)
18 # AMO: Acquire/release field for AMO instructions
19 # AMOL: Acquire/release field for load reserve
20 # AMOS: Acquire/release field for store conditional
21 #
22 # Type-specific fields are:
23 # R: (opcode, funct3, funct7)
24 # I: (opcode, funct3)
25 # S: (opcode, funct3)
26 # B: (opcode, funct3)
27 # U: (opcode,)
28 # J: (opcode,)
29 # I_SHAMT5: (opcode, funct3, funct7)
30 # I_SHAMT6: (opcode, funct3, funct6)
31 # R4_RM: (opcode, funct2)
32 # R_RM: (opcode, funct7)
33 # I12: (opcode, funct3, funct12)
34 # I12_RM: (opcode, funct12)
35 # A: (opcode, funct25)
36 # F: (opcode, funct3, fm)
37 # AMO2/AMO3: (opcode, funct3, funct5)
38
39 rv_base_i_instructions = [
40 ('LUI', 'U', 'R:U20', (0b0110111,)),
41 ('AUIPC', 'U', 'R:U20', (0b0010111,)),
42 ('JAL', 'J', 'R:I21', (0b1101111,)),
43 ('JALR', 'I', 'R:RB:I12', (0b1100111, 0b000)),
44 ('BEQ', 'B', 'R:R:I13', (0b1100011, 0b000)),
45 ('BNE', 'B', 'R:R:I13', (0b1100011, 0b001)),
46 ('BLT', 'B', 'R:R:I13', (0b1100011, 0b100)),
47 ('BGE', 'B', 'R:R:I13', (0b1100011, 0b101)),
48 ('BLTU', 'B', 'R:R:I13', (0b1100011, 0b110)),
49 ('BGEU', 'B', 'R:R:I13', (0b1100011, 0b111)),
50 ('LB', 'I', 'R:RB:I12', (0b0000011, 0b000)),
51 ('LH', 'I', 'R:RB:I12', (0b0000011, 0b001)),
52 ('LW', 'I', 'R:RB:I12', (0b0000011, 0b010)),
53 ('LBU', 'I', 'R:RB:I12', (0b0000011, 0b100)),
54 ('LHU', 'I', 'R:RB:I12', (0b0000011, 0b101)),
55 ('SB', 'S', 'R:RB:I12', (0b0100011, 0b000)),
56 ('SH', 'S', 'R:RB:I12', (0b0100011, 0b001)),
57 ('SW', 'S', 'R:RB:I12', (0b0100011, 0b010)),
58 ('ADDI', 'I', 'R:R:I12', (0b0010011, 0b000)),
59 ('SLTI', 'I', 'R:R:I12', (0b0010011, 0b010)),
60 ('SLTIU', 'I', 'R:R:I12', (0b0010011, 0b011)),
61 ('XORI', 'I', 'R:R:I12', (0b0010011, 0b100)),
62 ('ORI', 'I', 'R:R:I12', (0b0010011, 0b110)),
63 ('ANDI', 'I', 'R:R:I12', (0b0010011, 0b111)),
64 ('ADD', 'R', 'R:R:R', (0b0110011, 0b000, 0b0000000)),
65 ('SUB', 'R', 'R:R:R', (0b0110011, 0b000, 0b0100000)),
66 ('SLL', 'R', 'R:R:R', (0b0110011, 0b001, 0b0000000)),
67 ('SLT', 'R', 'R:R:R', (0b0110011, 0b010, 0b0000000)),
68 ('SLTU', 'R', 'R:R:R', (0b0110011, 0b011, 0b0000000)),
69 ('XOR', 'R', 'R:R:R', (0b0110011, 0b100, 0b0000000)),
70 ('SRL', 'R', 'R:R:R', (0b0110011, 0b101, 0b0000000)),
71 ('SRA', 'R', 'R:R:R', (0b0110011, 0b101, 0b0100000)),
72 ('OR', 'R', 'R:R:R', (0b0110011, 0b110, 0b0000000)),
73 ('AND', 'R', 'R:R:R', (0b0110011, 0b111, 0b0000000)),
74 ('ECALL', 'A', '', (0b1110011, 0b0000000000000000000000000)),
75 ('EBREAK', 'A', '', (0b1110011, 0b0000000000010000000000000)),
76 ('FENCE', 'F', 'FMO:FMO', (0b0001111, 0b000, 0b0000)),
77 ('FENCE_TSO', 'A', '', (0b0001111, 0b1000001100110000000000000)),
78 ]
79
80 rv32_base_i_instructions = [
81 ('SLLI', 'I_SHAMT5', 'R:R:SH5', (0b0010011, 0b001, 0b0000000)),
82 ('SRLI', 'I_SHAMT5', 'R:R:SH5', (0b0010011, 0b101, 0b0000000)),
83 ('SRAI', 'I_SHAMT5', 'R:R:SH5', (0b0010011, 0b101, 0b0100000)),
84 ]
85
86 rv64_base_i_instructions = [
87 ('LWU', 'I', 'R:RB:I12', (0b0000011, 0b110)),
88 ('LD', 'I', 'R:RB:I12', (0b0000011, 0b011)),
89 ('SD', 'S', 'R:RB:I12', (0b0100011, 0b011)),
90 ('SLLI', 'I_SHAMT6', 'R:R:SH6', (0b0010011, 0b001, 0b000000)),
91 ('SRLI', 'I_SHAMT6', 'R:R:SH6', (0b0010011, 0b101, 0b000000)),
92 ('SRAI', 'I_SHAMT6', 'R:R:SH6', (0b0010011, 0b101, 0b010000)),
93 ('ADDIW', 'I', 'R:R:I12', (0b0011011, 0b000)),
94 ('SLLIW', 'I_SHAMT5', 'R:R:SH5', (0b0011011, 0b001, 0b0000000)),
95 ('SRLIW', 'I_SHAMT5', 'R:R:SH5', (0b0011011, 0b101, 0b0000000)),
96 ('SRAIW', 'I_SHAMT5', 'R:R:SH5', (0b0011011, 0b101, 0b0100000)),
97 ('ADDW', 'R', 'R:R:R', (0b0111011, 0b000, 0b0000000)),
98 ('SUBW', 'R', 'R:R:R', (0b0111011, 0b000, 0b0100000)),
99 ('SLLW', 'R', 'R:R:R', (0b0111011, 0b001, 0b0000000)),
100 ('SRLW', 'R', 'R:R:R', (0b0111011, 0b101, 0b0000000)),
101 ('SRAW', 'R', 'R:R:R', (0b0111011, 0b101, 0b0100000)),
102 ]
103
104 rv_ext_m_instructions = [
105 ('MUL', 'R', 'R:R:R', (0b0110011, 0b000, 0b0000001)),
106 ('MULH', 'R', 'R:R:R', (0b0110011, 0b001, 0b0000001)),
107 ('MULHSU', 'R', 'R:R:R', (0b0110011, 0b010, 0b0000001)),
108 ('MULHU', 'R', 'R:R:R', (0b0110011, 0b011, 0b0000001)),
109 ('DIV', 'R', 'R:R:R', (0b0110011, 0b100, 0b0000001)),
110 ('DIVU', 'R', 'R:R:R', (0b0110011, 0b101, 0b0000001)),
111 ('REM', 'R', 'R:R:R', (0b0110011, 0b110, 0b0000001)),
112 ('REMU', 'R', 'R:R:R', (0b0110011, 0b111, 0b0000001)),
113 ]
114
115 rv64_ext_m_instructions = [
116 ('MULW', 'R', 'R:R:R', (0b0111011, 0b000, 0b0000001)),
117 ('DIVW', 'R', 'R:R:R', (0b0111011, 0b100, 0b0000001)),
118 ('DIVUW', 'R', 'R:R:R', (0b0111011, 0b101, 0b0000001)),
119 ('REMW', 'R', 'R:R:R', (0b0111011, 0b110, 0b0000001)),
120 ('REMUW', 'R', 'R:R:R', (0b0111011, 0b111, 0b0000001)),
121 ]
122
123 rv_ext_d_instructions = [
124 ('FLD', 'I', 'F:RB:I12', (0b0000111, 0b011)),
125 ('FSD', 'S', 'F:RB:I12', (0b0100111, 0b011)),
126 ('FMADD_D', 'R4_RM', 'F:F:F:F:RM', (0b1000011, 0b01)),
127 ('FMSUB_D', 'R4_RM', 'F:F:F:F:RM', (0b1000111, 0b01)),
128 ('FNMSUB_D', 'R4_RM', 'F:F:F:F:RM', (0b1001011, 0b01)),
129 ('FNMADD_D', 'R4_RM', 'F:F:F:F:RM', (0b1001111, 0b01)),
130 ('FADD_D', 'R_RM', 'F:F:F:RM', (0b1010011, 0b0000001)),
131 ('FSUB_D', 'R_RM', 'F:F:F:RM', (0b1010011, 0b0000101)),
132 ('FMUL_D', 'R_RM', 'F:F:F:RM', (0b1010011, 0b0001001)),
133 ('FDIV_D', 'R_RM', 'F:F:F:RM', (0b1010011, 0b0001101)),
134 ('FSQRT_D', 'I12_RM', 'F:F:RM', (0b1010011, 0b010110100000)),
135 ('FSGNJ_D', 'R', 'F:F:F', (0b1010011, 0b000, 0b0010001)),
136 ('FSGNJN_D', 'R', 'F:F:F', (0b1010011, 0b001, 0b0010001)),
137 ('FSGNJX_D', 'R', 'F:F:F', (0b1010011, 0b010, 0b0010001)),
138 ('FMIN_D', 'R', 'F:F:F', (0b1010011, 0b000, 0b0010101)),
139 ('FMAX_D', 'R', 'F:F:F', (0b1010011, 0b001, 0b0010101)),
140 ('FCVT_S_D', 'I12_RM', 'F:F:RM', (0b1010011, 0b010000000001)),
141 ('FCVT_D_S', 'I12', 'F:F', (0b1010011, 0b000, 0b010000100000)),
142 ('FEQ_D', 'R', 'R:F:F', (0b1010011, 0b010, 0b1010001)),
143 ('FLT_D', 'R', 'R:F:F', (0b1010011, 0b001, 0b1010001)),
144 ('FLE_D', 'R', 'R:F:F', (0b1010011, 0b000, 0b1010001)),
145 ('FCLASS_D', 'I12', 'R:F', (0b1010011, 0b001, 0b111000100000)),
146 ('FCVT_W_D', 'I12_RM', 'R:F:RM', (0b1010011, 0b110000100000)),
147 ('FCVT_WU_D', 'I12_RM', 'R:F:RM', (0b1010011, 0b110000100001)),
148 ('FCVT_D_W', 'I12', 'F:R', (0b1010011, 0b000, 0b110100100000)),
149 ('FCVT_D_WU', 'I12', 'F:R', (0b1010011, 0b000, 0b110100100001)),
150 ]
151
152 rv64_ext_d_instructions = [
153 ('FCVT_L_D', 'I12_RM', 'R:F:RM', (0b1010011, 0b110000100010)),
154 ('FCVT_LU_D', 'I12_RM', 'R:F:RM', (0b1010011, 0b110000100011)),
155 ('FMV_X_D', 'I12', 'R:F', (0b1010011, 0b000, 0b111000100000)),
156 ('FCVT_D_L', 'I12_RM', 'F:R:RM', (0b1010011, 0b110100100010)),
157 ('FCVT_D_LU', 'I12_RM', 'F:R:RM', (0b1010011, 0b110100100011)),
158 ('FMV_D_X', 'I12', 'F:R', (0b1010011, 0b000, 0b111100100000)),
159 ]
160
161 rv_ext_a_instructions = [
162 ('LR_W', 'AMO2', 'R:R:AMOLR', (0b0101111, 0b010, 0b00010)),
163 ('SC_W', 'AMO3', 'R:R:R:AMOSC', (0b0101111, 0b010, 0b00011)),
164 ('AMOSWAP_W', 'AMO3', 'R:R:R:AMO', (0b0101111, 0b010, 0b00001)),
165 ('AMOADD_W', 'AMO3', 'R:R:R:AMO', (0b0101111, 0b010, 0b00000)),
166 ('AMOXOR_W', 'AMO3', 'R:R:R:AMO', (0b0101111, 0b010, 0b00100)),
167 ('AMOAND_W', 'AMO3', 'R:R:R:AMO', (0b0101111, 0b010, 0b01100)),
168 ('AMOOR_W', 'AMO3', 'R:R:R:AMO', (0b0101111, 0b010, 0b01000)),
169 ('AMOMIN_W', 'AMO3', 'R:R:R:AMO', (0b0101111, 0b010, 0b10000)),
170 ('AMOMAX_W', 'AMO3', 'R:R:R:AMO', (0b0101111, 0b010, 0b10100)),
171 ('AMOMINU_W', 'AMO3', 'R:R:R:AMO', (0b0101111, 0b010, 0b11000)),
172 ('AMOMAXU_W', 'AMO3', 'R:R:R:AMO', (0b0101111, 0b010, 0b11100)),
173 ]
174
175 rv64_ext_a_instructions = [
176 ('LR_D', 'AMO2', 'R:R:AMOLR', (0b0101111, 0b011, 0b00010)),
177 ('SC_D', 'AMO3', 'R:R:R:AMOSC', (0b0101111, 0b011, 0b00011)),
178 ('AMOSWAP_D', 'AMO3', 'R:R:R:AMO', (0b0101111, 0b011, 0b00001)),
179 ('AMOADD_D', 'AMO3', 'R:R:R:AMO', (0b0101111, 0b011, 0b00000)),
180 ('AMOXOR_D', 'AMO3', 'R:R:R:AMO', (0b0101111, 0b011, 0b00100)),
181 ('AMOAND_D', 'AMO3', 'R:R:R:AMO', (0b0101111, 0b011, 0b01100)),
182 ('AMOOR_D', 'AMO3', 'R:R:R:AMO', (0b0101111, 0b011, 0b01000)),
183 ('AMOMIN_D', 'AMO3', 'R:R:R:AMO', (0b0101111, 0b011, 0b10000)),
184 ('AMOMAX_D', 'AMO3', 'R:R:R:AMO', (0b0101111, 0b011, 0b10100)),
185 ('AMOMINU_D', 'AMO3', 'R:R:R:AMO', (0b0101111, 0b011, 0b11000)),
186 ('AMOMAXU_D', 'AMO3', 'R:R:R:AMO', (0b0101111, 0b011, 0b11100)),
187 ]
188
189 all_instructions = \
190 rv_base_i_instructions + \
191 rv64_base_i_instructions + \
192 rv_ext_m_instructions + \
193 rv64_ext_m_instructions + \
194 rv_ext_d_instructions + \
195 rv64_ext_d_instructions + \
196 rv_ext_a_instructions + \
197 rv64_ext_a_instructions
198
199 # fence instruction memory ordering immediate fields
200 FMO_WRITE = 0b0001
201 FMO_READ = 0b0010
202 FMO_OUTPUT = 0b0100
203 FMO_INPUT = 0b1000
204
205 AMO_ACQUIRE = 0b10
206 AMO_RELEASE = 0b01
207
208 def _main():
209 has_error = False
210
211 # Check whether there are duplicated mnemonics.
212 mnemonics = set()
213 for mnemonic, instr_type, op_spec, fields in all_instructions:
214 if mnemonic in mnemonics:
215 print 'error: Found duplicated mnemonic:', mnemonic
216 has_error = True
217 mnemonics.add(mnemonic)
218
219 # Check whether the type-specific fields are correct.
220 _EXPECTED_NUM_FIELDS = {
221 'R': 3,
222 'I': 2,
223 'S': 2,
224 'B': 2,
225 'U': 1,
226 'J': 1,
227 'I_SHAMT5': 3,
228 'I_SHAMT6': 3,
229 'R4_RM': 2,
230 'R_RM': 2,
231 'I12': 3,
232 'I12_RM': 2,
233 'A': 2,
234 'F': 3,
235 'AMO2': 3,
236 'AMO3': 3,
237 }
238 for mnemonic, instr_type, op_spec, fields in all_instructions:
239 if len(fields) != _EXPECTED_NUM_FIELDS[instr_type]:
240 print 'error: Mismatched number of fields:', mnemonic
241 has_error = True
242
243 # Check whether the operand specification matches the instruction type.
244 _SUPPORTED_OP_SPEC = {
245 'R': {'F:F:F', 'R:F:F', 'R:R:R'},
246 'I': {'F:RB:I12', 'R:R:I12', 'R:RB:I12'},
247 'S': {'F:RB:I12', 'R:RB:I12'},
248 'B': {'R:R:I13'},
249 'U': {'R:U20'},
250 'J': {'R:I21'},
251 'I_SHAMT5': {'R:R:SH5'},
252 'I_SHAMT6': {'R:R:SH6'},
253 'R4_RM': {'F:F:F:F:RM'},
254 'R_RM': {'F:F:F:RM'},
255 'I12': {'F:F', 'F:R', 'R:F'},
256 'I12_RM': {'F:F:RM', 'F:R:RM', 'R:F:RM'},
257 'A': {''},
258 'F': {'FMO:FMO'},
259 'AMO2': {'R:R:AMOLR'},
260 'AMO3': {'R:R:R:AMO', 'R:R:R:AMOSC'},
261 }
262 for mnemonic, instr_type, op_spec, fields in all_instructions:
263 if op_spec not in _SUPPORTED_OP_SPEC[instr_type]:
264 print 'error: Unsupported operand specification:', mnemonic, \
265 instr_type, op_spec
266 has_error = True
267
268 # Check whether there are unused supported operand specifications.
269 all_instructions_op_specs = set(
270 (instr_type, op_spec) for _, instr_type, op_spec, _ in all_instructions)
271 for instr_type, op_specs in _SUPPORTED_OP_SPEC.iteritems():
272 for op_spec in op_specs:
273 if (instr_type, op_spec) not in all_instructions_op_specs:
274 print 'error: Found unused supported operand spec:', \
275 instr_type, op_spec
276 has_error = True
277
278 if not has_error:
279 print 'defined', len(mnemonics), 'instructions successfully'
280
281 if __name__ == '__main__':
282 _main()
283 del _main