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 #if !defined(ARCH_SPARC64)
20 #define SPARC_9 cpu_test_feature(CPU_FEATURE_sparc9)
21 #define FRAME_SIZE 0x60
22 #define OP_SIZE_NATIVE (SPARC_9 ? OP_SIZE_8 : OP_SIZE_4)
23 #define OP_SIZE_ADDRESS OP_SIZE_4
26 #define FRAME_SIZE 0xb0
27 #define OP_SIZE_NATIVE OP_SIZE_8
28 #define OP_SIZE_ADDRESS OP_SIZE_8
31 #define JMP_LIMIT JMP_LONG
33 #define UNALIGNED_TRAP 1
35 #define ALU_WRITES_FLAGS(alu, im) 0
36 #define ALU1_WRITES_FLAGS(alu) 0
37 #define ROT_WRITES_FLAGS(alu, size, im) 0
38 #define COND_IS_LOGICAL(cond) 0
40 #define ARCH_PARTIAL_ALU(size) 0
41 #define ARCH_IS_3ADDRESS(alu, f) 1
42 #define ARCH_IS_3ADDRESS_IMM(alu, f) 1
43 #define ARCH_IS_3ADDRESS_ROT(alu, size) 1
44 #define ARCH_IS_3ADDRESS_ROT_IMM(alu) 1
45 #define ARCH_IS_2ADDRESS(alu) 1
46 #define ARCH_IS_3ADDRESS_FP 1
47 #define ARCH_HAS_JMP_2REGS(cond) 0
48 #define ARCH_HAS_FLAGS 1
49 #define ARCH_PREFERS_SX(size) 0
50 #define ARCH_HAS_BWX 1
51 #define ARCH_HAS_MUL SPARC_9
52 #define ARCH_HAS_DIV SPARC_9
53 #define ARCH_HAS_ANDN 1
54 #define ARCH_HAS_SHIFTED_ADD(bits) 0
55 #define ARCH_HAS_BTX(btx, size, cnst) 0
56 #define ARCH_SHIFT_SIZE OP_SIZE_4
57 #define ARCH_BOOL_SIZE OP_SIZE_NATIVE
58 #define ARCH_HAS_FP_GP_MOV 0
59 #define ARCH_NEEDS_BARRIER 0
61 #define i_size(size) OP_SIZE_NATIVE
62 #define i_size_rot(size) maximum(size, OP_SIZE_4)
63 #define i_size_cmp(size) maximum(size, OP_SIZE_4)
65 /*#define SUPPORT_QUAD_PRECISION*/
187 #define R_UPCALL R_I1
188 #define R_TIMESTAMP R_I2
190 #define R_SCRATCH_1 R_O1
191 #define R_SCRATCH_2 R_O0
192 #define R_SCRATCH_3 R_O3
193 #define R_SCRATCH_4 R_O2
195 #define R_SCRATCH_NA_1 R_O4
196 #define R_SCRATCH_NA_2 R_O5
197 #ifdef HAVE_BITWISE_FRAME
198 #define R_SCRATCH_NA_3 R_O7
201 #define R_SAVED_1 R_L0
202 #define R_SAVED_2 R_L1
210 #define R_OFFSET_IMM R_G1
211 #define R_CONST_IMM R_G2
212 #define R_CONST_HELPER R_G3
214 #ifdef SUPPORT_QUAD_PRECISION
215 #define FR_SCRATCH_1 FDR_0
216 #define FR_SCRATCH_2 FDR_4
217 #define SUPPORTED_FP 0x16
219 #define FR_SCRATCH_1 FDR_0
220 #define FR_SCRATCH_2 FDR_2
221 #define SUPPORTED_FP 0x6
224 static bool reg_is_fp(unsigned reg)
226 return reg >= 0x20 && reg < 0x40;
229 static const uint8_t regs_saved[] = { R_L2, R_L3, R_L4, R_L5, R_L6, R_L7, R_I3, R_I4, R_I5 };
230 static const uint8_t regs_volatile[] = { R_G4, R_G5,
231 #ifndef HAVE_BITWISE_FRAME
235 static const uint8_t fp_saved[] = { 0 };
236 #define n_fp_saved 0U
237 #ifdef SUPPORT_QUAD_PRECISION
238 static const uint8_t fp_volatile[] = { FDR_8, FDR_12, FDR_16, FDR_20, FDR_24, FDR_28 };
240 static const uint8_t fp_volatile[] = { FDR_4, FDR_6, FDR_8, FDR_10, FDR_12, FDR_14, FDR_16, FDR_18, FDR_20, FDR_22, FDR_24, FDR_26, FDR_28, FDR_30 };
242 #define reg_is_saved(r) ((r) >= R_L0 && (r) <= R_I7)
244 static bool attr_w gen_load_constant(struct codegen_context *ctx, unsigned reg, uint64_t c)
248 if (c >= (uint64_t)-4096) {
249 gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_OR, 0);
256 if (c >= 0x100000000ULL) {
257 int32_t cu = c >> 32;
258 if (cu < -4096 || cu >= 4096) {
259 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
260 gen_one(R_CONST_HELPER);
262 gen_eight(cu & 0xFFFFFC00UL);
265 gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_OR, 0);
266 gen_one(R_CONST_HELPER);
267 gen_one(R_CONST_HELPER);
272 gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_OR, 0);
273 gen_one(R_CONST_HELPER);
278 if (!(c & 0xFFFFFFFFULL)) {
279 gen_insn(INSN_ROT, OP_SIZE_NATIVE, ROT_SHL, 0);
281 gen_one(R_CONST_HELPER);
286 gen_insn(INSN_ROT, OP_SIZE_NATIVE, ROT_SHL, 0);
287 gen_one(R_CONST_HELPER);
288 gen_one(R_CONST_HELPER);
294 if (cl < 0 || cl >= 4096) {
295 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
298 gen_eight(cl & 0xFFFFFC00UL);
301 gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_OR, 0);
308 gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_OR, 0);
315 if (c >= 0x100000000ULL) {
316 gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_OR, 0);
319 gen_one(R_CONST_HELPER);
325 static bool attr_w gen_address(struct codegen_context *ctx, unsigned base, int64_t imm, unsigned purpose, unsigned size)
327 ctx->base_reg = base;
328 ctx->offset_imm = imm;
329 ctx->offset_reg = false;
331 case IMM_PURPOSE_LDR_OFFSET:
332 case IMM_PURPOSE_LDR_SX_OFFSET:
333 case IMM_PURPOSE_STR_OFFSET:
334 case IMM_PURPOSE_LDP_STP_OFFSET:
335 case IMM_PURPOSE_VLDR_VSTR_OFFSET:
336 case IMM_PURPOSE_MVI_CLI_OFFSET:
337 if (likely(imm >= -4096) && likely(imm < 4096))
341 internal(file_line, "gen_address: invalid purpose %u (imm %"PRIxMAX", size %u)", purpose, (uintmax_t)imm, size);
344 g(gen_load_constant(ctx, R_OFFSET_IMM, imm));
345 ctx->offset_reg = true;
349 static bool is_direct_const(int64_t imm, unsigned purpose, unsigned size)
352 case IMM_PURPOSE_STORE_VALUE:
356 case IMM_PURPOSE_ADD:
357 case IMM_PURPOSE_SUB:
358 case IMM_PURPOSE_CMP:
359 case IMM_PURPOSE_CMP_LOGICAL:
360 case IMM_PURPOSE_AND:
362 case IMM_PURPOSE_XOR:
363 case IMM_PURPOSE_ANDN:
364 case IMM_PURPOSE_TEST:
365 case IMM_PURPOSE_MUL:
366 if (likely(imm >= -4096) && likely(imm < 4096))
369 case IMM_PURPOSE_CMOV:
370 if (likely(imm >= -1024) && likely(imm < 1024))
373 case IMM_PURPOSE_MOVR:
374 if (likely(imm >= -512) && likely(imm < 512))
378 internal(file_line, "is_direct_const: invalid purpose %u (imm %"PRIxMAX", size %u)", purpose, (uintmax_t)imm, size);
383 static bool attr_w gen_imm(struct codegen_context *ctx, int64_t imm, unsigned purpose, unsigned size)
385 if (is_direct_const(imm, purpose, size)) {
386 ctx->const_imm = imm;
387 ctx->const_reg = false;
389 g(gen_load_constant(ctx, R_CONST_IMM, imm));
390 ctx->const_reg = true;
395 static bool attr_w gen_entry(struct codegen_context *ctx)
397 gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_SAVE, 0);
401 gen_eight(-FRAME_SIZE);
403 gen_insn(INSN_JMP_INDIRECT, 0, 0, 0);
409 static bool attr_w gen_escape_arg(struct codegen_context *ctx, ip_t ip, uint32_t escape_label)
411 g(gen_load_constant(ctx, R_I1, ip));
413 gen_insn(INSN_JMP, 0, 0, 0);
414 gen_four(escape_label);
419 static bool attr_w gen_escape(struct codegen_context *ctx)
421 gen_insn(INSN_RET, 0, 0, 0);
426 static bool attr_w gen_upcall_argument(struct codegen_context attr_unused *ctx, unsigned attr_unused arg)
431 static bool attr_w gen_upcall(struct codegen_context *ctx, unsigned offset, unsigned n_args)
433 g(gen_address(ctx, R_UPCALL, offset, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_ADDRESS));
434 gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
435 gen_one(R_SCRATCH_NA_1);
436 gen_address_offset();
438 gen_insn(INSN_CALL_INDIRECT, OP_SIZE_NATIVE, 0, 0);
439 gen_one(R_SCRATCH_NA_1);
441 g(gen_upcall_end(ctx, n_args));
446 static bool attr_w gen_timestamp_test(struct codegen_context *ctx, uint32_t escape_label)
448 g(gen_address(ctx, R_UPCALL, offsetof(struct cg_upcall_vector_s, ts), IMM_PURPOSE_LDR_OFFSET, OP_SIZE_4));
449 gen_insn(INSN_MOV, OP_SIZE_4, 0, 0);
450 gen_one(R_SCRATCH_1);
451 gen_address_offset();
453 gen_insn(INSN_CMP, OP_SIZE_4, 0, 1);
454 gen_one(R_SCRATCH_1);
455 gen_one(R_TIMESTAMP);
457 gen_insn(INSN_JMP_COND, OP_SIZE_4, COND_NE, 0);
458 gen_four(escape_label);