implement array_len_greater_than+jmp fusion
[ajla.git] / c1-hppa.inc
blob403287ee3ad990d87d7c6ea670efaf231cf1f49a
1 /*
2  * Copyright (C) 2024 Mikulas Patocka
3  *
4  * This file is part of Ajla.
5  *
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
9  * version.
10  *
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.
14  *
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/>.
17  */
19 #define PA_20                           cpu_test_feature(CPU_FEATURE_pa20)
21 #if defined(ARCH_PARISC64)
22 #define ARCH_PARISC_USE_STUBS
23 #elif defined(__hpux)
24 #define ARCH_PARISC_USE_STUBS
25 #endif
27 #define OP_SIZE_NATIVE                  (PA_20 ? OP_SIZE_8 : OP_SIZE_4)
29 #ifdef ARCH_PARISC32
30 #define OP_SIZE_ADDRESS                 OP_SIZE_4
31 #else
32 #define OP_SIZE_ADDRESS                 OP_SIZE_8
33 #endif
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)
70 #define R_ZERO          0x00
71 #define R_1             0x01
72 #define R_RP            0x02
73 #define R_3             0x03
74 #define R_4             0x04
75 #define R_5             0x05
76 #define R_6             0x06
77 #define R_7             0x07
78 #define R_8             0x08
79 #define R_9             0x09
80 #define R_10            0x0a
81 #define R_11            0x0b
82 #define R_12            0x0c
83 #define R_13            0x0d
84 #define R_14            0x0e
85 #define R_15            0x0f
86 #define R_16            0x10
87 #define R_17            0x11
88 #define R_18            0x12
89 #define R_19            0x13
90 #define R_20            0x14
91 #define R_21            0x15
92 #define R_22            0x16
93 #define R_23            0x17
94 #define R_24            0x18
95 #define R_25            0x19
96 #define R_26            0x1a
97 #define R_DP            0x1b
98 #define R_RET0          0x1c
99 #define R_RET1          0x1d
100 #define R_SP            0x1e
101 #define R_31            0x1f
103 #define R_FSTATUS       0x20
104 #define R_F4            0x24
105 #define R_F5            0x25
106 #define R_F6            0x26
107 #define R_F7            0x27
108 #define R_F8            0x28
109 #define R_F9            0x29
110 #define R_F10           0x2a
111 #define R_F11           0x2b
112 #define R_F12           0x2c
113 #define R_F13           0x2d
114 #define R_F14           0x2e
115 #define R_F15           0x2f
116 #define R_F16           0x30
117 #define R_F17           0x31
118 #define R_F18           0x32
119 #define R_F19           0x33
120 #define R_F20           0x34
121 #define R_F21           0x35
122 #define R_F22           0x36
123 #define R_F23           0x37
124 #define R_F24           0x38
125 #define R_F25           0x39
126 #define R_F26           0x3a
127 #define R_F27           0x3b
128 #define R_F28           0x3c
129 #define R_F29           0x3d
130 #define R_F30           0x3e
131 #define R_F31           0x3f
133 #define R_FRAME         R_3
134 #define R_UPCALL        R_4
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
146 #endif
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
154 #define R_ARG0          R_26
155 #define R_ARG1          R_25
156 #define R_ARG2          R_24
157 #define R_ARG3          R_23
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
167 #ifdef ARCH_PARISC32
168 #define FRAME_SIZE      0x80
170  * 0-64         - register save area
171  * 64-96        - outgoing parameters
172  * 96-128       - frame marker
173  */
174 #else
175 #define FRAME_SIZE      0xd0
177  * 0-128        - register save area
178  * 128-128      - unused
179  * 128-192      - outgoing parameters
180  * 192-208      - frame marker
181  */
182 #endif
184 #ifdef ARCH_PARISC32
185 #define RP_OFFS -0x14
186 #else
187 #define RP_OFFS -0x10
188 #endif
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))
197         R_8,
198 #endif
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)
202         R_DP,
203 #endif
204 #ifndef HAVE_BITWISE_FRAME
205         R_20,
206 #endif
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)
215         unsigned r = R_ZERO;
216         int32_t c1, c2, c3, c4;
217         c1 = c & 0x3fffULL;
218         if (c1 & 0x2000)
219                 c1 |= 0xffffc000U;
220         if (c1 < 0)
221                 c += 0x4000ULL;
222         c2 = (c & 0xffffc000ULL) >> 14;
223         if (c2 & 0x20000)
224                 c2 |= 0xfffc0000U;
225         if (c2 < 0)
226                 c += 0x100000000ULL;
227         c >>= 32;
228         c3 = c & 0x3fffULL;
229         if (c3 & 0x2000)
230                 c3 |= 0xffffc000U;
231         if (c3 < 0)
232                 c += 0x4000ULL;
233         c4 = (c & 0xffffc000ULL) >> 14;
234         if (c4 & 0x20000)
235                 c4 |= 0xfffc0000U;
236         if (OP_SIZE_NATIVE == OP_SIZE_8) {
237                 if (c4) {
238                         gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
239                         gen_one(reg);
240                         gen_one(ARG_IMM);
241                         gen_eight((uint64_t)c4 << 14);
242                         r = reg;
243                 }
244                 if (c3) {
245                         gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_ADD, 0);
246                         gen_one(reg);
247                         gen_one(r);
248                         gen_one(ARG_IMM);
249                         gen_eight(c3);
250                         r = reg;
251                 }
252                 if (r != R_ZERO) {
253                         gen_insn(INSN_ROT, OP_SIZE_NATIVE, ROT_SHL, 0);
254                         gen_one(reg);
255                         gen_one(reg);
256                         gen_one(ARG_IMM);
257                         gen_eight(32);
258                 }
259         }
260         if (c2) {
261                 if (r == R_ZERO) {
262                         gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
263                         gen_one(reg);
264                         gen_one(ARG_IMM);
265                         gen_eight((uint64_t)c2 << 14);
266                         r = reg;
267                 } else {
268                         gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_ADD, 0);
269                         gen_one(R_CONST_IMM);
270                         gen_one(r);
271                         gen_one(ARG_IMM);
272                         gen_eight((uint64_t)c2 << 14);
273                         r = R_CONST_IMM;
274                 }
275         }
276         if (c1 || r != reg) {
277                 gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_ADD, 0);
278                 gen_one(reg);
279                 gen_one(r);
280                 gen_one(ARG_IMM);
281                 gen_eight(c1);
282         }
283         return true;
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;
293         switch (purpose) {
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) {
299                                 if (imm & 7)
300                                         break;
301                         }
302                         if (likely(imm >= -0x2000) && likely(imm < 0x2000))
303                                 return true;
304                         break;
305                 case IMM_PURPOSE_VLDR_VSTR_OFFSET:
306                         if (likely(imm >= -0x10) && likely(imm < 0x10))
307                                 return true;
308                         if (!PA_20)
309                                 break;
310                         if (unlikely((imm & ((1 << size) - 1)) != 0))
311                                 break;
312                         if (likely(imm >= -0x2000) && likely(imm < 0x2000))
313                                 return true;
314                         break;
315                 default:
316                         internal(file_line, "gen_address: invalid purpose %u (imm %"PRIxMAX", size %u)", purpose, (uintmax_t)imm, size);
317         }
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);
322                 gen_one(base);
323                 gen_one(ARG_IMM);
324                 gen_eight(imm);
326                 ctx->base_reg = R_OFFSET_IMM;
327                 ctx->offset_imm = 0;
329                 return true;
330         }
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;
336                 return true;
337         }
339         gen_insn(INSN_ALU, OP_SIZE_ADDRESS, ALU_ADD, 0);
340         gen_one(R_OFFSET_IMM);
341         gen_one(R_OFFSET_IMM);
342         gen_one(base);
344         ctx->base_reg = R_OFFSET_IMM;
345         ctx->offset_imm = 0;
347         return true;
350 static bool is_direct_const(int64_t imm, unsigned purpose, unsigned size)
352         int64_t imm_copy = imm;
353         switch (purpose) {
354                 case IMM_PURPOSE_STORE_VALUE:
355                         if (!imm)
356                                 return true;
357                         break;
358                 case IMM_PURPOSE_SUB:
359                         imm_copy = -(uint64_t)imm_copy;
360                         /*-fallthrough*/
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))
366                                 return true;
367                         break;
368                 case IMM_PURPOSE_JMP_2REGS:
369 #ifdef ARCH_PARISC32
370                         if (likely(imm >= -16) && likely(imm < 16))
371                                 return true;
372 #endif
373                         break;
374                 case IMM_PURPOSE_AND:
375                 case IMM_PURPOSE_OR:
376                 case IMM_PURPOSE_XOR:
377                 case IMM_PURPOSE_ANDN:
378                 case IMM_PURPOSE_TEST:
379                         if (!imm)
380                                 return true;
381                         break;
382                 case IMM_PURPOSE_BITWISE:
383                         return true;
384                 default:
385                         internal(file_line, "is_direct_const: invalid purpose %u (imm %"PRIxMAX", size %u)", purpose, (uintmax_t)imm, size);
386         }
387         return false;
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;
395         } else {
396                 g(gen_load_constant(ctx, R_CONST_IMM, imm));
397                 ctx->const_reg = true;
398         }
399         return true;
402 static bool attr_w gen_entry(struct codegen_context *ctx)
404         int i;
406         gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
407         gen_one(ARG_ADDRESS_1);
408         gen_one(R_SP);
409         gen_eight(RP_OFFS);
410         gen_one(R_RP);
412         gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
413         gen_one(ARG_ADDRESS_1_POST_I);
414         gen_one(R_SP);
415         gen_eight(FRAME_SIZE);
416         gen_one(R_3);
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);
422                 gen_one(R_SP);
423                 gen_eight(offs);
424                 gen_one(i);
425         }
427         gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
428         gen_one(R_FRAME);
429         gen_one(R_ARG0);
431         gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
432         gen_one(R_UPCALL);
433         gen_one(R_ARG1);
435         gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
436         gen_one(R_TIMESTAMP);
437         gen_one(R_ARG2);
439         gen_insn(INSN_JMP_INDIRECT, 0, 0, 0);
440         gen_one(R_ARG3);
442         return true;
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);
452         return true;
455 static bool attr_w gen_escape(struct codegen_context *ctx)
457         int i;
459         gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
460         gen_one(R_RET0);
461         gen_one(R_FRAME);
463         gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
464         gen_one(R_RET1);
465         gen_one(R_SCRATCH_1);
467         gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
468         gen_one(R_RP);
469         gen_one(ARG_ADDRESS_1);
470         gen_one(R_SP);
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);
476                 gen_one(i);
477                 gen_one(ARG_ADDRESS_1);
478                 gen_one(R_SP);
479                 gen_eight(offs);
480         }
482         gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
483         gen_one(R_3);
484         gen_one(ARG_ADDRESS_1_PRE_I);
485         gen_one(R_SP);
486         gen_eight(-FRAME_SIZE);
488         gen_insn(INSN_RET, 0, 0, 0);
490         return true;
493 static bool attr_w gen_upcall_argument(struct codegen_context attr_unused *ctx, unsigned attr_unused arg)
495         return true;
498 static bool attr_w gen_upcall(struct codegen_context *ctx, unsigned offset, unsigned n_args)
500 #ifdef ARCH_PARISC32
501         uint32_t label = alloc_call_label(ctx);
502         if (unlikely(!label))
503                 return false;
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);
511         gen_four(label);
512 #else
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);
515         gen_one(R_DP);
516         gen_address_offset();
518         gen_insn(INSN_CALL_INDIRECT, OP_SIZE_8, 0, 0);
519         gen_one(R_DP);
520 #endif
521         g(gen_upcall_end(ctx, n_args));
523         return true;
526 static bool attr_w gen_call_millicode(struct codegen_context *ctx)
528         gen_insn(INSN_CALL_MILLICODE, 0, 0, 0);
529         return true;
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));
543         return true;