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_FLAGS 0
52 #define ARCH_SUPPORTS_TRAPS 1
53 #define ARCH_TRAP_BEFORE 0
54 #define ARCH_PREFERS_SX(size) 0
55 #define ARCH_HAS_BWX 1
56 #define ARCH_HAS_MUL 0
57 #define ARCH_HAS_DIV 0
58 #define ARCH_HAS_ANDN 1
59 #define ARCH_HAS_SHIFTED_ADD(bits) ((bits) <= 3)
60 #define ARCH_HAS_BTX(btx, size, cnst) (((btx) == BTX_BTS || (btx) == BTX_BTR || (btx) == BTX_BTEXT) && (((size) >= OP_SIZE_4)))
61 #define ARCH_SHIFT_SIZE OP_SIZE_4
62 #define ARCH_NEEDS_BARRIER 0
64 #define i_size(size) OP_SIZE_NATIVE
65 #define i_size_rot(size) maximum(size, OP_SIZE_4)
66 #define i_size_cmp(size) maximum(size, OP_SIZE_4)
101 #define R_FSTATUS 0x20
133 #define R_TIMESTAMP R_5
135 #define R_SCRATCH_1 R_26
136 #define R_SCRATCH_2 R_25
137 #define R_SCRATCH_3 R_24
138 #define R_SCRATCH_4 R_SAVED_2
140 #define R_SCRATCH_NA_1 R_22
141 #define R_SCRATCH_NA_2 R_21
142 #ifdef HAVE_BITWISE_FRAME
143 #define R_SCRATCH_NA_3 R_20
145 #define R_CMP_RESULT R_19
147 #define R_CG_SCRATCH R_31
149 #define R_SAVED_1 R_6
150 #define R_SAVED_2 R_7
157 #define R_CONST_IMM R_1
158 #define R_OFFSET_IMM R_RP
160 #define FR_SCRATCH_1 R_F4
161 #define FR_SCRATCH_2 R_F5
163 #define SUPPORTED_FP 0x6
166 #define FRAME_SIZE 0x80
168 * 0-64 - register save area
169 * 64-96 - outgoing parameters
170 * 96-128 - frame marker
173 #define FRAME_SIZE 0xd0
175 * 0-128 - register save area
177 * 128-192 - outgoing parameters
178 * 192-208 - frame marker
183 #define RP_OFFS -0x14
185 #define RP_OFFS -0x10
188 static bool reg_is_fp(unsigned reg)
190 return reg >= R_FSTATUS && reg < R_F31;
193 static const uint8_t regs_saved[] = {
194 #if !(defined(ARCH_PARISC32) && defined(__HP_cc))
197 R_9, R_10, R_11, R_12, R_13, R_14, R_15, R_16, R_17, R_18 };
198 static const uint8_t regs_volatile[] = { R_23, R_RET1,
199 #if defined(ARCH_PARISC64)
202 #ifndef HAVE_BITWISE_FRAME
206 static const uint8_t fp_saved[] = { 0 };
207 #define n_fp_saved 0U
208 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 };
209 #define reg_is_saved(r) ((r) >= R_3 && (r) <= R_18)
211 static bool attr_w gen_load_constant(struct codegen_context *ctx, unsigned reg, uint64_t c)
214 int32_t c1, c2, c3, c4;
220 c2 = (c & 0xffffc000ULL) >> 14;
231 c4 = (c & 0xffffc000ULL) >> 14;
234 if (OP_SIZE_NATIVE == OP_SIZE_8) {
236 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
239 gen_eight((uint64_t)c4 << 14);
243 gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_ADD, 0);
251 gen_insn(INSN_ROT, OP_SIZE_NATIVE, ROT_SHL, 0);
260 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
263 gen_eight((uint64_t)c2 << 14);
266 gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_ADD, 0);
267 gen_one(R_CONST_IMM);
270 gen_eight((uint64_t)c2 << 14);
274 if (c1 || r != reg) {
275 gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_ADD, 0);
284 static bool is_direct_const(int64_t imm, unsigned purpose, unsigned size);
286 static bool attr_w gen_address(struct codegen_context *ctx, unsigned base, int64_t imm, unsigned purpose, unsigned size)
288 ctx->base_reg = base;
289 ctx->offset_imm = imm;
290 ctx->offset_reg = false;
292 case IMM_PURPOSE_LDR_OFFSET:
293 case IMM_PURPOSE_LDR_SX_OFFSET:
294 case IMM_PURPOSE_STR_OFFSET:
295 case IMM_PURPOSE_MVI_CLI_OFFSET:
296 if (size == OP_SIZE_8) {
300 if (likely(imm >= -0x2000) && likely(imm < 0x2000))
303 case IMM_PURPOSE_VLDR_VSTR_OFFSET:
304 if (likely(imm >= -0x10) && likely(imm < 0x10))
308 if (unlikely((imm & ((1 << size) - 1)) != 0))
310 if (likely(imm >= -0x2000) && likely(imm < 0x2000))
314 internal(file_line, "gen_address: invalid purpose %u (imm %"PRIxMAX", size %u)", purpose, (uintmax_t)imm, size);
317 if (is_direct_const(imm, IMM_PURPOSE_ADD, OP_SIZE_ADDRESS)) {
318 gen_insn(INSN_ALU, OP_SIZE_ADDRESS, ALU_ADD, 0);
319 gen_one(R_OFFSET_IMM);
324 ctx->base_reg = R_OFFSET_IMM;
330 g(gen_load_constant(ctx, R_OFFSET_IMM, imm));
332 if (purpose == IMM_PURPOSE_LDR_OFFSET || purpose == IMM_PURPOSE_LDR_SX_OFFSET) {
333 ctx->offset_reg = true;
337 gen_insn(INSN_ALU, OP_SIZE_ADDRESS, ALU_ADD, 0);
338 gen_one(R_OFFSET_IMM);
339 gen_one(R_OFFSET_IMM);
342 ctx->base_reg = R_OFFSET_IMM;
348 static bool is_direct_const(int64_t imm, unsigned purpose, unsigned size)
350 int64_t imm_copy = imm;
352 case IMM_PURPOSE_STORE_VALUE:
356 case IMM_PURPOSE_SUB:
357 imm_copy = -(uint64_t)imm_copy;
359 case IMM_PURPOSE_ADD:
360 case IMM_PURPOSE_CMP:
361 case IMM_PURPOSE_CMP_LOGICAL:
362 case IMM_PURPOSE_MOVR:
363 if (likely(imm_copy >= -1024) && likely(imm_copy < 1024))
366 case IMM_PURPOSE_JMP_2REGS:
368 if (likely(imm >= -16) && likely(imm < 16))
372 case IMM_PURPOSE_AND:
374 case IMM_PURPOSE_XOR:
375 case IMM_PURPOSE_ANDN:
376 case IMM_PURPOSE_TEST:
380 case IMM_PURPOSE_BITWISE:
383 internal(file_line, "is_direct_const: invalid purpose %u (imm %"PRIxMAX", size %u)", purpose, (uintmax_t)imm, size);
388 static bool attr_w gen_imm(struct codegen_context *ctx, int64_t imm, unsigned purpose, unsigned size)
390 if (is_direct_const(imm, purpose, size)) {
391 ctx->const_imm = imm;
392 ctx->const_reg = false;
394 g(gen_load_constant(ctx, R_CONST_IMM, imm));
395 ctx->const_reg = true;
400 static bool attr_w gen_entry(struct codegen_context *ctx)
404 gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
405 gen_one(ARG_ADDRESS_1);
410 gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
411 gen_one(ARG_ADDRESS_1_POST_I);
413 gen_eight(FRAME_SIZE);
416 for (i = R_4; i <= R_18; i++) {
417 int offs = -FRAME_SIZE + ((i - R_3) << OP_SIZE_ADDRESS);
418 gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
419 gen_one(ARG_ADDRESS_1);
425 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
429 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
433 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
434 gen_one(R_TIMESTAMP);
437 gen_insn(INSN_JMP_INDIRECT, 0, 0, 0);
443 static bool attr_w gen_escape_arg(struct codegen_context *ctx, ip_t ip, uint32_t escape_label)
445 g(gen_load_constant(ctx, R_SCRATCH_1, ip));
447 gen_insn(INSN_JMP, 0, 0, 0);
448 gen_four(escape_label);
453 static bool attr_w gen_escape(struct codegen_context *ctx)
457 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
461 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
463 gen_one(R_SCRATCH_1);
465 gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
467 gen_one(ARG_ADDRESS_1);
469 gen_eight(-FRAME_SIZE + RP_OFFS);
471 for (i = R_4; i <= R_18; i++) {
472 int offs = -FRAME_SIZE + ((i - R_3) << OP_SIZE_ADDRESS);
473 gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
475 gen_one(ARG_ADDRESS_1);
480 gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
482 gen_one(ARG_ADDRESS_1_PRE_I);
484 gen_eight(-FRAME_SIZE);
486 gen_insn(INSN_RET, 0, 0, 0);
491 static bool attr_w gen_upcall_argument(struct codegen_context attr_unused *ctx, unsigned attr_unused arg)
496 static bool attr_w gen_upcall(struct codegen_context *ctx, unsigned offset, unsigned n_args)
499 uint32_t label = alloc_call_label(ctx);
500 if (unlikely(!label))
503 g(gen_address(ctx, R_UPCALL, offset, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
504 gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
505 gen_one(R_SCRATCH_NA_1);
506 gen_address_offset();
508 gen_insn(INSN_CALL, 0, 0, 0);
511 g(gen_address(ctx, R_UPCALL, offset, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_ADDRESS));
512 gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
514 gen_address_offset();
516 gen_insn(INSN_CALL_INDIRECT, OP_SIZE_8, 0, 0);
519 g(gen_upcall_end(ctx, n_args));
524 static bool attr_w gen_call_millicode(struct codegen_context *ctx)
526 gen_insn(INSN_CALL_MILLICODE, 0, 0, 0);
530 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);
532 static bool attr_w gen_timestamp_test(struct codegen_context *ctx, uint32_t escape_label)
534 g(gen_address(ctx, R_UPCALL, offsetof(struct cg_upcall_vector_s, ts), IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
535 gen_insn(INSN_MOV, OP_SIZE_4, 0, 0);
536 gen_one(R_SCRATCH_1);
537 gen_address_offset();
539 g(gen_cmp_test_jmp(ctx, INSN_CMP, OP_SIZE_4, R_SCRATCH_1, R_TIMESTAMP, COND_NE, escape_label));