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_PREFERS_SX(size) 0
54 #define ARCH_HAS_BWX 1
55 #define ARCH_HAS_MUL 0
56 #define ARCH_HAS_DIV 0
57 #define ARCH_HAS_ANDN 1
58 #define ARCH_HAS_SHIFTED_ADD(bits) ((bits) <= 3)
59 #define ARCH_HAS_BTX(btx, size, cnst) (((btx) == BTX_BTS || (btx) == BTX_BTR || (btx) == BTX_BTEXT) && (((size) >= OP_SIZE_4)))
60 #define ARCH_SHIFT_SIZE OP_SIZE_4
61 #define ARCH_NEEDS_BARRIER 0
63 #define i_size(size) OP_SIZE_NATIVE
64 #define i_size_rot(size) maximum(size, OP_SIZE_4)
65 #define i_size_cmp(size) maximum(size, OP_SIZE_4)
100 #define R_FSTATUS 0x20
132 #define R_TIMESTAMP R_5
134 #define R_SCRATCH_1 R_26
135 #define R_SCRATCH_2 R_25
136 #define R_SCRATCH_3 R_24
137 #define R_SCRATCH_4 R_SAVED_2
139 #define R_SCRATCH_NA_1 R_22
140 #define R_SCRATCH_NA_2 R_21
141 #ifdef HAVE_BITWISE_FRAME
142 #define R_SCRATCH_NA_3 R_20
144 #define R_CMP_RESULT R_19
146 #define R_CG_SCRATCH R_31
148 #define R_SAVED_1 R_6
149 #define R_SAVED_2 R_7
156 #define R_CONST_IMM R_1
157 #define R_OFFSET_IMM R_RP
159 #define FR_SCRATCH_1 R_F4
160 #define FR_SCRATCH_2 R_F5
162 #define SUPPORTED_FP 0x6
165 #define FRAME_SIZE 0x80
167 * 0-64 - register save area
168 * 64-96 - outgoing parameters
169 * 96-128 - frame marker
172 #define FRAME_SIZE 0xd0
174 * 0-128 - register save area
176 * 128-192 - outgoing parameters
177 * 192-208 - frame marker
182 #define RP_OFFS -0x14
184 #define RP_OFFS -0x10
187 static bool reg_is_fp(unsigned reg)
189 return reg >= R_FSTATUS && reg < R_F31;
192 static const uint8_t regs_saved[] = {
193 #if !(defined(ARCH_PARISC32) && defined(__HP_cc))
196 R_9, R_10, R_11, R_12, R_13, R_14, R_15, R_16, R_17, R_18 };
197 static const uint8_t regs_volatile[] = { R_23, R_RET1,
198 #if defined(ARCH_PARISC64)
201 #ifndef HAVE_BITWISE_FRAME
205 static const uint8_t fp_saved[] = { 0 };
206 #define n_fp_saved 0U
207 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 };
208 #define reg_is_saved(r) ((r) >= R_3 && (r) <= R_18)
210 static bool attr_w gen_load_constant(struct codegen_context *ctx, unsigned reg, uint64_t c)
213 int32_t c1, c2, c3, c4;
219 c2 = (c & 0xffffc000ULL) >> 14;
230 c4 = (c & 0xffffc000ULL) >> 14;
233 if (OP_SIZE_NATIVE == OP_SIZE_8) {
235 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
238 gen_eight((uint64_t)c4 << 14);
242 gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_ADD, 0);
250 gen_insn(INSN_ROT, OP_SIZE_NATIVE, ROT_SHL, 0);
259 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
262 gen_eight((uint64_t)c2 << 14);
265 gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_ADD, 0);
266 gen_one(R_CONST_IMM);
269 gen_eight((uint64_t)c2 << 14);
273 if (c1 || r != reg) {
274 gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_ADD, 0);
283 static bool is_direct_const(int64_t imm, unsigned purpose, unsigned size);
285 static bool attr_w gen_address(struct codegen_context *ctx, unsigned base, int64_t imm, unsigned purpose, unsigned size)
287 ctx->base_reg = base;
288 ctx->offset_imm = imm;
289 ctx->offset_reg = false;
291 case IMM_PURPOSE_LDR_OFFSET:
292 case IMM_PURPOSE_LDR_SX_OFFSET:
293 case IMM_PURPOSE_STR_OFFSET:
294 case IMM_PURPOSE_MVI_CLI_OFFSET:
295 if (size == OP_SIZE_8) {
299 if (likely(imm >= -0x2000) && likely(imm < 0x2000))
302 case IMM_PURPOSE_VLDR_VSTR_OFFSET:
303 if (likely(imm >= -0x10) && likely(imm < 0x10))
307 if (unlikely((imm & ((1 << size) - 1)) != 0))
309 if (likely(imm >= -0x2000) && likely(imm < 0x2000))
313 internal(file_line, "gen_address: invalid purpose %u (imm %"PRIxMAX", size %u)", purpose, (uintmax_t)imm, size);
316 if (is_direct_const(imm, IMM_PURPOSE_ADD, OP_SIZE_ADDRESS)) {
317 gen_insn(INSN_ALU, OP_SIZE_ADDRESS, ALU_ADD, 0);
318 gen_one(R_OFFSET_IMM);
323 ctx->base_reg = R_OFFSET_IMM;
329 g(gen_load_constant(ctx, R_OFFSET_IMM, imm));
331 if (purpose == IMM_PURPOSE_LDR_OFFSET || purpose == IMM_PURPOSE_LDR_SX_OFFSET) {
332 ctx->offset_reg = true;
336 gen_insn(INSN_ALU, OP_SIZE_ADDRESS, ALU_ADD, 0);
337 gen_one(R_OFFSET_IMM);
338 gen_one(R_OFFSET_IMM);
341 ctx->base_reg = R_OFFSET_IMM;
347 static bool is_direct_const(int64_t imm, unsigned purpose, unsigned size)
349 int64_t imm_copy = imm;
351 case IMM_PURPOSE_STORE_VALUE:
355 case IMM_PURPOSE_SUB:
356 imm_copy = -(uint64_t)imm_copy;
358 case IMM_PURPOSE_ADD:
359 case IMM_PURPOSE_CMP:
360 case IMM_PURPOSE_CMP_LOGICAL:
361 case IMM_PURPOSE_MOVR:
362 if (likely(imm_copy >= -1024) && likely(imm_copy < 1024))
365 case IMM_PURPOSE_JMP_2REGS:
367 if (likely(imm >= -16) && likely(imm < 16))
371 case IMM_PURPOSE_AND:
373 case IMM_PURPOSE_XOR:
374 case IMM_PURPOSE_ANDN:
375 case IMM_PURPOSE_TEST:
379 case IMM_PURPOSE_BITWISE:
382 internal(file_line, "is_direct_const: invalid purpose %u (imm %"PRIxMAX", size %u)", purpose, (uintmax_t)imm, size);
387 static bool attr_w gen_imm(struct codegen_context *ctx, int64_t imm, unsigned purpose, unsigned size)
389 if (is_direct_const(imm, purpose, size)) {
390 ctx->const_imm = imm;
391 ctx->const_reg = false;
393 g(gen_load_constant(ctx, R_CONST_IMM, imm));
394 ctx->const_reg = true;
399 static bool attr_w gen_entry(struct codegen_context *ctx)
403 gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
404 gen_one(ARG_ADDRESS_1);
409 gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
410 gen_one(ARG_ADDRESS_1_POST_I);
412 gen_eight(FRAME_SIZE);
415 for (i = R_4; i <= R_18; i++) {
416 int offs = -FRAME_SIZE + ((i - R_3) << OP_SIZE_ADDRESS);
417 gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
418 gen_one(ARG_ADDRESS_1);
424 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
428 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
432 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
433 gen_one(R_TIMESTAMP);
436 gen_insn(INSN_JMP_INDIRECT, 0, 0, 0);
442 static bool attr_w gen_escape_arg(struct codegen_context *ctx, ip_t ip, uint32_t escape_label)
444 g(gen_load_constant(ctx, R_SCRATCH_1, ip));
446 gen_insn(INSN_JMP, 0, 0, 0);
447 gen_four(escape_label);
452 static bool attr_w gen_escape(struct codegen_context *ctx)
456 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
460 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
462 gen_one(R_SCRATCH_1);
464 gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
466 gen_one(ARG_ADDRESS_1);
468 gen_eight(-FRAME_SIZE + RP_OFFS);
470 for (i = R_4; i <= R_18; i++) {
471 int offs = -FRAME_SIZE + ((i - R_3) << OP_SIZE_ADDRESS);
472 gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
474 gen_one(ARG_ADDRESS_1);
479 gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
481 gen_one(ARG_ADDRESS_1_PRE_I);
483 gen_eight(-FRAME_SIZE);
485 gen_insn(INSN_RET, 0, 0, 0);
490 static bool attr_w gen_upcall_argument(struct codegen_context attr_unused *ctx, unsigned attr_unused arg)
495 static bool attr_w gen_upcall(struct codegen_context *ctx, unsigned offset, unsigned n_args)
498 uint32_t label = alloc_call_label(ctx);
499 if (unlikely(!label))
502 g(gen_address(ctx, R_UPCALL, offset, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
503 gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
504 gen_one(R_SCRATCH_NA_1);
505 gen_address_offset();
507 gen_insn(INSN_CALL, 0, 0, 0);
510 g(gen_address(ctx, R_UPCALL, offset, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_ADDRESS));
511 gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
513 gen_address_offset();
515 gen_insn(INSN_CALL_INDIRECT, OP_SIZE_8, 0, 0);
518 g(gen_upcall_end(ctx, n_args));
523 static bool attr_w gen_call_millicode(struct codegen_context *ctx)
525 gen_insn(INSN_CALL_MILLICODE, 0, 0, 0);
529 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);
531 static bool attr_w gen_timestamp_test(struct codegen_context *ctx, uint32_t escape_label)
533 g(gen_address(ctx, R_UPCALL, offsetof(struct cg_upcall_vector_s, ts), IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
534 gen_insn(INSN_MOV, OP_SIZE_4, 0, 0);
535 gen_one(R_SCRATCH_1);
536 gen_address_offset();
538 g(gen_cmp_test_jmp(ctx, INSN_CMP, OP_SIZE_4, R_SCRATCH_1, R_TIMESTAMP, COND_NE, escape_label));