2 * Copyright (C) 2024 Mikulas Patocka
4 * This file is part of Ajla.
6 * Ajla is free software: you can redistribute it and/or modify it under the
7 * terms of the GNU General Public License as published by the Free Software
8 * Foundation, either version 3 of the License, or (at your option) any later
11 * Ajla is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along with
16 * Ajla. If not, see <https://www.gnu.org/licenses/>.
19 #define OP_SIZE_NATIVE OP_SIZE_8
20 #define OP_SIZE_ADDRESS OP_SIZE_NATIVE
22 #define JMP_LIMIT JMP_EXTRA_LONG
24 #define UNALIGNED_TRAP (!cpu_test_feature(CPU_FEATURE_unaligned))
26 #define ALU_WRITES_FLAGS(alu, im) 0
27 #define ALU1_WRITES_FLAGS(alu) 0
28 #define ROT_WRITES_FLAGS(alu, size, im) 0
29 #define COND_IS_LOGICAL(cond) 0
31 #define ARCH_PARTIAL_ALU(size) 0
32 #define ARCH_IS_3ADDRESS(alu, f) 1
33 #define ARCH_IS_3ADDRESS_IMM(alu, f) 1
34 #define ARCH_IS_3ADDRESS_ROT(alu, size) 1
35 #define ARCH_IS_3ADDRESS_ROT_IMM(alu) 1
36 #define ARCH_IS_2ADDRESS(alu) 1
37 #define ARCH_IS_3ADDRESS_FP 1
38 #define ARCH_HAS_JMP_2REGS(cond) 1
39 #define ARCH_HAS_FLAGS 0
40 #define ARCH_PREFERS_SX(size) 0
41 #define ARCH_HAS_BWX 1
42 #define ARCH_HAS_MUL 1
43 #define ARCH_HAS_DIV 1
44 #define ARCH_HAS_ANDN cpu_test_feature(CPU_FEATURE_zbb)
45 #define ARCH_HAS_SHIFTED_ADD(bits) ((bits) <= 3 && cpu_test_feature(CPU_FEATURE_zba))
46 #define ARCH_HAS_BTX(btx, size, cnst) (((size) == OP_SIZE_8 || (cnst)) && cpu_test_feature(CPU_FEATURE_zbs))
47 #define ARCH_SHIFT_SIZE OP_SIZE_4
48 #define ARCH_BOOL_SIZE OP_SIZE_NATIVE
49 #define ARCH_HAS_FP_GP_MOV 0
50 #define ARCH_NEEDS_BARRIER 0
52 #define i_size(size) OP_SIZE_NATIVE
53 #define i_size_rot(size) maximum(size, OP_SIZE_4)
54 #define i_size_cmp(size) OP_SIZE_NATIVE
123 #define R_UPCALL R_S1
124 #define R_TIMESTAMP R_S2
126 #define R_SCRATCH_1 R_A0
127 #define R_SCRATCH_2 R_A1
128 #define R_SCRATCH_3 R_A2
129 #define R_SCRATCH_4 R_A3
130 #define R_SCRATCH_NA_1 R_A4
131 #define R_SCRATCH_NA_2 R_A5
132 #ifdef HAVE_BITWISE_FRAME
133 #define R_SCRATCH_NA_3 R_A6
136 #define R_SAVED_1 R_S3
137 #define R_SAVED_2 R_S4
146 #define R_OFFSET_IMM R_T0
147 #define R_CONST_IMM R_T1
148 #define R_CONST_HELPER R_T2
149 #define R_CMP_RESULT R_T3
151 #define FR_SCRATCH_1 R_FT0
152 #define FR_SCRATCH_2 R_FT1
154 #define SUPPORTED_FP 0x6
156 #define FRAME_SIZE 0x70
158 static bool reg_is_fp(unsigned reg)
160 return reg >= 0x20 && reg < 0x40;
163 static const uint8_t regs_saved[] = { R_S5, R_S6, R_S7, R_S8, R_S9, R_S10, R_S11 };
164 static const uint8_t regs_volatile[] = { R_RA,
165 #ifndef HAVE_BITWISE_FRAME
168 R_A7, R_T4, R_T5, R_T6 };
169 static const uint8_t fp_saved[] = { 0 };
170 #define n_fp_saved 0U
171 static const uint8_t fp_volatile[] = { R_FT2, R_FT3, R_FT4, R_FT5, R_FT6, R_FT7, R_FA0, R_FA1, R_FA2, R_FA3, R_FA4, R_FA5, R_FA6, R_FA7, R_FT8, R_FT9, R_FT10, R_FT11 };
172 #define reg_is_saved(r) (((r) >= R_S0 && (r) <= R_S1) || ((r) >= R_S2 && (r) <= R_S11) || ((r) >= R_FS0 && (r) <= R_FS1) || ((r) >= R_FS2 && (r) <= R_FS11))
174 static const struct {
177 } riscv_compress[] = {
178 #include "riscv-c.inc"
181 static bool attr_w gen_load_constant(struct codegen_context *ctx, unsigned reg, uint64_t c)
184 int32_t c1, c2, c3, c4;
192 c2 = (c >> 12) & 0xfffffUL;
198 c3 = (c >> 32) & 0xfffUL;
202 c += 0x100000000000UL;
209 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
212 gen_eight((uint64_t)c4 << 12);
216 gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_ADD, 0);
224 gen_insn(INSN_ROT, OP_SIZE_NATIVE, ROT_SHL, 0);
232 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
233 gen_one(R_CONST_HELPER);
235 gen_eight((uint64_t)c2 << 12);
237 gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_ADD, 0);
240 gen_one(R_CONST_HELPER);
242 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
245 gen_eight((uint64_t)c2 << 12);
249 if (c1 || r == R_ZERO) {
250 gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_ADD, 0);
259 static bool attr_w gen_address(struct codegen_context *ctx, unsigned base, int64_t imm, unsigned purpose, unsigned size)
261 ctx->base_reg = base;
262 ctx->offset_imm = imm;
263 ctx->offset_reg = false;
265 case IMM_PURPOSE_LDR_OFFSET:
266 case IMM_PURPOSE_LDR_SX_OFFSET:
267 case IMM_PURPOSE_STR_OFFSET:
268 case IMM_PURPOSE_VLDR_VSTR_OFFSET:
269 case IMM_PURPOSE_MVI_CLI_OFFSET:
270 if (likely(imm >= -0x800) && likely(imm < 0x800))
274 internal(file_line, "gen_address: invalid purpose %u (imm %"PRIxMAX", size %u)", purpose, (uintmax_t)imm, size);
276 g(gen_load_constant(ctx, R_OFFSET_IMM, imm));
277 gen_insn(INSN_ALU, OP_SIZE_ADDRESS, ALU_ADD, 0);
278 gen_one(R_OFFSET_IMM);
279 gen_one(R_OFFSET_IMM);
281 ctx->base_reg = R_OFFSET_IMM;
286 static bool is_direct_const(int64_t imm, unsigned purpose, unsigned size)
289 case IMM_PURPOSE_STORE_VALUE:
293 case IMM_PURPOSE_ADD:
294 case IMM_PURPOSE_AND:
296 case IMM_PURPOSE_XOR:
297 case IMM_PURPOSE_TEST:
298 case IMM_PURPOSE_CMP:
299 case IMM_PURPOSE_CMP_LOGICAL:
300 if (likely(imm >= -0x800) && likely(imm < 0x800))
303 case IMM_PURPOSE_SUB:
304 if (likely(imm > -0x800) && likely(imm <= 0x800))
307 case IMM_PURPOSE_ANDN:
309 case IMM_PURPOSE_JMP_2REGS:
311 case IMM_PURPOSE_MUL:
313 case IMM_PURPOSE_BITWISE:
316 internal(file_line, "is_direct_const: invalid purpose %u (imm %"PRIxMAX", size %u)", purpose, (uintmax_t)imm, size);
321 static bool attr_w gen_imm(struct codegen_context *ctx, int64_t imm, unsigned purpose, unsigned size)
323 if (is_direct_const(imm, purpose, size)) {
324 ctx->const_imm = imm;
325 ctx->const_reg = false;
327 g(gen_load_constant(ctx, R_CONST_IMM, imm));
328 ctx->const_reg = true;
333 static bool attr_w gen_entry(struct codegen_context *ctx)
335 g(gen_imm(ctx, -FRAME_SIZE, IMM_PURPOSE_ADD, OP_SIZE_NATIVE));
336 gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_ADD, 0);
341 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x08, IMM_PURPOSE_STR_OFFSET, OP_SIZE_NATIVE));
342 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
343 gen_address_offset();
346 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x10, IMM_PURPOSE_STR_OFFSET, OP_SIZE_NATIVE));
347 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
348 gen_address_offset();
351 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x18, IMM_PURPOSE_STR_OFFSET, OP_SIZE_NATIVE));
352 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
353 gen_address_offset();
356 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x20, IMM_PURPOSE_STR_OFFSET, OP_SIZE_NATIVE));
357 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
358 gen_address_offset();
361 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x28, IMM_PURPOSE_STR_OFFSET, OP_SIZE_NATIVE));
362 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
363 gen_address_offset();
366 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x30, IMM_PURPOSE_STR_OFFSET, OP_SIZE_NATIVE));
367 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
368 gen_address_offset();
371 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x38, IMM_PURPOSE_STR_OFFSET, OP_SIZE_NATIVE));
372 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
373 gen_address_offset();
376 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x40, IMM_PURPOSE_STR_OFFSET, OP_SIZE_NATIVE));
377 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
378 gen_address_offset();
381 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x48, IMM_PURPOSE_STR_OFFSET, OP_SIZE_NATIVE));
382 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
383 gen_address_offset();
386 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x50, IMM_PURPOSE_STR_OFFSET, OP_SIZE_NATIVE));
387 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
388 gen_address_offset();
391 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x58, IMM_PURPOSE_STR_OFFSET, OP_SIZE_NATIVE));
392 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
393 gen_address_offset();
396 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x60, IMM_PURPOSE_STR_OFFSET, OP_SIZE_NATIVE));
397 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
398 gen_address_offset();
401 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x68, IMM_PURPOSE_STR_OFFSET, OP_SIZE_NATIVE));
402 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
403 gen_address_offset();
406 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
410 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
414 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
415 gen_one(R_TIMESTAMP);
418 gen_insn(INSN_JMP_INDIRECT, 0, 0, 0);
424 static bool attr_w gen_escape_arg(struct codegen_context *ctx, ip_t ip, uint32_t escape_label)
426 g(gen_load_constant(ctx, R_RET1, (int32_t)ip));
428 gen_insn(INSN_JMP, 0, 0, 0);
429 gen_four(escape_label);
434 static bool attr_w gen_escape(struct codegen_context *ctx)
436 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
440 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x08, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
441 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
443 gen_address_offset();
445 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x10, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
446 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
448 gen_address_offset();
450 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x18, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
451 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
453 gen_address_offset();
455 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x20, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
456 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
458 gen_address_offset();
460 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x28, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
461 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
463 gen_address_offset();
465 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x30, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
466 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
468 gen_address_offset();
470 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x38, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
471 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
473 gen_address_offset();
475 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x40, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
476 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
478 gen_address_offset();
480 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x48, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
481 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
483 gen_address_offset();
485 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x50, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
486 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
488 gen_address_offset();
490 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x58, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
491 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
493 gen_address_offset();
495 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x60, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
496 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
498 gen_address_offset();
500 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x68, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
501 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
503 gen_address_offset();
505 g(gen_imm(ctx, FRAME_SIZE, IMM_PURPOSE_ADD, OP_SIZE_NATIVE));
506 gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_ADD, 0);
511 gen_insn(INSN_RET, 0, 0, 0);
516 static bool attr_w gen_upcall_argument(struct codegen_context attr_unused *ctx, unsigned attr_unused arg)
521 static bool attr_w gen_upcall(struct codegen_context *ctx, unsigned offset, unsigned n_args)
523 g(gen_address(ctx, R_UPCALL, offset, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_ADDRESS));
524 gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
525 gen_one(R_SCRATCH_NA_1);
526 gen_address_offset();
528 gen_insn(INSN_CALL_INDIRECT, OP_SIZE_ADDRESS, 0, 0);
529 gen_one(R_SCRATCH_NA_1);
531 g(gen_upcall_end(ctx, n_args));
536 static bool attr_w gen_cmp_test_jmp(struct codegen_context *ctx, unsigned insn, unsigned op_size, unsigned reg1, unsigned reg2, unsigned cond, uint32_t label);
538 static bool attr_w gen_timestamp_test(struct codegen_context *ctx, uint32_t escape_label)
540 g(gen_address(ctx, R_UPCALL, offsetof(struct cg_upcall_vector_s, ts), IMM_PURPOSE_LDR_SX_OFFSET, OP_SIZE_4));
541 gen_insn(INSN_MOVSX, OP_SIZE_4, 0, 0);
542 gen_one(R_SCRATCH_1);
543 gen_address_offset();
545 g(gen_cmp_test_jmp(ctx, INSN_CMP, OP_SIZE_NATIVE, R_SCRATCH_1, R_TIMESTAMP, COND_NE, escape_label));