45
45
#endif
46
46
47
47
#define INTERNAL_TEMPORARY ASM_RV32_REG_S0
48
- #define AVAILABLE_REGISTERS_COUNT 32
49
-
50
- #define IS_IN_C_REGISTER_WINDOW (register_number ) \
51
- (((register_number) >= ASM_RV32_REG_X8) && ((register_number) <= ASM_RV32_REG_X15))
52
- #define MAP_IN_C_REGISTER_WINDOW (register_number ) \
53
- ((register_number) - ASM_RV32_REG_X8)
54
48
55
49
#define FIT_UNSIGNED (value , bits ) (((value) & ~((1U << (bits)) - 1)) == 0)
56
50
#define FIT_SIGNED (value , bits ) \
@@ -106,7 +100,6 @@ static void split_immediate(mp_int_t immediate, mp_uint_t *upper, mp_uint_t *low
106
100
// Turn the lower half from unsigned to signed.
107
101
if ((* lower & 0x800 ) != 0 ) {
108
102
* upper += 0x1000 ;
109
- * lower -= 0x1000 ;
110
103
}
111
104
}
112
105
@@ -180,7 +173,7 @@ void asm_rv32_emit_optimised_load_immediate(asm_rv32_t *state, mp_uint_t rd, mp_
180
173
181
174
static void emit_registers_store (asm_rv32_t * state , mp_uint_t registers_mask ) {
182
175
mp_uint_t offset = 0 ;
183
- for (mp_uint_t register_index = 0 ; register_index < AVAILABLE_REGISTERS_COUNT ; register_index ++ ) {
176
+ for (mp_uint_t register_index = 0 ; register_index < RV32_AVAILABLE_REGISTERS_COUNT ; register_index ++ ) {
184
177
if (registers_mask & (1U << register_index )) {
185
178
assert (FIT_UNSIGNED (offset >> 2 , 6 ) && "Registers save stack offset out of range." );
186
179
// c.swsp register, offset
@@ -192,7 +185,7 @@ static void emit_registers_store(asm_rv32_t *state, mp_uint_t registers_mask) {
192
185
193
186
static void emit_registers_load (asm_rv32_t * state , mp_uint_t registers_mask ) {
194
187
mp_uint_t offset = 0 ;
195
- for (mp_uint_t register_index = 0 ; register_index < AVAILABLE_REGISTERS_COUNT ; register_index ++ ) {
188
+ for (mp_uint_t register_index = 0 ; register_index < RV32_AVAILABLE_REGISTERS_COUNT ; register_index ++ ) {
196
189
if (registers_mask & (1U << register_index )) {
197
190
assert (FIT_UNSIGNED (offset >> 2 , 6 ) && "Registers load stack offset out of range." );
198
191
// c.lwsp register, offset
@@ -262,7 +255,7 @@ static bool calculate_displacement_for_label(asm_rv32_t *state, mp_uint_t label,
262
255
263
256
void asm_rv32_entry (asm_rv32_t * state , mp_uint_t locals ) {
264
257
state -> saved_registers_mask |= (1U << REG_FUN_TABLE ) | (1U << REG_LOCAL_1 ) | \
265
- (1U << REG_LOCAL_2 ) | (1U << REG_LOCAL_3 ) | ( 1U << INTERNAL_TEMPORARY ) ;
258
+ (1U << REG_LOCAL_2 ) | (1U << REG_LOCAL_3 );
266
259
state -> locals_count = locals ;
267
260
emit_function_prologue (state , state -> saved_registers_mask );
268
261
}
@@ -281,10 +274,11 @@ void asm_rv32_emit_call_ind(asm_rv32_t *state, mp_uint_t index) {
281
274
mp_uint_t offset = index * ASM_WORD_SIZE ;
282
275
state -> saved_registers_mask |= (1U << ASM_RV32_REG_RA );
283
276
284
- if (IS_IN_C_REGISTER_WINDOW (REG_FUN_TABLE ) && IS_IN_C_REGISTER_WINDOW (INTERNAL_TEMPORARY ) && FIT_UNSIGNED (offset , 6 )) {
277
+ if (RV32_IS_IN_C_REGISTER_WINDOW (REG_FUN_TABLE ) && RV32_IS_IN_C_REGISTER_WINDOW (INTERNAL_TEMPORARY ) && FIT_UNSIGNED (offset , 6 )) {
278
+ state -> saved_registers_mask |= (1U << INTERNAL_TEMPORARY );
285
279
// c.lw temporary, offset(fun_table)
286
280
// c.jalr temporary
287
- asm_rv32_opcode_clw (state , MAP_IN_C_REGISTER_WINDOW (INTERNAL_TEMPORARY ), MAP_IN_C_REGISTER_WINDOW (REG_FUN_TABLE ), offset );
281
+ asm_rv32_opcode_clw (state , RV32_MAP_IN_C_REGISTER_WINDOW (INTERNAL_TEMPORARY ), RV32_MAP_IN_C_REGISTER_WINDOW (REG_FUN_TABLE ), offset );
288
282
asm_rv32_opcode_cjalr (state , INTERNAL_TEMPORARY );
289
283
return ;
290
284
}
@@ -341,9 +335,9 @@ void asm_rv32_emit_jump_if_reg_nonzero(asm_rv32_t *state, mp_uint_t rs, mp_uint_
341
335
ptrdiff_t displacement = 0 ;
342
336
bool can_emit_short_jump = calculate_displacement_for_label (state , label , & displacement );
343
337
344
- if (can_emit_short_jump && FIT_SIGNED (displacement , 8 ) && IS_IN_C_REGISTER_WINDOW (rs )) {
338
+ if (can_emit_short_jump && FIT_SIGNED (displacement , 8 ) && RV32_IS_IN_C_REGISTER_WINDOW (rs )) {
345
339
// c.bnez rs', displacement
346
- asm_rv32_opcode_cbnez (state , MAP_IN_C_REGISTER_WINDOW (rs ), displacement );
340
+ asm_rv32_opcode_cbnez (state , RV32_MAP_IN_C_REGISTER_WINDOW (rs ), displacement );
347
341
return ;
348
342
}
349
343
@@ -364,8 +358,8 @@ void asm_rv32_emit_jump_if_reg_nonzero(asm_rv32_t *state, mp_uint_t rs, mp_uint_
364
358
// jalr zero, temporary, LO(displacement) ; PC + 8
365
359
// ... ; PC + 12
366
360
367
- if (can_emit_short_jump && IS_IN_C_REGISTER_WINDOW (rs )) {
368
- asm_rv32_opcode_cbeqz (state , MAP_IN_C_REGISTER_WINDOW (rs ), 10 );
361
+ if (can_emit_short_jump && RV32_IS_IN_C_REGISTER_WINDOW (rs )) {
362
+ asm_rv32_opcode_cbeqz (state , RV32_MAP_IN_C_REGISTER_WINDOW (rs ), 10 );
369
363
// Compensate for the C.BEQZ opcode.
370
364
displacement -= ASM_HALFWORD_SIZE ;
371
365
} else {
@@ -438,9 +432,9 @@ void asm_rv32_emit_mov_reg_local(asm_rv32_t *state, mp_uint_t rd, mp_uint_t loca
438
432
void asm_rv32_emit_mov_reg_local_addr (asm_rv32_t * state , mp_uint_t rd , mp_uint_t local ) {
439
433
mp_uint_t offset = state -> locals_stack_offset + (local * ASM_WORD_SIZE );
440
434
441
- if (FIT_UNSIGNED (offset , 10 ) && offset != 0 && IS_IN_C_REGISTER_WINDOW (rd )) {
435
+ if (FIT_UNSIGNED (offset , 10 ) && offset != 0 && RV32_IS_IN_C_REGISTER_WINDOW (rd )) {
442
436
// c.addi4spn rd', offset
443
- asm_rv32_opcode_caddi4spn (state , MAP_IN_C_REGISTER_WINDOW (rd ), offset );
437
+ asm_rv32_opcode_caddi4spn (state , RV32_MAP_IN_C_REGISTER_WINDOW (rd ), offset );
444
438
return ;
445
439
}
446
440
@@ -459,9 +453,9 @@ void asm_rv32_emit_mov_reg_local_addr(asm_rv32_t *state, mp_uint_t rd, mp_uint_t
459
453
void asm_rv32_emit_load_reg_reg_offset (asm_rv32_t * state , mp_uint_t rd , mp_uint_t rs , mp_int_t offset ) {
460
454
mp_int_t scaled_offset = offset * sizeof (ASM_WORD_SIZE );
461
455
462
- if (scaled_offset >= 0 && IS_IN_C_REGISTER_WINDOW (rd ) && IS_IN_C_REGISTER_WINDOW (rs ) && FIT_UNSIGNED (scaled_offset , 6 )) {
456
+ if (scaled_offset >= 0 && RV32_IS_IN_C_REGISTER_WINDOW (rd ) && RV32_IS_IN_C_REGISTER_WINDOW (rs ) && FIT_UNSIGNED (scaled_offset , 6 )) {
463
457
// c.lw rd', offset(rs')
464
- asm_rv32_opcode_clw (state , MAP_IN_C_REGISTER_WINDOW (rd ), MAP_IN_C_REGISTER_WINDOW (rs ), scaled_offset );
458
+ asm_rv32_opcode_clw (state , RV32_MAP_IN_C_REGISTER_WINDOW (rd ), RV32_MAP_IN_C_REGISTER_WINDOW (rs ), scaled_offset );
465
459
return ;
466
460
}
467
461
0 commit comments