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 PA_20 cpu_test_feature(CPU_FEATURE_pa20)
21 #if defined(ARCH_PARISC64)
22 #define ARCH_PARISC_USE_STUBS
24 #define ARCH_PARISC_USE_STUBS
27 #define OP_SIZE_NATIVE (PA_20 ? OP_SIZE_8 : OP_SIZE_4)
30 #define OP_SIZE_ADDRESS OP_SIZE_4
32 #define OP_SIZE_ADDRESS OP_SIZE_8
35 #define JMP_LIMIT JMP_EXTRA_LONG
37 #define UNALIGNED_TRAP 1
39 #define ALU_WRITES_FLAGS(alu, im) ((alu) == ALU_ADC || (alu) == ALU_SUB || (alu) == ALU_SBB ? 3 : 0)
40 #define ALU1_WRITES_FLAGS(alu) ((alu) == ALU1_NEG || (alu) == ALU1_INC || (alu) == ALU1_DEC ? 3 : 0)
41 #define ROT_WRITES_FLAGS(alu, size, im) 0
42 #define COND_IS_LOGICAL(cond) 0
44 #define ARCH_PARTIAL_ALU(size) 0
45 #define ARCH_IS_3ADDRESS(alu, f) 1
46 #define ARCH_IS_3ADDRESS_IMM(alu, f) 1
47 #define ARCH_IS_3ADDRESS_ROT(alu, size) 1
48 #define ARCH_IS_3ADDRESS_ROT_IMM(alu) 1
49 #define ARCH_IS_2ADDRESS(alu) 1
50 #define ARCH_IS_3ADDRESS_FP 1
51 #define ARCH_HAS_JMP_2REGS(cond) 1
52 #define ARCH_HAS_FLAGS 0
53 #define ARCH_SUPPORTS_TRAPS 1
54 #define ARCH_TRAP_BEFORE 0
55 #define ARCH_PREFERS_SX(size) 0
56 #define ARCH_HAS_BWX 1
57 #define ARCH_HAS_MUL 0
58 #define ARCH_HAS_DIV 0
59 #define ARCH_HAS_ANDN 1
60 #define ARCH_HAS_SHIFTED_ADD(bits) ((bits) <= 3)
61 #define ARCH_HAS_BTX(btx, size, cnst) (((btx) == BTX_BTS || (btx) == BTX_BTR || (btx) == BTX_BTEXT) && (((size) >= OP_SIZE_4)))
62 #define ARCH_SHIFT_SIZE OP_SIZE_4
63 #define ARCH_HAS_FP_GP_MOV 0
64 #define ARCH_NEEDS_BARRIER 0
66 #define i_size(size) OP_SIZE_NATIVE
67 #define i_size_rot(size) maximum(size, OP_SIZE_4)
68 #define i_size_cmp(size) maximum(size, OP_SIZE_4)
103 #define R_FSTATUS 0x20
135 #define R_TIMESTAMP R_5
137 #define R_SCRATCH_1 R_26
138 #define R_SCRATCH_2 R_25
139 #define R_SCRATCH_3 R_24
140 #define R_SCRATCH_4 R_SAVED_2
142 #define R_SCRATCH_NA_1 R_22
143 #define R_SCRATCH_NA_2 R_21
144 #ifdef HAVE_BITWISE_FRAME
145 #define R_SCRATCH_NA_3 R_20
147 #define R_CMP_RESULT R_19
149 #define R_CG_SCRATCH R_31
151 #define R_SAVED_1 R_6
152 #define R_SAVED_2 R_7
159 #define R_CONST_IMM R_1
160 #define R_OFFSET_IMM R_RP
162 #define FR_SCRATCH_1 R_F4
163 #define FR_SCRATCH_2 R_F5
165 #define SUPPORTED_FP 0x6
168 #define FRAME_SIZE 0x80
170 * 0-64 - register save area
171 * 64-96 - outgoing parameters
172 * 96-128 - frame marker
175 #define FRAME_SIZE 0xd0
177 * 0-128 - register save area
179 * 128-192 - outgoing parameters
180 * 192-208 - frame marker
185 #define RP_OFFS -0x14
187 #define RP_OFFS -0x10
190 static bool reg_is_fp(unsigned reg)
192 return reg >= R_FSTATUS && reg < R_F31;
195 static const uint8_t regs_saved[] = {
196 #if !(defined(ARCH_PARISC32) && defined(__HP_cc))
199 R_9, R_10, R_11, R_12, R_13, R_14, R_15, R_16, R_17, R_18 };
200 static const uint8_t regs_volatile[] = { R_23, R_RET1,
201 #if defined(ARCH_PARISC64)
204 #ifndef HAVE_BITWISE_FRAME
208 static const uint8_t fp_saved[] = { 0 };
209 #define n_fp_saved 0U
210 static const uint8_t fp_volatile[] = { R_F6, R_F7, R_F8, R_F9, R_F10, R_F11, R_F22, R_F23, R_F24, R_F25, R_F26, R_F27, R_F28, R_F29, R_F30, R_F31 };
211 #define reg_is_saved(r) ((r) >= R_3 && (r) <= R_18)
213 static bool attr_w gen_load_constant(struct codegen_context *ctx, unsigned reg, uint64_t c)
216 int32_t c1, c2, c3, c4;
222 c2 = (c & 0xffffc000ULL) >> 14;
233 c4 = (c & 0xffffc000ULL) >> 14;
236 if (OP_SIZE_NATIVE == OP_SIZE_8) {
238 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
241 gen_eight((uint64_t)c4 << 14);
245 gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_ADD, 0);
253 gen_insn(INSN_ROT, OP_SIZE_NATIVE, ROT_SHL, 0);
262 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
265 gen_eight((uint64_t)c2 << 14);
268 gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_ADD, 0);
269 gen_one(R_CONST_IMM);
272 gen_eight((uint64_t)c2 << 14);
276 if (c1 || r != reg) {
277 gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_ADD, 0);
286 static bool is_direct_const(int64_t imm, unsigned purpose, unsigned size);
288 static bool attr_w gen_address(struct codegen_context *ctx, unsigned base, int64_t imm, unsigned purpose, unsigned size)
290 ctx->base_reg = base;
291 ctx->offset_imm = imm;
292 ctx->offset_reg = false;
294 case IMM_PURPOSE_LDR_OFFSET:
295 case IMM_PURPOSE_LDR_SX_OFFSET:
296 case IMM_PURPOSE_STR_OFFSET:
297 case IMM_PURPOSE_MVI_CLI_OFFSET:
298 if (size == OP_SIZE_8) {
302 if (likely(imm >= -0x2000) && likely(imm < 0x2000))
305 case IMM_PURPOSE_VLDR_VSTR_OFFSET:
306 if (likely(imm >= -0x10) && likely(imm < 0x10))
310 if (unlikely((imm & ((1 << size) - 1)) != 0))
312 if (likely(imm >= -0x2000) && likely(imm < 0x2000))
316 internal(file_line, "gen_address: invalid purpose %u (imm %"PRIxMAX", size %u)", purpose, (uintmax_t)imm, size);
319 if (is_direct_const(imm, IMM_PURPOSE_ADD, OP_SIZE_ADDRESS)) {
320 gen_insn(INSN_ALU, OP_SIZE_ADDRESS, ALU_ADD, 0);
321 gen_one(R_OFFSET_IMM);
326 ctx->base_reg = R_OFFSET_IMM;
332 g(gen_load_constant(ctx, R_OFFSET_IMM, imm));
334 if (purpose == IMM_PURPOSE_LDR_OFFSET || purpose == IMM_PURPOSE_LDR_SX_OFFSET) {
335 ctx->offset_reg = true;
339 gen_insn(INSN_ALU, OP_SIZE_ADDRESS, ALU_ADD, 0);
340 gen_one(R_OFFSET_IMM);
341 gen_one(R_OFFSET_IMM);
344 ctx->base_reg = R_OFFSET_IMM;
350 static bool is_direct_const(int64_t imm, unsigned purpose, unsigned size)
352 int64_t imm_copy = imm;
354 case IMM_PURPOSE_STORE_VALUE:
358 case IMM_PURPOSE_SUB:
359 imm_copy = -(uint64_t)imm_copy;
361 case IMM_PURPOSE_ADD:
362 case IMM_PURPOSE_CMP:
363 case IMM_PURPOSE_CMP_LOGICAL:
364 case IMM_PURPOSE_MOVR:
365 if (likely(imm_copy >= -1024) && likely(imm_copy < 1024))
368 case IMM_PURPOSE_JMP_2REGS:
370 if (likely(imm >= -16) && likely(imm < 16))
374 case IMM_PURPOSE_AND:
376 case IMM_PURPOSE_XOR:
377 case IMM_PURPOSE_ANDN:
378 case IMM_PURPOSE_TEST:
382 case IMM_PURPOSE_BITWISE:
385 internal(file_line, "is_direct_const: invalid purpose %u (imm %"PRIxMAX", size %u)", purpose, (uintmax_t)imm, size);
390 static bool attr_w gen_imm(struct codegen_context *ctx, int64_t imm, unsigned purpose, unsigned size)
392 if (is_direct_const(imm, purpose, size)) {
393 ctx->const_imm = imm;
394 ctx->const_reg = false;
396 g(gen_load_constant(ctx, R_CONST_IMM, imm));
397 ctx->const_reg = true;
402 static bool attr_w gen_entry(struct codegen_context *ctx)
406 gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
407 gen_one(ARG_ADDRESS_1);
412 gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
413 gen_one(ARG_ADDRESS_1_POST_I);
415 gen_eight(FRAME_SIZE);
418 for (i = R_4; i <= R_18; i++) {
419 int offs = -FRAME_SIZE + ((i - R_3) << OP_SIZE_ADDRESS);
420 gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
421 gen_one(ARG_ADDRESS_1);
427 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
431 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
435 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
436 gen_one(R_TIMESTAMP);
439 gen_insn(INSN_JMP_INDIRECT, 0, 0, 0);
445 static bool attr_w gen_escape_arg(struct codegen_context *ctx, ip_t ip, uint32_t escape_label)
447 g(gen_load_constant(ctx, R_SCRATCH_1, ip));
449 gen_insn(INSN_JMP, 0, 0, 0);
450 gen_four(escape_label);
455 static bool attr_w gen_escape(struct codegen_context *ctx)
459 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
463 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
465 gen_one(R_SCRATCH_1);
467 gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
469 gen_one(ARG_ADDRESS_1);
471 gen_eight(-FRAME_SIZE + RP_OFFS);
473 for (i = R_4; i <= R_18; i++) {
474 int offs = -FRAME_SIZE + ((i - R_3) << OP_SIZE_ADDRESS);
475 gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
477 gen_one(ARG_ADDRESS_1);
482 gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
484 gen_one(ARG_ADDRESS_1_PRE_I);
486 gen_eight(-FRAME_SIZE);
488 gen_insn(INSN_RET, 0, 0, 0);
493 static bool attr_w gen_upcall_argument(struct codegen_context attr_unused *ctx, unsigned attr_unused arg)
498 static bool attr_w gen_upcall(struct codegen_context *ctx, unsigned offset, unsigned n_args)
501 uint32_t label = alloc_call_label(ctx);
502 if (unlikely(!label))
505 g(gen_address(ctx, R_UPCALL, offset, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
506 gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
507 gen_one(R_SCRATCH_NA_1);
508 gen_address_offset();
510 gen_insn(INSN_CALL, 0, 0, 0);
513 g(gen_address(ctx, R_UPCALL, offset, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_ADDRESS));
514 gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
516 gen_address_offset();
518 gen_insn(INSN_CALL_INDIRECT, OP_SIZE_8, 0, 0);
521 g(gen_upcall_end(ctx, n_args));
526 static bool attr_w gen_call_millicode(struct codegen_context *ctx)
528 gen_insn(INSN_CALL_MILLICODE, 0, 0, 0);
532 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);
534 static bool attr_w gen_timestamp_test(struct codegen_context *ctx, uint32_t escape_label)
536 g(gen_address(ctx, R_UPCALL, offsetof(struct cg_upcall_vector_s, ts), IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
537 gen_insn(INSN_MOV, OP_SIZE_4, 0, 0);
538 gen_one(R_SCRATCH_1);
539 gen_address_offset();
541 g(gen_cmp_test_jmp(ctx, INSN_CMP, OP_SIZE_4, R_SCRATCH_1, R_TIMESTAMP, COND_NE, escape_label));