@@ -151,23 +151,40 @@ void asm_rv32_end_pass(asm_rv32_t *state);
151
151
((op & 0b1111111) | ((rd & 0b11111) << 7) | \
152
152
(imm & 0b11111111111111111111000000000000))
153
153
154
+ #define RV32_ENCODE_TYPE_CB (op , ft3 , rs , imm ) \
155
+ ((op & 0b11) | ((ft3 & 0b111) << 13) | ((rs & 0b111) << 7) | \
156
+ (((imm) & 0b100000000) << 4) | (((imm) & 0b11000000) >> 1) | \
157
+ (((imm) & 0b100000) >> 3) | (((imm) & 0b11000) << 7) | \
158
+ (((imm) & 0b110) << 2))
159
+
154
160
#define RV32_ENCODE_TYPE_CI (op , ft3 , rd , imm ) \
155
161
((op & 0b11) | ((ft3 & 0b111) << 13) | ((rd & 0b11111) << 7) | \
156
162
(((imm) & 0b100000) << 7) | (((imm) & 0b11111) << 2))
157
163
164
+ #define RV32_ENCODE_TYPE_CIW (op , ft3 , rd , imm ) \
165
+ ((op & 0b11) | ((ft3 & 0b111) << 13) | ((rd & 0b111) << 2) | \
166
+ ((imm & 0b1111000000) << 1) | ((imm & 0b110000) << 7) | \
167
+ ((imm & 0b1000) << 2) | ((imm & 0b100) << 4))
168
+
158
169
#define RV32_ENCODE_TYPE_CJ (op , ft3 , imm ) \
159
170
((op & 0b11) | ((ft3 & 0b111) << 13) | \
160
171
((imm & 0b1110) << 2) | ((imm & 0b1100000000) << 1) | \
161
172
((imm & 0b100000000000) << 1) | ((imm & 0b10000000000) >> 2) | \
162
173
((imm & 0b10000000) >> 1) | ((imm & 0b1000000) << 1) | \
163
174
((imm & 0b100000) >> 3) | ((imm & 0b10000) << 7))
164
175
176
+ #define RV32_ENCODE_TYPE_CL (op , ft3 , rd , rs , imm ) \
177
+ ((op & 0b11) | ((ft3 & 0b111) << 13) | ((rd & 0b111) << 2) | \
178
+ ((rs & 0b111) << 7) | ((imm & 0b1000000) >> 1) | \
179
+ ((imm & 0b111000) << 7) | ((imm & 0b100) << 4))
180
+
165
181
#define RV32_ENCODE_TYPE_CR (op , ft4 , rs1 , rs2 ) \
166
182
((op & 0b11) | ((rs2 & 0b11111) << 2) | ((rs1 & 0b11111) << 7) | \
167
183
((ft4 & 0b1111) << 12))
168
184
169
185
#define RV32_ENCODE_TYPE_CSS (op , ft3 , rs , imm ) \
170
- ((op & 0b11) | ((ft3 & 0b111) << 13) | ((rs & 0b11111) << 2) | ((imm) & 0b111111) << 7)
186
+ ((op & 0b11) | ((ft3 & 0b111) << 13) | ((rs & 0b11111) << 2) | \
187
+ ((imm) & 0b111111) << 7)
171
188
172
189
void asm_rv32_emit_word_opcode (asm_rv32_t * state , mp_uint_t opcode );
173
190
void asm_rv32_emit_halfword_opcode (asm_rv32_t * state , mp_uint_t opcode );
@@ -220,10 +237,28 @@ static inline void asm_rv32_opcode_caddi(asm_rv32_t *state, mp_uint_t rd, mp_int
220
237
asm_rv32_emit_halfword_opcode (state , RV32_ENCODE_TYPE_CI (0b01 , 0b000 , rd , immediate ));
221
238
}
222
239
240
+ // C.ADDI4SPN RD', IMMEDIATE
241
+ static inline void asm_rv32_opcode_caddi4spn (asm_rv32_t * state , mp_uint_t rd , mp_uint_t immediate ) {
242
+ // CIW: 000 ........ ... 00
243
+ asm_rv32_emit_halfword_opcode (state , RV32_ENCODE_TYPE_CIW (0b00 , 0b000 , rd , immediate ));
244
+ }
245
+
246
+ // C.BEQZ RS', IMMEDIATE
247
+ static inline void asm_rv32_opcode_cbeqz (asm_rv32_t * state , mp_uint_t rs , mp_int_t offset ) {
248
+ // CB: 110 ... ... ..... 01
249
+ asm_rv32_emit_halfword_opcode (state , RV32_ENCODE_TYPE_CB (0b01 , 0b110 , rs , offset ));
250
+ }
251
+
252
+ // C.BNEZ RS', IMMEDIATE
253
+ static inline void asm_rv32_opcode_cbnez (asm_rv32_t * state , mp_uint_t rs , mp_int_t offset ) {
254
+ // CB: 111 ... ... ..... 01
255
+ asm_rv32_emit_halfword_opcode (state , RV32_ENCODE_TYPE_CB (0b01 , 0b111 , rs , offset ));
256
+ }
257
+
223
258
// C.J OFFSET
224
- static inline void asm_rv32_opcode_cj (asm_rv32_t * state , mp_uint_t offset ) {
259
+ static inline void asm_rv32_opcode_cj (asm_rv32_t * state , mp_int_t offset ) {
225
260
// CJ: 101 ........... 01
226
- asm_rv32_emit_halfword_opcode (state , RV32_ENCODE_TYPE_CJ (0b01 , 0b001 , offset ));
261
+ asm_rv32_emit_halfword_opcode (state , RV32_ENCODE_TYPE_CJ (0b01 , 0b101 , offset ));
227
262
}
228
263
229
264
// C.JALR RS
@@ -250,6 +285,12 @@ static inline void asm_rv32_opcode_clui(asm_rv32_t *state, mp_uint_t rd, mp_int_
250
285
asm_rv32_emit_halfword_opcode (state , RV32_ENCODE_TYPE_CI (0b01 , 0b011 , rd , immediate >> 12 ));
251
286
}
252
287
288
+ // C.LW RD', OFFSET(RS')
289
+ static inline void asm_rv32_opcode_clw (asm_rv32_t * state , mp_uint_t rd , mp_uint_t rs , mp_int_t offset ) {
290
+ // CL: 010 ... ... .. ... 00
291
+ asm_rv32_emit_halfword_opcode (state , RV32_ENCODE_TYPE_CL (0b00 , 0b010 , rd , rs , offset ));
292
+ }
293
+
253
294
// C.LWSP RD, OFFSET
254
295
static inline void asm_rv32_opcode_clwsp (asm_rv32_t * state , mp_uint_t rd , mp_uint_t offset ) {
255
296
// CI: 010 . ..... ..... 10
@@ -383,6 +424,7 @@ static inline void asm_rv32_opcode_xori(asm_rv32_t *state, mp_uint_t rd, mp_uint
383
424
}
384
425
385
426
#define ASM_WORD_SIZE (4)
427
+ #define ASM_HALFWORD_SIZE (2)
386
428
387
429
#define REG_RET ASM_RV32_REG_A0
388
430
#define REG_ARG_1 ASM_RV32_REG_A0
@@ -392,8 +434,7 @@ static inline void asm_rv32_opcode_xori(asm_rv32_t *state, mp_uint_t rd, mp_uint
392
434
#define REG_TEMP0 ASM_RV32_REG_T1
393
435
#define REG_TEMP1 ASM_RV32_REG_T2
394
436
#define REG_TEMP2 ASM_RV32_REG_T3
395
- // S0 may be used as the frame pointer by the compiler.
396
- #define REG_FUN_TABLE ASM_RV32_REG_S2
437
+ #define REG_FUN_TABLE ASM_RV32_REG_S1
397
438
#define REG_LOCAL_1 ASM_RV32_REG_S3
398
439
#define REG_LOCAL_2 ASM_RV32_REG_S4
399
440
#define REG_LOCAL_3 ASM_RV32_REG_S5
@@ -432,10 +473,10 @@ void asm_rv32_emit_store_reg_reg_offset(asm_rv32_t *state, mp_uint_t source, mp_
432
473
#define ASM_JUMP_IF_REG_EQ (state , rs1 , rs2 , label ) asm_rv32_emit_jump_if_reg_eq(state, rs1, rs2, label)
433
474
#define ASM_JUMP_IF_REG_NONZERO (state , rs , label , bool_test ) asm_rv32_emit_jump_if_reg_nonzero(state, rs, label)
434
475
#define ASM_JUMP_IF_REG_ZERO (state , rs , label , bool_test ) asm_rv32_emit_jump_if_reg_eq(state, rs, ASM_RV32_REG_ZERO, label)
435
- #define ASM_JUMP_REG (state , rs ) asm_rv32_opcode_jalr (state, ASM_RV32_REG_ZERO, rs, 0 )
476
+ #define ASM_JUMP_REG (state , rs ) asm_rv32_opcode_cjr (state, rs )
436
477
#define ASM_LOAD16_REG_REG_OFFSET (state , rd , rs , offset ) asm_rv32_emit_load16_reg_reg_offset(state, rd, rs, offset)
437
478
#define ASM_LOAD16_REG_REG (state , rd , rs ) asm_rv32_opcode_lhu(state, rd, rs, 0)
438
- #define ASM_LOAD32_REG_REG (state , rd , rs ) asm_rv32_opcode_lw (state, rd, rs, 0)
479
+ #define ASM_LOAD32_REG_REG (state , rd , rs ) ASM_LOAD_REG_REG_OFFSET (state, rd, rs, 0)
439
480
#define ASM_LOAD8_REG_REG (state , rd , rs ) asm_rv32_opcode_lbu(state, rd, rs, 0)
440
481
#define ASM_LOAD_REG_REG_OFFSET (state , rd , rs , offset ) asm_rv32_emit_load_reg_reg_offset(state, rd, rs, offset)
441
482
#define ASM_LOAD_REG_REG (state , rd , rs ) ASM_LOAD32_REG_REG(state, rd, rs)
@@ -452,7 +493,7 @@ void asm_rv32_emit_store_reg_reg_offset(asm_rv32_t *state, mp_uint_t source, mp_
452
493
#define ASM_NOT_REG (state , rd ) asm_rv32_opcode_xori(state, rd, rd, -1)
453
494
#define ASM_OR_REG_REG (state , rd , rs ) asm_rv32_opcode_or(state, rd, rd, rs)
454
495
#define ASM_STORE16_REG_REG (state , rs1 , rs2 ) asm_rv32_opcode_sh(state, rs1, rs2, 0)
455
- #define ASM_STORE32_REG_REG (state , rs1 , rs2 ) asm_rv32_opcode_sw (state, rs1, rs2, 0)
496
+ #define ASM_STORE32_REG_REG (state , rs1 , rs2 ) ASM_STORE_REG_REG_OFFSET (state, rs1, rs2, 0)
456
497
#define ASM_STORE8_REG_REG (state , rs1 , rs2 ) asm_rv32_opcode_sb(state, rs1, rs2, 0)
457
498
#define ASM_STORE_REG_REG_OFFSET (state , rd , rs , offset ) asm_rv32_emit_store_reg_reg_offset(state, rd, rs, offset)
458
499
#define ASM_STORE_REG_REG (state , rs1 , rs2 ) ASM_STORE32_REG_REG(state, rs1, rs2)
0 commit comments