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) 0
38 #define COND_IS_LOGICAL(cond) 0
40 #define ARCH_PARTIAL_ALU(size) 0
41 #define ARCH_IS_3ADDRESS 1
42 #define ARCH_HAS_FLAGS 1
43 #define ARCH_PREFERS_SX(size) 0
44 #define ARCH_HAS_BWX 1
45 #define ARCH_HAS_MUL SPARC_9
46 #define ARCH_HAS_DIV SPARC_9
47 #define ARCH_HAS_ANDN 1
48 #define ARCH_HAS_SHIFTED_ADD(bits) 0
49 #define ARCH_HAS_BTX(btx, size, cnst) 0
50 #define ARCH_SHIFT_SIZE OP_SIZE_4
51 #define ARCH_NEEDS_BARRIER 0
53 #define i_size(size) OP_SIZE_NATIVE
54 #define i_size_rot(size) maximum(size, OP_SIZE_4)
176 #define R_UPCALL R_I1
177 #define R_TIMESTAMP R_I2
179 #define R_SCRATCH_1 R_O1
180 #define R_SCRATCH_2 R_O0
181 #define R_SCRATCH_3 R_O3
182 #define R_SCRATCH_4 R_O2
184 #define R_SCRATCH_NA_1 R_O4
185 #define R_SCRATCH_NA_2 R_O5
186 #define R_SCRATCH_NA_3 R_O7
188 #define R_SAVED_1 R_L0
189 #define R_SAVED_2 R_L1
197 #define FR_SCRATCH_1 FDR_0
198 #define FR_SCRATCH_2 FDR_4
200 #define R_OFFSET_IMM R_G1
201 #define R_CONST_IMM R_G2
202 #define R_CONST_HELPER R_G3
204 #define SUPPORTED_FP 0x6
206 static bool reg_is_fp(unsigned reg)
208 return reg >= 0x20 && reg < 0x40;
211 static const uint8_t reg_available[] = { 0 };
212 #define reg_is_saved(r) 1
214 static bool attr_w gen_load_constant(struct codegen_context *ctx, unsigned reg, uint64_t c)
218 if (c >= (uint64_t)-4096) {
219 gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_OR, 0);
226 if (c >= 0x100000000ULL) {
227 int32_t cu = c >> 32;
228 if (cu < -4096 || cu >= 4096) {
229 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
230 gen_one(R_CONST_HELPER);
232 gen_eight(cu & 0xFFFFFC00UL);
235 gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_OR, 0);
236 gen_one(R_CONST_HELPER);
237 gen_one(R_CONST_HELPER);
242 gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_OR, 0);
243 gen_one(R_CONST_HELPER);
248 gen_insn(INSN_ROT, OP_SIZE_NATIVE, ROT_SHL, 0);
249 gen_one(R_CONST_HELPER);
250 gen_one(R_CONST_HELPER);
256 if (cl < 0 || cl >= 4096) {
257 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
260 gen_eight(cl & 0xFFFFFC00UL);
263 gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_OR, 0);
270 gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_OR, 0);
277 if (c >= 0x100000000ULL) {
278 gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_OR, 0);
281 gen_one(R_CONST_HELPER);
287 static bool attr_w gen_address(struct codegen_context *ctx, unsigned base, int64_t imm, unsigned purpose, unsigned size)
289 ctx->base_reg = base;
290 ctx->offset_imm = imm;
291 ctx->offset_reg = false;
293 case IMM_PURPOSE_LDR_OFFSET:
294 case IMM_PURPOSE_LDR_SX_OFFSET:
295 case IMM_PURPOSE_STR_OFFSET:
296 case IMM_PURPOSE_LDP_STP_OFFSET:
297 case IMM_PURPOSE_VLDR_VSTR_OFFSET:
298 case IMM_PURPOSE_MVI_CLI_OFFSET:
299 if (likely(imm >= -4096) && likely(imm < 4096))
303 internal(file_line, "gen_address: invalid purpose %u (imm %"PRIxMAX", size %u)", purpose, (uintmax_t)imm, size);
306 g(gen_load_constant(ctx, R_OFFSET_IMM, imm));
307 ctx->offset_reg = true;
311 static bool is_direct_const(int64_t imm, unsigned purpose, unsigned size)
314 case IMM_PURPOSE_STORE_VALUE:
318 case IMM_PURPOSE_ADD:
319 case IMM_PURPOSE_SUB:
320 case IMM_PURPOSE_CMP:
321 case IMM_PURPOSE_CMP_LOGICAL:
322 case IMM_PURPOSE_AND:
324 case IMM_PURPOSE_XOR:
325 case IMM_PURPOSE_ANDN:
326 case IMM_PURPOSE_TEST:
327 case IMM_PURPOSE_MUL:
328 if (likely(imm >= -4096) && likely(imm < 4096))
331 case IMM_PURPOSE_CMOV:
332 if (likely(imm >= -1024) && likely(imm < 1024))
335 case IMM_PURPOSE_MOVR:
336 if (likely(imm >= -512) && likely(imm < 512))
340 internal(file_line, "is_direct_const: invalid purpose %u (imm %"PRIxMAX", size %u)", purpose, (uintmax_t)imm, size);
345 static bool attr_w gen_imm(struct codegen_context *ctx, int64_t imm, unsigned purpose, unsigned size)
347 if (is_direct_const(imm, purpose, size)) {
348 ctx->const_imm = imm;
349 ctx->const_reg = false;
351 g(gen_load_constant(ctx, R_CONST_IMM, imm));
352 ctx->const_reg = true;
357 static bool attr_w gen_entry(struct codegen_context *ctx)
359 gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_SAVE, 0);
363 gen_eight(-FRAME_SIZE);
365 gen_insn(INSN_JMP_INDIRECT, 0, 0, 0);
371 static bool attr_w gen_escape_arg(struct codegen_context *ctx, ip_t ip, uint32_t escape_label)
373 g(gen_load_constant(ctx, R_I1, ip));
375 gen_insn(INSN_JMP, 0, 0, 0);
376 gen_four(escape_label);
381 static bool attr_w gen_escape(struct codegen_context *ctx)
383 gen_insn(INSN_RET, 0, 0, 0);
388 static bool attr_w gen_upcall_argument(struct codegen_context attr_unused *ctx, unsigned attr_unused arg)
393 static bool attr_w gen_upcall(struct codegen_context *ctx, unsigned offset, unsigned n_args)
395 g(gen_address(ctx, R_UPCALL, offset, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_ADDRESS));
396 gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
397 gen_one(R_SCRATCH_NA_1);
398 gen_address_offset();
400 gen_insn(INSN_CALL_INDIRECT, OP_SIZE_NATIVE, 0, 0);
401 gen_one(R_SCRATCH_NA_1);
403 g(gen_upcall_end(ctx, n_args));
408 static bool attr_w gen_timestamp_test(struct codegen_context *ctx, uint32_t escape_label)
410 g(gen_address(ctx, R_UPCALL, offsetof(struct cg_upcall_vector_s, ts), IMM_PURPOSE_LDR_OFFSET, OP_SIZE_4));
411 gen_insn(INSN_MOV, OP_SIZE_4, 0, 0);
412 gen_one(R_SCRATCH_1);
413 gen_address_offset();
415 gen_insn(INSN_CMP, OP_SIZE_4, 0, 1);
416 gen_one(R_SCRATCH_1);
417 gen_one(R_TIMESTAMP);
419 gen_insn(INSN_JMP_COND, OP_SIZE_4, COND_NE, 0);
420 gen_four(escape_label);