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) 0
29 #define COND_IS_LOGICAL(cond) 0
31 #define ARCH_PARTIAL_ALU(size) 0
32 #define ARCH_IS_3ADDRESS 1
33 #define ARCH_HAS_FLAGS 0
34 #define ARCH_PREFERS_SX(size) 0
35 #define ARCH_HAS_BWX 1
36 #define ARCH_HAS_MUL 1
37 #define ARCH_HAS_DIV 1
38 #define ARCH_HAS_ANDN cpu_test_feature(CPU_FEATURE_zbb)
39 #define ARCH_HAS_SHIFTED_ADD(bits) ((bits) <= 3 && cpu_test_feature(CPU_FEATURE_zba))
40 #define ARCH_HAS_BTX(btx, size, cnst) (((size) == OP_SIZE_8 || (cnst)) && cpu_test_feature(CPU_FEATURE_zbs))
41 #define ARCH_SHIFT_SIZE OP_SIZE_4
42 #define ARCH_NEEDS_BARRIER 0
44 #define i_size(size) OP_SIZE_NATIVE
45 #define i_size_rot(size) maximum(size, OP_SIZE_4)
114 #define R_UPCALL R_S1
115 #define R_TIMESTAMP R_S2
117 #define R_SCRATCH_1 R_A0
118 #define R_SCRATCH_2 R_A1
119 #define R_SCRATCH_3 R_A2
120 #define R_SCRATCH_4 R_A3
121 #define R_SCRATCH_NA_1 R_A4
122 #define R_SCRATCH_NA_2 R_A5
123 #ifdef HAVE_BITWISE_FRAME
124 #define R_SCRATCH_NA_3 R_A6
127 #define R_SAVED_1 R_S3
128 #define R_SAVED_2 R_S4
137 #define R_OFFSET_IMM R_T0
138 #define R_CONST_IMM R_T1
139 #define R_CONST_HELPER R_T2
140 #define R_CMP_RESULT R_T3
142 #define FR_SCRATCH_1 R_FA0
143 #define FR_SCRATCH_2 R_FA1
145 #define SUPPORTED_FP 0x6
147 #define FRAME_SIZE 0x70
149 static bool reg_is_fp(unsigned reg)
151 return reg >= 0x20 && reg < 0x40;
154 static const uint8_t regs_saved[] = { R_S5, R_S6, R_S7, R_S8, R_S9, R_S10, R_S11 };
155 static const uint8_t regs_volatile[] = { R_RA,
156 #ifndef HAVE_BITWISE_FRAME
159 R_A7, R_T4, R_T5, R_T6 };
160 static const uint8_t fp_saved[] = { 0 };
161 #define n_fp_saved 0U
162 static const uint8_t fp_volatile[] = { 0 };
163 #define n_fp_volatile 0U
164 #define reg_is_saved(r) (((r) >= R_S0 && (r) <= R_S1) || ((r) >= R_S2 && (r) <= R_S11))
166 static const struct {
169 } riscv_compress[] = {
170 #include "riscv-c.inc"
173 static bool attr_w gen_load_constant(struct codegen_context *ctx, unsigned reg, uint64_t c)
176 int32_t c1, c2, c3, c4;
184 c2 = (c >> 12) & 0xfffffUL;
190 c3 = (c >> 32) & 0xfffUL;
194 c += 0x100000000000UL;
201 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
204 gen_eight((uint64_t)c4 << 12);
208 gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_ADD, 0);
216 gen_insn(INSN_ROT, OP_SIZE_NATIVE, ROT_SHL, 0);
224 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
225 gen_one(R_CONST_HELPER);
227 gen_eight((uint64_t)c2 << 12);
229 gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_ADD, 0);
232 gen_one(R_CONST_HELPER);
234 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
237 gen_eight((uint64_t)c2 << 12);
241 if (c1 || r == R_ZERO) {
242 gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_ADD, 0);
251 static bool attr_w gen_address(struct codegen_context *ctx, unsigned base, int64_t imm, unsigned purpose, unsigned size)
253 ctx->base_reg = base;
254 ctx->offset_imm = imm;
255 ctx->offset_reg = false;
257 case IMM_PURPOSE_LDR_OFFSET:
258 case IMM_PURPOSE_LDR_SX_OFFSET:
259 case IMM_PURPOSE_STR_OFFSET:
260 case IMM_PURPOSE_VLDR_VSTR_OFFSET:
261 case IMM_PURPOSE_MVI_CLI_OFFSET:
262 if (likely(imm >= -0x800) && likely(imm < 0x800))
266 internal(file_line, "gen_address: invalid purpose %u (imm %"PRIxMAX", size %u)", purpose, (uintmax_t)imm, size);
268 g(gen_load_constant(ctx, R_OFFSET_IMM, imm));
269 gen_insn(INSN_ALU, OP_SIZE_ADDRESS, ALU_ADD, 0);
270 gen_one(R_OFFSET_IMM);
271 gen_one(R_OFFSET_IMM);
273 ctx->base_reg = R_OFFSET_IMM;
278 static bool is_direct_const(int64_t imm, unsigned purpose, unsigned size)
281 case IMM_PURPOSE_STORE_VALUE:
285 case IMM_PURPOSE_ADD:
286 case IMM_PURPOSE_AND:
288 case IMM_PURPOSE_XOR:
289 case IMM_PURPOSE_TEST:
290 case IMM_PURPOSE_CMP:
291 case IMM_PURPOSE_CMP_LOGICAL:
292 if (likely(imm >= -0x800) && likely(imm < 0x800))
295 case IMM_PURPOSE_SUB:
296 if (likely(imm > -0x800) && likely(imm <= 0x800))
299 case IMM_PURPOSE_ANDN:
301 case IMM_PURPOSE_JMP_2REGS:
303 case IMM_PURPOSE_MUL:
305 case IMM_PURPOSE_BITWISE:
308 internal(file_line, "is_direct_const: invalid purpose %u (imm %"PRIxMAX", size %u)", purpose, (uintmax_t)imm, size);
313 static bool attr_w gen_imm(struct codegen_context *ctx, int64_t imm, unsigned purpose, unsigned size)
315 if (is_direct_const(imm, purpose, size)) {
316 ctx->const_imm = imm;
317 ctx->const_reg = false;
319 g(gen_load_constant(ctx, R_CONST_IMM, imm));
320 ctx->const_reg = true;
325 static bool attr_w gen_entry(struct codegen_context *ctx)
327 g(gen_imm(ctx, -FRAME_SIZE, IMM_PURPOSE_ADD, OP_SIZE_NATIVE));
328 gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_ADD, 0);
333 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x08, IMM_PURPOSE_STR_OFFSET, OP_SIZE_NATIVE));
334 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
335 gen_address_offset();
338 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x10, IMM_PURPOSE_STR_OFFSET, OP_SIZE_NATIVE));
339 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
340 gen_address_offset();
343 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x18, IMM_PURPOSE_STR_OFFSET, OP_SIZE_NATIVE));
344 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
345 gen_address_offset();
348 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x20, IMM_PURPOSE_STR_OFFSET, OP_SIZE_NATIVE));
349 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
350 gen_address_offset();
353 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x28, IMM_PURPOSE_STR_OFFSET, OP_SIZE_NATIVE));
354 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
355 gen_address_offset();
358 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x30, IMM_PURPOSE_STR_OFFSET, OP_SIZE_NATIVE));
359 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
360 gen_address_offset();
363 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x38, IMM_PURPOSE_STR_OFFSET, OP_SIZE_NATIVE));
364 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
365 gen_address_offset();
368 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x40, IMM_PURPOSE_STR_OFFSET, OP_SIZE_NATIVE));
369 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
370 gen_address_offset();
373 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x48, IMM_PURPOSE_STR_OFFSET, OP_SIZE_NATIVE));
374 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
375 gen_address_offset();
378 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x50, IMM_PURPOSE_STR_OFFSET, OP_SIZE_NATIVE));
379 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
380 gen_address_offset();
383 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x58, IMM_PURPOSE_STR_OFFSET, OP_SIZE_NATIVE));
384 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
385 gen_address_offset();
388 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x60, IMM_PURPOSE_STR_OFFSET, OP_SIZE_NATIVE));
389 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
390 gen_address_offset();
393 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x68, IMM_PURPOSE_STR_OFFSET, OP_SIZE_NATIVE));
394 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
395 gen_address_offset();
398 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
402 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
406 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
407 gen_one(R_TIMESTAMP);
410 gen_insn(INSN_JMP_INDIRECT, 0, 0, 0);
416 static bool attr_w gen_escape_arg(struct codegen_context *ctx, ip_t ip, uint32_t escape_label)
418 g(gen_load_constant(ctx, R_RET1, (int32_t)ip));
420 gen_insn(INSN_JMP, 0, 0, 0);
421 gen_four(escape_label);
426 static bool attr_w gen_escape(struct codegen_context *ctx)
428 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
432 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x08, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
433 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
435 gen_address_offset();
437 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x10, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
438 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
440 gen_address_offset();
442 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x18, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
443 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
445 gen_address_offset();
447 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x20, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
448 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
450 gen_address_offset();
452 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x28, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
453 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
455 gen_address_offset();
457 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x30, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
458 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
460 gen_address_offset();
462 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x38, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
463 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
465 gen_address_offset();
467 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x40, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
468 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
470 gen_address_offset();
472 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x48, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
473 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
475 gen_address_offset();
477 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x50, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
478 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
480 gen_address_offset();
482 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x58, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
483 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
485 gen_address_offset();
487 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x60, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
488 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
490 gen_address_offset();
492 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x68, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
493 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
495 gen_address_offset();
497 g(gen_imm(ctx, FRAME_SIZE, IMM_PURPOSE_ADD, OP_SIZE_NATIVE));
498 gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_ADD, 0);
503 gen_insn(INSN_RET, 0, 0, 0);
508 static bool attr_w gen_upcall_argument(struct codegen_context attr_unused *ctx, unsigned attr_unused arg)
513 static bool attr_w gen_upcall(struct codegen_context *ctx, unsigned offset, unsigned n_args)
515 g(gen_address(ctx, R_UPCALL, offset, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_ADDRESS));
516 gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
517 gen_one(R_SCRATCH_NA_1);
518 gen_address_offset();
520 gen_insn(INSN_CALL_INDIRECT, OP_SIZE_ADDRESS, 0, 0);
521 gen_one(R_SCRATCH_NA_1);
523 g(gen_upcall_end(ctx, n_args));
528 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);
530 static bool attr_w gen_timestamp_test(struct codegen_context *ctx, uint32_t escape_label)
532 g(gen_address(ctx, R_UPCALL, offsetof(struct cg_upcall_vector_s, ts), IMM_PURPOSE_LDR_SX_OFFSET, OP_SIZE_4));
533 gen_insn(INSN_MOVSX, OP_SIZE_4, 0, 0);
534 gen_one(R_SCRATCH_1);
535 gen_address_offset();
537 g(gen_cmp_test_jmp(ctx, INSN_CMP, OP_SIZE_NATIVE, R_SCRATCH_1, R_TIMESTAMP, COND_NE, escape_label));