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_SHORTEST
24 #define UNALIGNED_TRAP 1
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_FLAGS 0
39 #define ARCH_SUPPORTS_TRAPS OS_SUPPORTS_TRAPS
40 #define ARCH_TRAP_BEFORE 0
41 #define ARCH_PREFERS_SX(size) ((size) == OP_SIZE_4)
42 #define ARCH_HAS_BWX cpu_test_feature(CPU_FEATURE_bwx)
43 #define ARCH_HAS_MUL 1
44 #define ARCH_HAS_DIV 0
45 #define ARCH_HAS_ANDN 1
46 #define ARCH_HAS_SHIFTED_ADD(bits) ((bits) == 0 || (bits) == 2 || (bits) == 3)
47 #define ARCH_HAS_BTX(btx, size, cnst) 0
48 #define ARCH_SHIFT_SIZE OP_SIZE_8
49 #define ARCH_HAS_FP_GP_MOV cpu_test_feature(CPU_FEATURE_fix)
50 #define ARCH_NEEDS_BARRIER thread_needs_barriers
52 #define i_size(size) OP_SIZE_NATIVE
53 #define i_size_rot(size) OP_SIZE_NATIVE
54 #define i_size_cmp(size) OP_SIZE_NATIVE
56 /*#define TIMESTAMP_IN_REGISTER*/
125 #define R_UPCALL R_S1
126 #ifdef TIMESTAMP_IN_REGISTER
127 #define R_TIMESTAMP R_S2
130 #define R_SCRATCH_1 R_A0
131 #define R_SCRATCH_2 R_A1
132 #define R_SCRATCH_3 R_A2
133 #define R_SCRATCH_4 R_A3
134 #define R_SCRATCH_NA_1 R_T0
135 #define R_SCRATCH_NA_2 R_T1
136 #define R_SCRATCH_NA_3 R_T2
138 #define R_SAVED_1 R_S3
139 #define R_SAVED_2 R_S4
148 #define R_OFFSET_IMM R_T3
149 #define R_CONST_IMM R_T4
150 #define R_CMP_RESULT R_T5
152 #define FR_SCRATCH_1 R_F0
153 #define FR_SCRATCH_2 R_F1
154 #define FR_SCRATCH_3 R_F10
156 #define SUPPORTED_FP 0x6
158 #define FRAME_SIZE 0x50
160 static bool reg_is_fp(unsigned reg)
162 return reg >= 0x20 && reg < 0x40;
165 static const uint8_t regs_saved[] = {
166 #ifndef TIMESTAMP_IN_REGISTER
170 static const uint8_t regs_volatile[] = { R_T6, R_T7, R_A4, R_A5, R_T8, R_T9, R_T10, R_T11, R_RA, R_T12, R_AT, R_GP };
171 static const uint8_t fp_saved[] = { 0 };
172 #define n_fp_saved 0U
173 static const uint8_t fp_volatile[] = { R_F11, R_F12, R_F13, R_F14, R_F15, R_F16, R_F17, R_F18, R_F19, R_F20, R_F21, R_F22, R_F23, R_F24, R_F25, R_F26, R_F27, R_F28, R_F29, R_F30 };
174 #define reg_is_saved(r) (((r) >= R_S0 && (r) <= R_FP) || ((r) >= R_F2 && (r) <= R_F9))
176 static bool attr_w gen_load_constant(struct codegen_context *ctx, unsigned reg, uint64_t c)
179 int16_t c1, c2, c3, c4;
184 c2 = (int16_t)(c >> 16);
188 c3 = (int16_t)(c >> 32);
189 c &= ~0xffffffffffffUL;
191 c += 0x1000000000000UL;
192 c4 = (int16_t)(c >> 48);
194 gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_ADD, 0);
198 gen_eight((uint64_t)c4 << 16);
202 gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_ADD, 0);
210 gen_insn(INSN_ROT, OP_SIZE_NATIVE, ROT_SHL, 0);
217 gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_ADD, 0);
221 gen_eight((uint64_t)c2 << 16);
224 if (c1 || r == R_ZERO) {
225 gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_ADD, 0);
234 static bool attr_w gen_address(struct codegen_context *ctx, unsigned base, int64_t imm, unsigned purpose, unsigned size)
236 ctx->base_reg = base;
237 ctx->offset_imm = imm;
238 ctx->offset_reg = false;
240 case IMM_PURPOSE_LDR_OFFSET:
241 case IMM_PURPOSE_LDR_SX_OFFSET:
242 case IMM_PURPOSE_STR_OFFSET:
243 case IMM_PURPOSE_VLDR_VSTR_OFFSET:
244 case IMM_PURPOSE_MVI_CLI_OFFSET:
245 if (likely(imm >= -0x8000) && likely(imm < 0x8000))
249 internal(file_line, "gen_address: invalid purpose %u (imm %"PRIxMAX", size %u)", purpose, (uintmax_t)imm, size);
251 g(gen_load_constant(ctx, R_OFFSET_IMM, imm));
252 gen_insn(INSN_ALU, OP_SIZE_ADDRESS, ALU_ADD, 0);
253 gen_one(R_OFFSET_IMM);
254 gen_one(R_OFFSET_IMM);
256 ctx->base_reg = R_OFFSET_IMM;
261 static bool is_direct_const(int64_t imm, unsigned purpose, unsigned size)
263 int64_t imm_copy = imm;
265 case IMM_PURPOSE_STORE_VALUE:
269 case IMM_PURPOSE_SUB:
270 imm_copy = -(uint64_t)imm_copy;
272 case IMM_PURPOSE_ADD:
273 if (likely(imm_copy >= -0x8000) && likely(imm_copy < 0x8000))
275 if (imm_copy & 0xffff)
277 if (likely(imm_copy >= -0x80000000L) && likely(imm_copy < 0x80000000L))
280 case IMM_PURPOSE_CMP:
281 case IMM_PURPOSE_CMP_LOGICAL:
282 case IMM_PURPOSE_AND:
284 case IMM_PURPOSE_XOR:
285 case IMM_PURPOSE_ANDN:
286 case IMM_PURPOSE_TEST:
287 case IMM_PURPOSE_MUL:
288 case IMM_PURPOSE_MOVR:
289 if (imm >= 0 && imm < 256)
293 internal(file_line, "is_direct_const: invalid purpose %u (imm %"PRIxMAX", size %u)", purpose, (uintmax_t)imm, size);
298 static bool attr_w gen_imm(struct codegen_context *ctx, int64_t imm, unsigned purpose, unsigned size)
300 if (is_direct_const(imm, purpose, size)) {
301 ctx->const_imm = imm;
302 ctx->const_reg = false;
304 g(gen_load_constant(ctx, R_CONST_IMM, imm));
305 ctx->const_reg = true;
310 static bool attr_w gen_entry(struct codegen_context *ctx)
312 g(gen_imm(ctx, FRAME_SIZE, IMM_PURPOSE_SUB, OP_SIZE_NATIVE));
313 gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_SUB, 0);
318 g(gen_address(ctx, R_SP, 0, IMM_PURPOSE_STR_OFFSET, OP_SIZE_NATIVE));
319 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
320 gen_address_offset();
323 #ifndef TIMESTAMP_IN_REGISTER
324 g(gen_address(ctx, R_SP, 8, IMM_PURPOSE_STR_OFFSET, OP_SIZE_4));
325 gen_insn(INSN_MOV, OP_SIZE_4, 0, 0);
326 gen_address_offset();
330 g(gen_address(ctx, R_SP, 16, IMM_PURPOSE_STR_OFFSET, OP_SIZE_NATIVE));
331 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
332 gen_address_offset();
335 g(gen_address(ctx, R_SP, 24, IMM_PURPOSE_STR_OFFSET, OP_SIZE_NATIVE));
336 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
337 gen_address_offset();
340 g(gen_address(ctx, R_SP, 32, IMM_PURPOSE_STR_OFFSET, OP_SIZE_NATIVE));
341 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
342 gen_address_offset();
345 g(gen_address(ctx, R_SP, 40, IMM_PURPOSE_STR_OFFSET, OP_SIZE_NATIVE));
346 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
347 gen_address_offset();
350 g(gen_address(ctx, R_SP, 48, IMM_PURPOSE_STR_OFFSET, OP_SIZE_NATIVE));
351 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
352 gen_address_offset();
355 g(gen_address(ctx, R_SP, 56, IMM_PURPOSE_STR_OFFSET, OP_SIZE_NATIVE));
356 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
357 gen_address_offset();
360 g(gen_address(ctx, R_SP, 64, IMM_PURPOSE_STR_OFFSET, OP_SIZE_NATIVE));
361 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
362 gen_address_offset();
365 g(gen_address(ctx, R_SP, 72, IMM_PURPOSE_STR_OFFSET, OP_SIZE_NATIVE));
366 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
367 gen_address_offset();
370 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
374 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
378 #ifdef TIMESTAMP_IN_REGISTER
379 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
380 gen_one(R_TIMESTAMP);
384 gen_insn(INSN_JMP_INDIRECT, 0, 0, 0);
390 static bool attr_w gen_escape_arg(struct codegen_context *ctx, ip_t ip, uint32_t escape_label)
392 g(gen_load_constant(ctx, R_SCRATCH_1, (int32_t)ip));
394 gen_insn(INSN_JMP, 0, 0, 0);
395 gen_four(escape_label);
400 static bool attr_w gen_escape(struct codegen_context *ctx)
402 g(gen_address(ctx, R_SP, 72, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
403 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
405 gen_address_offset();
407 g(gen_address(ctx, R_RET0, 0, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
408 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
409 gen_address_offset();
412 g(gen_address(ctx, R_RET0, 8, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_4));
413 gen_insn(INSN_MOV, OP_SIZE_4, 0, 0);
414 gen_address_offset();
415 gen_one(R_SCRATCH_1);
417 g(gen_address(ctx, R_SP, 0, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
418 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
420 gen_address_offset();
422 g(gen_address(ctx, R_SP, 16, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
423 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
425 gen_address_offset();
427 g(gen_address(ctx, R_SP, 24, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
428 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
430 gen_address_offset();
432 g(gen_address(ctx, R_SP, 32, 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, 40, 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, 48, 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, 56, 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, 64, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
453 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
455 gen_address_offset();
457 g(gen_imm(ctx, FRAME_SIZE, IMM_PURPOSE_ADD, OP_SIZE_NATIVE));
458 gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_ADD, 0);
463 gen_insn(INSN_RET, 0, 0, 0);
468 static bool attr_w gen_upcall_argument(struct codegen_context attr_unused *ctx, unsigned attr_unused arg)
473 static bool attr_w gen_upcall(struct codegen_context *ctx, unsigned offset, unsigned n_args)
475 g(gen_address(ctx, R_UPCALL, offset, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
476 gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
478 gen_address_offset();
480 gen_insn(INSN_CALL_INDIRECT, OP_SIZE_ADDRESS, 0, 0);
483 g(gen_upcall_end(ctx, n_args));
488 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);
490 static bool attr_w gen_timestamp_test(struct codegen_context *ctx, uint32_t escape_label)
492 g(gen_address(ctx, R_UPCALL, offsetof(struct cg_upcall_vector_s, ts), IMM_PURPOSE_LDR_SX_OFFSET, OP_SIZE_4));
493 gen_insn(INSN_MOVSX, OP_SIZE_4, 0, 0);
494 gen_one(R_SCRATCH_1);
495 gen_address_offset();
497 #ifdef TIMESTAMP_IN_REGISTER
498 g(gen_cmp_test_jmp(ctx, INSN_CMP, OP_SIZE_NATIVE, R_SCRATCH_1, R_TIMESTAMP, COND_NE, escape_label));
500 g(gen_address(ctx, R_SP, 8, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_4));
501 gen_insn(INSN_MOVSX, OP_SIZE_4, 0, 0);
502 gen_one(R_SCRATCH_2);
503 gen_address_offset();
505 g(gen_cmp_test_jmp(ctx, INSN_CMP, OP_SIZE_NATIVE, R_SCRATCH_1, R_SCRATCH_2, COND_NE, escape_label));