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/>.
21 #define PARISC_COND_64 0x0020U
22 #define PARISC_COND_NEVER 0x0000U
23 #define PARISC_COND_ALWAYS 0x1000U
24 #define PARISC_COND_EQUAL 0x2000U
25 #define PARISC_COND_NOT_EQUAL 0x3000U
26 #define PARISC_COND_L_S 0x4000U
27 #define PARISC_COND_GE_S 0x5000U
28 #define PARISC_COND_LE_S 0x6000U
29 #define PARISC_COND_G_S 0x7000U
30 #define PARISC_COND_L_U 0x8000U
31 #define PARISC_COND_GE_U 0x9000U
32 #define PARISC_COND_LE_U 0xa000U
33 #define PARISC_COND_G_U 0xb000U
34 #define PARISC_COND_SV 0xc000U
35 #define PARISC_COND_NSV 0xd000U
36 #define PARISC_COND_OD 0xe000U
37 #define PARISC_COND_EV 0xf000U
38 #define PARISC_COND_NEG 0x1000U
40 #define PARISC_LDSID 0x000010a0U
41 #define PARISC_MTSP 0x00001820U
42 #define PARISC_MTSAR 0x01601840U
43 #define PARISC_MTSARCM 0x016018c0U
44 #define PARISC_ANDCM 0x08000000U
45 #define PARISC_AND 0x08000200U
46 #define PARISC_OR 0x08000240U
47 #define PARISC_NOP 0x08000240U
48 #define PARISC_XOR 0x08000280U
49 #define PARISC_SUB 0x08000400U
50 #define PARISC_SUB_B 0x08000500U
51 #define PARISC_SUB_DB 0x08000520U
52 #define PARISC_ADD 0x08000600U
53 #define PARISC_ADD_C 0x08000700U
54 #define PARISC_ADD_DC 0x08000720U
55 #define PARISC_CMPCLR 0x08000880U
56 #define PARISC_UADDCM 0x08000980U
57 #define PARISC_ADD_L 0x08000a00U
58 #define PARISC_SHLADD_L_1 0x08000a40U
59 #define PARISC_SHLADD_L_2 0x08000a80U
60 #define PARISC_SHLADD_L_3 0x08000ac0U
62 #define PARISC_LD_ST 0x0c000000U
63 #define PARISC_LD_ST_B 0x00000000U
64 #define PARISC_LD_ST_H 0x00000040U
65 #define PARISC_LD_ST_W 0x00000080U
66 #define PARISC_LD_ST_D 0x000000c0U
67 #define PARISC_LD_ST_IMM 0x00001000U
68 #define PARISC_LD_ST_MA 0x00000020U
69 #define PARISC_LD_ST_MB 0x00002020U
70 #define PARISC_LD_ST_LD 0x00000000U
71 #define PARISC_LD_ST_ST 0x00000200U
72 #define PARISC_LD_SCALE 0x00002000U
73 #define PARISC_LDIL 0x20000000U
74 #define PARISC_ADDIL 0x28000000U
75 #define PARISC_LDO 0x34000000U
77 #define PARISC_LDB_LONG 0x40000000U
78 #define PARISC_LDH_LONG 0x44000000U
79 #define PARISC_LDW_LONG 0x48000000U
80 #define PARISC_LDW_M 0x4c000000U
81 #define PARISC_LDD 0x50000000U
82 #define PARISC_STB_LONG 0x60000000U
83 #define PARISC_STH_LONG 0x64000000U
84 #define PARISC_STW_LONG 0x68000000U
85 #define PARISC_STW_M 0x6c000000U
86 #define PARISC_STD 0x70000000U
87 #define PARISC_LDD_STD_MA 0x00000008U
88 #define PARISC_LDD_STD_MB 0x0000000cU
90 #define PARISC_CMPB 0x80000000U
91 #define PARISC_CMPIB 0x84000000U
92 #define PARISC_CMPB_NOT 0x88000000U
93 #define PARISC_CMPIB_NOT 0x8c000000U
94 #define PARISC_CMPICLR 0x90000000U
95 #define PARISC_CMPICLR_64 0x00000800U
96 #define PARISC_SUBI 0x94000000U
97 #define PARISC_CMPB_64 0x9c000000U
98 #define PARISC_ADDB 0xa0000000U
99 #define PARISC_ADDB_NOT 0xa8000000U
100 #define PARISC_ADDI 0xb4000000U
101 #define PARISC_CMPB_64_NOT 0xbc000000U
103 #define PARISC_BB_1 0xc4004000U
104 #define PARISC_BB_64_1 0xc4006000U
105 #define PARISC_BB_0 0xc400c000U
106 #define PARISC_BB_64_0 0xc400e000U
107 #define PARISC_SHRPW_SAR 0xd0000000U
108 #define PARISC_SHRPD_SAR 0xd0000200U
109 #define PARISC_EXTRW_U_SAR 0xd0001000U
110 #define PARISC_EXTRD_U_SAR 0xd0001200U
111 #define PARISC_EXTRW_S_SAR 0xd0001400U
112 #define PARISC_EXTRD_S_SAR 0xd0001600U
113 #define PARISC_EXTRW_U 0xd0001800U
114 #define PARISC_EXTRW_S 0xd0001c00U
115 #define PARISC_DEPW_Z_SAR 0xd4000000U
116 #define PARISC_DEPD_Z_SAR 0xd4000200U
117 #define PARISC_DEPW_SAR 0xd4000400U
118 #define PARISC_DEPD_SAR 0xd4000600U
119 #define PARISC_DEPW_Z 0xd4000800U
120 #define PARISC_DEPW 0xd4000c00U
121 #define PARISC_DEPWI_Z_SAR 0xd4001000U
122 #define PARISC_DEPDI_Z_SAR 0xd4001200U
123 #define PARISC_DEPWI_SAR 0xd4001400U
124 #define PARISC_DEPDI_SAR 0xd4001600U
125 #define PARISC_DEPWI_Z 0xd4001800U
126 #define PARISC_DEPWI 0xd4001c00U
127 #define PARISC_EXTRD_U 0xd8001800U
128 #define PARISC_EXTRD_S 0xd8001c00U
129 #define PARISC_BE 0xe0000000U
130 #define PARISC_BL 0xe8000000U
131 #define PARISC_BLR 0xe8004000U
132 #define PARISC_BL_L 0xe800a000U
133 #define PARISC_BV 0xe800c000U
134 #define PARISC_BVE_RET 0xe800d000U
135 #define PARISC_BVE_CALL 0xe800f000U
136 #define PARISC_CMPIB_64 0xec000000U
137 #define PARISC_DEPD_Z 0xf0001000U
138 #define PARISC_DEPD 0xf0001400U
139 #define PARISC_DEPDI_Z 0xf4000000U
140 #define PARISC_DEPDI 0xf4000400U
142 #define PARISC_FP_DOUBLE 0x00000800U
143 #define PARISC_FLDW 0x24001000U
144 #define PARISC_FSTW 0x24001200U
145 #define PARISC_FLDD 0x2c001000U
146 #define PARISC_FSTD 0x2c001200U
147 #define PARISC_FCMP 0x30000400U
148 #define PARISC_FCMP_P 0x02U
149 #define PARISC_FCMP_NE 0x04U
150 #define PARISC_FCMP_AE 0x08U
151 #define PARISC_FCMP_A 0x0cU
152 #define PARISC_FCMP_BE 0x10U
153 #define PARISC_FCMP_B 0x14U
154 #define PARISC_FCMP_E 0x18U
155 #define PARISC_FCMP_NP 0x1cU
156 #define PARISC_FADD 0x30000600U
157 #define PARISC_FTEST 0x30002420U
158 #define PARISC_FSUB 0x30002600U
159 #define PARISC_FCPY 0x30004000U
160 #define PARISC_FMPY 0x30004600U
161 #define PARISC_FDIV 0x30006600U
162 #define PARISC_FCNVXF 0x30008200U
163 #define PARISC_FCNVXF_FROM_LONG 0x00000800U
164 #define PARISC_FCNVXF_TO_DOUBLE 0x00002000U
165 #define PARISC_FSQRT 0x30008000U
166 #define PARISC_FNEG 0x3000c000U
167 #define PARISC_FCNVFXT 0x30018200U
168 #define PARISC_FCNVFXT_FROM_DOUBLE 0x00000800U
169 #define PARISC_FCNVFXT_TO_LONG 0x00002000U
170 #define PARISC_FLDD_LONG 0x50000002U
171 #define PARISC_FLDW_LONG 0x5c000000U
172 #define PARISC_FSTD_LONG 0x70000002U
173 #define PARISC_FSTW_LONG 0x7c000000U
175 static const int32_t cmp_sub_cond[0x12] = {
176 PARISC_COND_SV, PARISC_COND_NSV, PARISC_COND_L_U, PARISC_COND_GE_U,
177 PARISC_COND_EQUAL, PARISC_COND_NOT_EQUAL, PARISC_COND_LE_U, PARISC_COND_G_U,
178 PARISC_COND_L_S, PARISC_COND_GE_S, -1, -1,
179 PARISC_COND_L_S, PARISC_COND_GE_S, PARISC_COND_LE_S, PARISC_COND_G_S,
180 PARISC_COND_EV, PARISC_COND_OD,
183 static unsigned cmp_swap_arguments(unsigned cond)
186 case COND_B: return COND_A;
187 case COND_AE: return COND_BE;
188 case COND_E: return COND_E;
189 case COND_NE: return COND_NE;
190 case COND_BE: return COND_AE;
191 case COND_A: return COND_B;
192 case COND_L: return COND_G;
193 case COND_GE: return COND_LE;
194 case COND_LE: return COND_GE;
195 case COND_G: return COND_L;
197 internal(file_line, "cmp_swap_arguments: can't swap the condition %u", cond);
201 static bool attr_w cgen_2reg(struct codegen_context *ctx, uint32_t mc, unsigned r1, unsigned rt)
203 mc |= (uint32_t)r1 << 21;
204 mc |= (uint32_t)rt << 16;
209 static bool attr_w cgen_3reg(struct codegen_context *ctx, uint32_t mc, unsigned r1, unsigned r2, unsigned rt)
211 mc |= (uint32_t)r1 << 16;
212 mc |= (uint32_t)r2 << 21;
218 static uint32_t attr_unused low_sign_ext_5(unsigned bit5)
220 return ((bit5 & 0x10) >> 4) |
221 ((bit5 & 0x0f) << 1);
224 static uint32_t low_sign_ext_11(unsigned bit11)
226 return ((bit11 & 0x400) >> 10) |
227 ((bit11 & 0x3ff) << 1);
230 static uint32_t low_sign_ext_14(unsigned bit14)
232 return ((bit14 & 0x2000) >> 13) |
233 ((bit14 & 0x1fff) << 1);
236 static uint32_t assemble_12(uint32_t bit12)
238 return ((bit12 & 0x800) >> 11) |
239 ((bit12 & 0x400) >> 8) |
240 ((bit12 & 0x3ff) << 3);
243 static uint32_t assemble_17(uint32_t bit17)
245 return ((bit17 & 0x10000) >> 16) |
246 ((bit17 & 0x0f800) << 5) |
247 ((bit17 & 0x00400) >> 8) |
248 ((bit17 & 0x003ff) << 3);
251 static uint32_t assemble_21(uint32_t bit21)
253 return ((bit21 & 0x100000) >> 20) |
254 ((bit21 & 0x0ffe00) >> 8) |
255 ((bit21 & 0x000180) << 7) |
256 ((bit21 & 0x00007c) << 14) |
257 ((bit21 & 0x000003) << 12);
260 static uint32_t assemble_22(uint32_t bit22)
262 return ((bit22 & 0x200000) >> 21) |
263 ((bit22 & 0x1f0000) << 5) |
264 ((bit22 & 0x00f800) << 5) |
265 ((bit22 & 0x000400) >> 8) |
266 ((bit22 & 0x0003ff) << 3);
269 static bool attr_w cgen_ret(struct codegen_context *ctx)
272 if (!PA_20 && PA_SPACES) {
274 mc |= (uint32_t)R_RP << 21;
278 mc |= (uint32_t)R_CG_SCRATCH << 16;
281 mc |= (uint32_t)R_RP << 21;
285 mc = PA_20 ? PARISC_BVE_RET : PARISC_BV;
286 mc |= (uint32_t)R_RP << 21;
290 cgen_four(PARISC_NOP);
294 static bool attr_w cgen_call(struct codegen_context *ctx, unsigned length)
296 if (length <= JMP_SHORT) {
297 g(add_relocation(ctx, JMP_SHORT, 0, NULL));
298 cgen_four(PARISC_BL | ((uint32_t)R_RP << 21) | PARISC_N);
299 cgen_four(PARISC_NOP);
300 } else if (length == JMP_LONG && PA_20) {
301 g(add_relocation(ctx, JMP_LONG, 0, NULL));
302 cgen_four(PARISC_BL_L | PARISC_N);
303 cgen_four(PARISC_NOP);
305 g(add_relocation(ctx, JMP_EXTRA_LONG, 0, NULL));
306 cgen_four(PARISC_LDIL | ((uint32_t)R_CG_SCRATCH << 21));
307 g(cgen_2reg(ctx, PARISC_LDO, R_CG_SCRATCH, R_CG_SCRATCH));
308 g(cgen_2reg(ctx, PARISC_BLR | PARISC_N, R_RP, R_CG_SCRATCH));
309 g(cgen_2reg(ctx, PARISC_BLR | PARISC_N, R_RP, R_CG_SCRATCH));
310 cgen_four(PARISC_NOP);
315 static bool attr_w cgen_call_indirect(struct codegen_context *ctx)
318 unsigned reg = cget_one(ctx);
320 mc = PARISC_LDD | low_sign_ext_14(0x10);
321 g(cgen_2reg(ctx, mc, reg, R_31));
323 cgen_four(PARISC_BVE_CALL | ((uint32_t)R_31 << 21));
325 mc = PARISC_LDD | low_sign_ext_14(0x18);
326 g(cgen_2reg(ctx, mc, reg, R_DP));
331 static bool attr_w cgen_ld_st(struct codegen_context *ctx, bool ld, unsigned size, uint8_t reg, uint8_t *address)
336 if (address[0] == ARG_ADDRESS_1) {
337 imm = get_imm(&address[2]);
340 case OP_SIZE_1: mc = PARISC_LDB_LONG; break;
341 case OP_SIZE_2: mc = PARISC_LDH_LONG; break;
342 case OP_SIZE_4: mc = PARISC_LDW_LONG; break;
343 case OP_SIZE_8: mc = PARISC_LDD; break;
344 default: goto invalid;
348 case OP_SIZE_1: mc = PARISC_STB_LONG; break;
349 case OP_SIZE_2: mc = PARISC_STH_LONG; break;
350 case OP_SIZE_4: mc = PARISC_STW_LONG; break;
351 case OP_SIZE_8: mc = PARISC_STD; break;
352 default: goto invalid;
355 mc |= low_sign_ext_14(imm);
356 g(cgen_2reg(ctx, mc, address[1], reg));
359 if (address[0] == ARG_ADDRESS_1_PRE_I || address[0] == ARG_ADDRESS_1_POST_I) {
360 imm = get_imm(&address[2]);
361 if (size == OP_SIZE_4) {
362 if (imm >= 0 && address[0] == ARG_ADDRESS_1_PRE_I)
364 if (imm < 0 && address[0] == ARG_ADDRESS_1_POST_I)
369 case OP_SIZE_4: mc = PARISC_LDW_M; break;
370 case OP_SIZE_8: mc = PARISC_LDD | (address[0] == ARG_ADDRESS_1_PRE_I ? PARISC_LDD_STD_MB : PARISC_LDD_STD_MA); break;
371 default: goto invalid;
375 case OP_SIZE_4: mc = PARISC_STW_M; break;
376 case OP_SIZE_8: mc = PARISC_STD | (address[0] == ARG_ADDRESS_1_PRE_I ? PARISC_LDD_STD_MB : PARISC_LDD_STD_MA); break;
377 default: goto invalid;
380 mc |= low_sign_ext_14(imm);
381 g(cgen_2reg(ctx, mc, address[1], reg));
384 if (address[0] >= ARG_ADDRESS_2 && address[0] <= ARG_ADDRESS_2_8 && ld) {
385 imm = get_imm(&address[3]);
386 if (unlikely(imm != 0))
388 mc = PARISC_LD_ST | PARISC_LD_ST_LD;
389 if (address[0] == ARG_ADDRESS_2) {
390 } else if (address[0] == ARG_ADDRESS_2_4 && size == OP_SIZE_4) {
391 mc |= PARISC_LD_SCALE;
392 } else if (address[0] == ARG_ADDRESS_2_8 && size == OP_SIZE_8) {
393 mc |= PARISC_LD_SCALE;
398 case OP_SIZE_1: mc |= PARISC_LD_ST_B; break;
399 case OP_SIZE_2: mc |= PARISC_LD_ST_H; break;
400 case OP_SIZE_4: mc |= PARISC_LD_ST_W; break;
401 case OP_SIZE_8: mc |= PARISC_LD_ST_D; break;
402 default: goto invalid;
404 g(cgen_3reg(ctx, mc, address[2], address[1], reg));
408 internal(file_line, "cgen_ld_st: invalid address: %x, %02x, %02x, %"PRIxMAX"", size, reg, address[0], (uintmax_t)imm);
412 static bool attr_w cgen_fp_ld_st(struct codegen_context *ctx, bool ld, unsigned size, uint8_t reg, uint8_t *address)
417 if (unlikely(address[0] != ARG_ADDRESS_1))
420 imm = get_imm(&address[2]);
421 if (imm >= -0x10 && imm < 0x10) {
423 mc = size == OP_SIZE_4 ? PARISC_FLDW : PARISC_FLDD;
425 mc = size == OP_SIZE_4 ? PARISC_FSTW : PARISC_FSTD;
426 mc |= low_sign_ext_5(imm) << 16;
428 mc |= (uint32_t)address[1] << 21;
433 mc = size == OP_SIZE_4 ? PARISC_FLDW_LONG : PARISC_FLDD_LONG;
435 mc = size == OP_SIZE_4 ? PARISC_FSTW_LONG : PARISC_FSTD_LONG;
436 mc |= low_sign_ext_14(imm);
437 g(cgen_2reg(ctx, mc, address[1], reg & 0x1f));
442 internal(file_line, "cgen_fp_ld_st: invalid address: %x, %02x, %02x, %"PRIxMAX"", size, reg, address[0], (uintmax_t)imm);
446 static bool attr_w cgen_mov_args(struct codegen_context *ctx, unsigned size, uint8_t *arg1, uint8_t *arg2)
451 if (arg1[0] < 0x20) {
452 if (arg2[0] < 0x20) {
453 if (size < OP_SIZE_NATIVE) {
454 if (OP_SIZE_NATIVE == OP_SIZE_4) {
457 mc = PARISC_EXTRW_U ^ 0x3f8U;
460 mc = PARISC_EXTRW_U ^ 0x3f0U;
463 internal(file_line, "cgen_mov_args: invalid op size %u", size);
468 mc = PARISC_EXTRD_U ^ 0x13f8U;
471 mc = PARISC_EXTRD_U ^ 0x13f0U;
474 mc = PARISC_EXTRD_U ^ 0x13e0U;
477 internal(file_line, "cgen_mov_args: invalid op size %u", size);
480 g(cgen_2reg(ctx, mc, arg2[0], arg1[0]));
483 g(cgen_3reg(ctx, PARISC_OR, arg2[0], 0, arg1[0]));
486 if (arg2[0] == ARG_IMM) {
487 imm = get_imm(&arg2[1]);
488 if (imm >= -0x2000 && imm < 0x2000) {
490 mc |= low_sign_ext_14(imm);
491 g(cgen_2reg(ctx, mc, R_ZERO, arg1[0]));
497 mc |= (uint32_t)arg1[0] << 21;
498 mc |= assemble_21((uint64_t)imm >> 11);
502 return cgen_ld_st(ctx, true, size, arg1[0], arg2);
505 if (arg2[0] < 0x20) {
506 return cgen_ld_st(ctx, false, size, arg2[0], arg1);
508 if (arg2[0] == ARG_IMM) {
509 imm = get_imm(&arg2[1]);
510 if (unlikely(imm != 0))
512 return cgen_ld_st(ctx, false, size, R_ZERO, arg1);
515 if (reg_is_fp(arg1[0]) && arg2[0] == ARG_ADDRESS_1) {
516 return cgen_fp_ld_st(ctx, true, size, arg1[0], arg2);
518 if (reg_is_fp(arg2[0]) && arg1[0] == ARG_ADDRESS_1) {
519 return cgen_fp_ld_st(ctx, false, size, arg2[0], arg1);
521 if (reg_is_fp(arg1[0]) && reg_is_fp(arg2[0])) {
524 case OP_SIZE_4: break;
525 case OP_SIZE_8: mc |= PARISC_FP_DOUBLE; break;
526 default: internal(file_line, "cgen_fp_alu: invalid size %u", size);
528 g(cgen_3reg(ctx, mc, 0, arg2[0] & 0x1f, arg1[0] & 0x1f));
533 internal(file_line, "cgen_mov_args: invalid arguments %02x, %02x", arg1[0], arg2[0]);
537 static bool attr_w cgen_mov(struct codegen_context *ctx, unsigned size)
539 uint8_t *arg1 = ctx->code_position;
540 uint8_t *arg2 = arg1 + arg_size(*arg1);
541 ctx->code_position = arg2 + arg_size(*arg2);
542 return cgen_mov_args(ctx, size, arg1, arg2);
545 static bool attr_w cgen_movsx(struct codegen_context *ctx, unsigned size)
548 uint8_t *arg1 = ctx->code_position;
549 uint8_t *arg2 = arg1 + arg_size(*arg1);
550 ctx->code_position = arg2 + arg_size(*arg2);
551 if (unlikely(arg1[0] >= 0x20) || unlikely(arg2[0] >= 0x20))
552 internal(file_line, "cgen_movsx: invalid arguments %02x, %02x", arg1[0], arg2[0]);
554 if (size < OP_SIZE_NATIVE) {
555 if (OP_SIZE_NATIVE == OP_SIZE_4) {
558 mc = PARISC_EXTRW_S ^ 0x3f8U;
561 mc = PARISC_EXTRW_S ^ 0x3f0U;
564 internal(file_line, "cgen_movsx: invalid op size %u", size);
569 mc = PARISC_EXTRD_S ^ 0x13f8U;
572 mc = PARISC_EXTRD_S ^ 0x13f0U;
575 mc = PARISC_EXTRD_S ^ 0x13e0U;
578 internal(file_line, "cgen_movsx: invalid op size %u", size);
581 g(cgen_2reg(ctx, mc, arg2[0], arg1[0]));
584 return cgen_mov_args(ctx, size, arg1, arg2);
588 static bool attr_w cgen_cmp_dest_reg(struct codegen_context *ctx, unsigned size, unsigned cond)
592 uint8_t *arg1 = ctx->code_position;
593 uint8_t *arg2 = arg1 + arg_size(*arg1);
594 uint8_t *arg3 = arg2 + arg_size(*arg2);
595 ctx->code_position = arg3 + arg_size(*arg3);
597 if (arg3[0] == ARG_IMM) {
598 cond = cmp_swap_arguments(cond);
601 fc = cmp_sub_cond[cond];
602 fc ^= PARISC_COND_NEG;
604 if (arg3[0] == ARG_IMM) {
605 int64_t imm = get_imm(&arg3[1]);
607 if (size == OP_SIZE_8)
608 mc |= PARISC_CMPICLR_64;
610 mc |= low_sign_ext_11(imm);
611 g(cgen_2reg(ctx, mc, arg2[0], arg1[0]));
614 if (size == OP_SIZE_8)
615 mc |= PARISC_COND_64;
617 g(cgen_3reg(ctx, mc, arg2[0], arg3[0], arg1[0]));
621 mc |= low_sign_ext_14(1);
622 g(cgen_2reg(ctx, mc, R_ZERO, arg1[0]));
627 static bool attr_w cgen_alu(struct codegen_context *ctx, unsigned size, unsigned writes_flags, unsigned alu, bool trap, unsigned length)
631 uint8_t *arg1 = ctx->code_position;
632 uint8_t *arg2 = arg1 + arg_size(*arg1);
633 uint8_t *arg3 = arg2 + arg_size(*arg2);
634 ctx->code_position = arg3 + arg_size(*arg3);
636 if (unlikely(arg1[0] >= 0x20) || unlikely(arg2[0] >= 0x20))
639 if (arg3[0] == ARG_IMM) {
640 int64_t imm = get_imm(&arg3[1]);
641 if (unlikely(writes_flags))
645 imm = -(uint64_t)imm;
646 else if (unlikely(alu != ALU_ADD))
648 if (imm >= -0x2000 && imm < 0x2000) {
650 mc |= low_sign_ext_14(imm);
651 g(cgen_2reg(ctx, mc, arg2[0], arg1[0]));
653 } else if (!(imm & 0x7ff) && arg1[0] == R_1) {
655 mc |= (uint32_t)arg2[0] << 21;
656 mc |= assemble_21((uint64_t)imm >> 11);
664 imm = -(uint64_t)imm;
665 else if (unlikely(alu != ALU_ADD))
667 if (size == OP_SIZE_8) {
669 mc |= low_sign_ext_14(imm);
670 g(cgen_2reg(ctx, mc, R_ZERO, R_CG_SCRATCH));
678 mc |= low_sign_ext_11(imm);
688 case ALU_ADD: mc = writes_flags ? PARISC_ADD : PARISC_ADD_L;
690 case ALU_OR: ajla_assert_lo(!writes_flags, (file_line, "cgen_alu: or must not write flags"));
693 case ALU_ADC: ajla_assert_lo(writes_flags, (file_line, "cgen_alu: adc must write flags"));
694 mc = size == OP_SIZE_4 ? PARISC_ADD_C : PARISC_ADD_DC;
696 case ALU_SBB: ajla_assert_lo(writes_flags, (file_line, "cgen_alu: sbb must write flags"));
697 mc = size == OP_SIZE_4 ? PARISC_SUB_B : PARISC_SUB_DB;
699 case ALU_AND: ajla_assert_lo(!writes_flags, (file_line, "cgen_alu: and must not write flags"));
702 case ALU_SUB: ajla_assert_lo(writes_flags, (file_line, "cgen_alu: sub must write flags"));
705 case ALU_XOR: ajla_assert_lo(!writes_flags, (file_line, "cgen_alu: xor must not write flags"));
708 case ALU_ANDN: ajla_assert_lo(!writes_flags, (file_line, "cgen_alu: andn must not write flags"));
711 default: goto invalid;
714 if (alu == ALU_ADD && arg3[0] == ARG_SHIFTED_REGISTER) {
716 if (unlikely((arg3[1] >> 6) != 0))
718 if (unlikely((arg3[1] & ARG_SHIFT_AMOUNT)) >= 4)
720 mc |= (uint32_t)(arg3[1] & ARG_SHIFT_AMOUNT) << 6;
727 if (unlikely(arg3[0] >= 0x20))
731 if (alu == ALU_ADD && size == OP_SIZE_4 && arg1[0] == arg2[0] && trap && length == JMP_SHORTEST) {
734 mc |= PARISC_COND_SV;
736 g(add_relocation(ctx, JMP_SHORTEST, ctx->code_position - arg1, &known));
737 g(cgen_2reg(ctx, mc, arg1[0], arg3[0]));
738 if (unlikely(!known))
739 cgen_four(PARISC_NOP);
750 if (length == JMP_EXTRA_LONG || (length == JMP_LONG && !PA_20))
751 mc |= PARISC_COND_SV;
753 mc |= PARISC_COND_NSV;
754 if (size == OP_SIZE_8)
755 mc |= PARISC_COND_64;
758 g(cgen_3reg(ctx, mc, r1, r2, rt));
761 if (length <= JMP_SHORT) {
762 g(add_relocation(ctx, JMP_SHORT, ctx->code_position - arg1, NULL));
763 cgen_four(PARISC_BL | PARISC_N);
764 } else if (length == JMP_LONG && PA_20) {
765 g(add_relocation(ctx, JMP_LONG, ctx->code_position - arg1, NULL));
766 cgen_four(PARISC_BL_L | PARISC_N);
768 cgen_four(PARISC_BL | PARISC_N | assemble_12(3));
769 g(add_relocation(ctx, JMP_EXTRA_LONG, ctx->code_position - arg1, NULL));
770 cgen_four(PARISC_LDIL | ((uint32_t)R_CG_SCRATCH << 21));
771 g(cgen_2reg(ctx, PARISC_LDO, R_CG_SCRATCH, R_CG_SCRATCH));
772 g(cgen_2reg(ctx, PARISC_BLR | PARISC_N, R_ZERO, R_CG_SCRATCH));
773 g(cgen_2reg(ctx, PARISC_BLR | PARISC_N, R_ZERO, R_CG_SCRATCH));
780 internal(file_line, "cgen_alu: invalid arguments %02x, %02x, %02x, %u, %u, %u", arg1[0], arg2[0], arg3[0], alu, writes_flags, (unsigned)trap);
784 static bool attr_w cgen_alu1(struct codegen_context *ctx, unsigned size, unsigned writes_flags, unsigned alu, bool trap, unsigned length)
788 uint8_t *arg1 = ctx->code_position;
789 uint8_t *arg2 = arg1 + arg_size(*arg1);
790 ctx->code_position = arg2 + arg_size(*arg2);
792 if (unlikely(arg1[0] >= 0x20) || unlikely(arg2[0] >= 0x20))
796 case ALU1_NOT: ajla_assert_lo(!writes_flags, (file_line, "cgen_alu1: not must not write flags"));
802 case ALU1_NEG: ajla_assert_lo(writes_flags, (file_line, "cgen_alu1: neg must write flags"));
808 default: goto invalid;
812 if (length == JMP_EXTRA_LONG || (length == JMP_LONG && !PA_20))
813 mc |= PARISC_COND_SV;
815 mc |= PARISC_COND_NSV;
816 if (size == OP_SIZE_8)
817 mc |= PARISC_COND_64;
820 g(cgen_3reg(ctx, mc, r1, r2, rt));
823 if (length <= JMP_SHORT) {
824 g(add_relocation(ctx, JMP_SHORT, ctx->code_position - arg1, NULL));
825 cgen_four(PARISC_BL | PARISC_N);
826 } else if (length == JMP_LONG && PA_20) {
827 g(add_relocation(ctx, JMP_LONG, ctx->code_position - arg1, NULL));
828 cgen_four(PARISC_BL_L | PARISC_N);
830 cgen_four(PARISC_BL | PARISC_N | assemble_12(3));
831 g(add_relocation(ctx, JMP_EXTRA_LONG, ctx->code_position - arg1, NULL));
832 cgen_four(PARISC_LDIL | ((uint32_t)R_CG_SCRATCH << 21));
833 g(cgen_2reg(ctx, PARISC_LDO, R_CG_SCRATCH, R_CG_SCRATCH));
834 g(cgen_2reg(ctx, PARISC_BLR | PARISC_N, R_ZERO, R_CG_SCRATCH));
835 g(cgen_2reg(ctx, PARISC_BLR | PARISC_N, R_ZERO, R_CG_SCRATCH));
842 internal(file_line, "cgen_alu1: invalid arguments %02x, %02x, %u, %u", arg1[0], arg2[0], alu, writes_flags);
846 static bool attr_w cgen_sar(struct codegen_context *ctx, bool sar_complement, uint8_t reg)
849 if (!sar_complement) {
851 mc |= (uint32_t)reg << 16;
855 mc |= (uint32_t)reg << 16;
859 mc |= low_sign_ext_14(-1);
860 g(cgen_2reg(ctx, mc, 0, R_CG_SCRATCH));
862 g(cgen_3reg(ctx, mc, R_CG_SCRATCH, reg, R_CG_SCRATCH));
864 mc |= (uint32_t)R_CG_SCRATCH << 16;
870 static bool attr_w cgen_rot(struct codegen_context *ctx, unsigned size, unsigned rot)
873 uint8_t *arg1 = ctx->code_position;
874 uint8_t *arg2 = arg1 + arg_size(*arg1);
875 uint8_t *arg3 = arg2 + arg_size(*arg2);
876 ctx->code_position = arg3 + arg_size(*arg3);
877 if (arg3[0] == ARG_IMM) {
878 int64_t imm = get_imm(&arg3[1]);
879 unsigned sh = imm & (size == OP_SIZE_4 ? 31 : 63);
880 if (size == OP_SIZE_4) {
884 mc |= sh | (sh << 5);
888 mc |= sh | ((31 - sh) << 5);
892 mc |= sh | ((31 - sh) << 5);
901 mc |= (sh & 31) | ((sh & 31) << 5);
907 mc |= (sh & 31) | ((31 - (sh & 31)) << 5);
913 mc |= (sh & 31) | ((31 - (sh & 31)) << 5);
922 g(cgen_2reg(ctx, mc, arg1[0], arg2[0]));
924 g(cgen_2reg(ctx, mc, arg2[0], arg1[0]));
926 } else if (arg3[0] < 0x20) {
927 bool sar_complement = rot != ROT_SHR;
928 g(cgen_sar(ctx, sar_complement, arg3[0]));
929 if (size == OP_SIZE_4) {
932 mc = PARISC_DEPW_Z_SAR;
935 mc = PARISC_SHRPW_SAR;
938 mc = PARISC_EXTRW_S_SAR;
946 mc = PARISC_DEPD_Z_SAR | 0x100;
949 mc = PARISC_SHRPD_SAR;
952 mc = PARISC_EXTRD_S_SAR | 0x100;
958 if (rot == ROT_SHL) {
959 g(cgen_2reg(ctx, mc, arg1[0], arg2[0]));
960 } else if (rot == ROT_SHR) {
961 g(cgen_3reg(ctx, mc, R_ZERO, arg2[0], arg1[0]));
963 g(cgen_2reg(ctx, mc, arg2[0], arg1[0]));
969 internal(file_line, "cgen_rot: invalid arguments %02x, %02x, %02x, %u, %u", arg1[0], arg2[0], arg3[0], size, rot);
973 static bool attr_w cgen_btx(struct codegen_context *ctx, unsigned size, unsigned aux)
976 uint8_t *arg1 = ctx->code_position;
977 uint8_t *arg2 = arg1 + arg_size(*arg1);
978 uint8_t *arg3 = arg2 + arg_size(*arg2);
979 ctx->code_position = arg3 + arg_size(*arg3);
981 if (aux == BTX_BTS || aux == BTX_BTR) {
982 if (arg1[0] != arg2[0]) {
983 g(cgen_3reg(ctx, PARISC_OR, arg2[0], 0, arg1[0]));
987 if (arg3[0] == ARG_IMM) {
988 int64_t imm = get_imm(&arg3[1]);
989 unsigned bit = imm & (size == OP_SIZE_4 ? 31 : 63);
990 if (size == OP_SIZE_4) {
995 mc |= 31U | (bit << 5);
996 g(cgen_2reg(ctx, mc, arg1[0], low_sign_ext_5(aux == BTX_BTS)));
1000 mc |= 31U | ((31 - bit) << 5);
1001 g(cgen_2reg(ctx, mc, arg2[0], arg1[0]));
1011 mc |= 31U | ((bit & 0x1f) << 5) | ((bit & 0x20) << 6);
1012 g(cgen_2reg(ctx, mc, arg1[0], low_sign_ext_5(aux == BTX_BTS)));
1015 mc = PARISC_EXTRD_U;
1016 mc |= 31U | (((63 - bit) & 0x1f) << 5) | (((63 - bit) & 0x20) << 6);
1017 g(cgen_2reg(ctx, mc, arg2[0], arg1[0]));
1024 g(cgen_sar(ctx, true, arg3[0]));
1025 if (size == OP_SIZE_4) {
1029 mc = PARISC_DEPWI_SAR;
1031 g(cgen_2reg(ctx, mc, arg1[0], low_sign_ext_5(aux == BTX_BTS)));
1034 mc = PARISC_EXTRW_U_SAR;
1036 g(cgen_2reg(ctx, mc, arg2[0], arg1[0]));
1045 mc = PARISC_DEPDI_SAR;
1047 g(cgen_2reg(ctx, mc, arg1[0], low_sign_ext_5(aux == BTX_BTS)));
1050 mc = PARISC_EXTRD_U_SAR;
1052 g(cgen_2reg(ctx, mc, arg2[0], arg1[0]));
1061 internal(file_line, "cgen_btx: invalid arguments %02x, %02x, %02x, %u, %u", arg1[0], arg2[0], arg3[0], size, aux);
1065 static bool attr_w cgen_cmp_cmov(struct codegen_context *ctx, unsigned op_size, unsigned cond)
1070 uint8_t *arg1 = ctx->code_position;
1071 uint8_t *arg2 = arg1 + arg_size(*arg1);
1072 uint8_t *arg3 = arg2 + arg_size(*arg2);
1073 uint8_t *arg4 = arg3 + arg_size(*arg3);
1074 uint8_t *arg5 = arg4 + arg_size(*arg4);
1075 ctx->code_position = arg5 + arg_size(*arg5);
1077 if (arg3[0] == ARG_IMM) {
1078 cond = cmp_swap_arguments(cond);
1081 fc = cmp_sub_cond[cond];
1082 fc ^= PARISC_COND_NEG;
1084 if (arg5[0] == ARG_IMM) {
1085 imm = get_imm(&arg5[1]);
1086 mc = PARISC_CMPICLR;
1088 mc |= low_sign_ext_11(imm);
1089 g(cgen_2reg(ctx, mc, arg4[0], R_ZERO));
1093 g(cgen_3reg(ctx, mc, arg4[0], arg5[0], R_ZERO));
1096 g(cgen_mov_args(ctx, op_size, arg1, arg3));
1101 static bool attr_w cgen_movr(struct codegen_context *ctx, unsigned cond)
1105 uint8_t *arg1 = ctx->code_position;
1106 uint8_t *arg2 = arg1 + arg_size(*arg1);
1107 uint8_t *arg3 = arg2 + arg_size(*arg2);
1108 uint8_t *arg4 = arg3 + arg_size(*arg3);
1109 ctx->code_position = arg4 + arg_size(*arg4);
1111 fc = cmp_sub_cond[cond];
1112 fc ^= PARISC_COND_NEG;
1114 if (unlikely(arg1[0] != arg2[0]) || unlikely(arg1[0] >= 0x20))
1115 internal(file_line, "cgen_movr: invalid arguments");
1119 if (OP_SIZE_NATIVE == OP_SIZE_8)
1120 mc |= PARISC_COND_64;
1122 g(cgen_3reg(ctx, mc, R_ZERO, arg3[0], R_ZERO));
1124 g(cgen_mov_args(ctx, OP_SIZE_NATIVE, arg1, arg4));
1129 static bool attr_w cgen_fp_cmp_cond(struct codegen_context *ctx, unsigned op_size, unsigned aux)
1133 uint8_t *arg1 = ctx->code_position;
1134 uint8_t *arg2 = arg1 + arg_size(*arg1);
1135 ctx->code_position = arg2 + arg_size(*arg2);
1137 case FP_COND_P: cond = PARISC_FCMP_P; break;
1138 case FP_COND_NP: cond = PARISC_FCMP_NP; break;
1139 case FP_COND_E: cond = PARISC_FCMP_E; break;
1140 case FP_COND_NE: cond = PARISC_FCMP_NE; break;
1141 case FP_COND_A: cond = PARISC_FCMP_A; break;
1142 case FP_COND_BE: cond = PARISC_FCMP_BE; break;
1143 case FP_COND_B: cond = PARISC_FCMP_B; break;
1144 case FP_COND_AE: cond = PARISC_FCMP_AE; break;
1145 default: internal(file_line, "cgen_fp_cmp_cond: invalid condition %u", aux);
1149 if (op_size == OP_SIZE_8)
1150 mc |= PARISC_FP_DOUBLE;
1151 g(cgen_3reg(ctx, mc, arg1[0] & 0x1f, arg2[0] & 0x1f, cond));
1155 static bool attr_w cgen_fp_test_reg(struct codegen_context *ctx)
1157 unsigned reg = cget_one(ctx);
1161 mc |= low_sign_ext_14(1);
1162 g(cgen_2reg(ctx, mc, R_ZERO, reg));
1164 cgen_four(PARISC_FTEST);
1167 mc |= low_sign_ext_14(0);
1168 g(cgen_2reg(ctx, mc, R_ZERO, reg));
1173 static bool attr_w cgen_fp_alu(struct codegen_context *ctx, unsigned op_size, unsigned aux)
1176 uint8_t *arg1 = ctx->code_position;
1177 uint8_t *arg2 = arg1 + arg_size(*arg1);
1178 uint8_t *arg3 = arg2 + arg_size(*arg2);
1179 ctx->code_position = arg3 + arg_size(*arg3);
1181 case FP_ALU_ADD: mc = PARISC_FADD; break;
1182 case FP_ALU_SUB: mc = PARISC_FSUB; break;
1183 case FP_ALU_MUL: mc = PARISC_FMPY; break;
1184 case FP_ALU_DIV: mc = PARISC_FDIV; break;
1185 default: internal(file_line, "cgen_fp_alu: invalid alu %u", aux);
1188 case OP_SIZE_4: break;
1189 case OP_SIZE_8: mc |= PARISC_FP_DOUBLE; break;
1190 default: internal(file_line, "cgen_fp_alu: invalid size %u", op_size);
1192 g(cgen_3reg(ctx, mc, arg3[0] & 0x1f, arg2[0] & 0x1f, arg1[0] & 0x1f));
1196 static bool attr_w cgen_fp_alu1(struct codegen_context *ctx, unsigned op_size, unsigned aux)
1199 uint8_t *arg1 = ctx->code_position;
1200 uint8_t *arg2 = arg1 + arg_size(*arg1);
1201 ctx->code_position = arg2 + arg_size(*arg2);
1203 case FP_ALU1_NEG: mc = PARISC_FNEG; break;
1204 case FP_ALU1_SQRT: mc = PARISC_FSQRT; break;
1205 default: internal(file_line, "cgen_fp_alu1: invalid alu %u", aux);
1208 case OP_SIZE_4: break;
1209 case OP_SIZE_8: mc |= PARISC_FP_DOUBLE; break;
1210 default: internal(file_line, "cgen_fp_alu: invalid size %u", op_size);
1212 g(cgen_3reg(ctx, mc, 0, arg2[0] & 0x1f, arg1[0] & 0x1f));
1216 static bool attr_w cgen_fp_to_int(struct codegen_context *ctx, unsigned int_op_size, unsigned fp_op_size)
1219 uint8_t *arg1 = ctx->code_position;
1220 uint8_t *arg2 = arg1 + arg_size(*arg1);
1221 ctx->code_position = arg2 + arg_size(*arg2);
1223 mc = PARISC_FCNVFXT;
1224 if (int_op_size == OP_SIZE_8)
1225 mc |= PARISC_FCNVFXT_TO_LONG;
1226 if (fp_op_size == OP_SIZE_8)
1227 mc |= PARISC_FCNVFXT_FROM_DOUBLE;
1228 mc |= arg1[0] & 0x1f;
1229 mc |= (uint32_t)(arg2[0] & 0x1f) << 21;
1234 static bool attr_w cgen_fp_from_int(struct codegen_context *ctx, unsigned int_op_size, unsigned fp_op_size)
1237 uint8_t *arg1 = ctx->code_position;
1238 uint8_t *arg2 = arg1 + arg_size(*arg1);
1239 ctx->code_position = arg2 + arg_size(*arg2);
1242 if (int_op_size == OP_SIZE_8)
1243 mc |= PARISC_FCNVXF_FROM_LONG;
1244 if (fp_op_size == OP_SIZE_8)
1245 mc |= PARISC_FCNVXF_TO_DOUBLE;
1246 mc |= arg1[0] & 0x1f;
1247 mc |= (uint32_t)(arg2[0] & 0x1f) << 21;
1252 static bool attr_w cgen_cond_jmp(struct codegen_context *ctx, uint32_t mc, unsigned length, unsigned reg1, unsigned reg2, int reloc_offset)
1254 if (length == JMP_SHORTEST) {
1257 g(add_relocation(ctx, JMP_SHORTEST, reloc_offset, &known));
1258 g(cgen_2reg(ctx, mc, reg2, reg1));
1259 if (unlikely(!known))
1260 cgen_four(PARISC_NOP);
1261 } else if (length == JMP_SHORT) {
1263 g(cgen_2reg(ctx, mc, reg2, reg1));
1264 g(add_relocation(ctx, JMP_SHORT, reloc_offset, NULL));
1265 cgen_four(PARISC_BL | PARISC_N);
1266 } else if (length == JMP_LONG && PA_20) {
1268 g(cgen_2reg(ctx, mc, reg2, reg1));
1269 g(add_relocation(ctx, JMP_LONG, reloc_offset, NULL));
1270 cgen_four(PARISC_BL_L | PARISC_N);
1273 mc |= assemble_12(3);
1274 g(cgen_2reg(ctx, mc, reg2, reg1));
1275 g(add_relocation(ctx, JMP_EXTRA_LONG, reloc_offset, NULL));
1276 cgen_four(PARISC_LDIL | ((uint32_t)R_CG_SCRATCH << 21));
1277 g(cgen_2reg(ctx, PARISC_LDO, R_CG_SCRATCH, R_CG_SCRATCH));
1278 g(cgen_2reg(ctx, PARISC_BLR | PARISC_N, R_ZERO, R_CG_SCRATCH));
1279 g(cgen_2reg(ctx, PARISC_BLR | PARISC_N, R_ZERO, R_CG_SCRATCH));
1284 static bool attr_w cgen_jmp_12regs(struct codegen_context *ctx, unsigned size, unsigned cond, unsigned length, unsigned reg1, unsigned reg2, int reloc_offset)
1288 fc = cmp_sub_cond[cond];
1289 if (length > JMP_SHORTEST)
1290 fc ^= PARISC_COND_NEG;
1291 if (size == OP_SIZE_4)
1292 mc = !(fc & PARISC_COND_NEG) ? PARISC_CMPB : PARISC_CMPB_NOT;
1294 mc = !(fc & PARISC_COND_NEG) ? PARISC_CMPB_64 : PARISC_CMPB_64_NOT;
1295 mc |= ((uint32_t)fc & ~PARISC_COND_NEG);
1296 g(cgen_cond_jmp(ctx, mc, length, reg1, reg2, reloc_offset));
1300 static bool attr_w cgen_jmp(struct codegen_context *ctx, unsigned length)
1302 if (length <= JMP_SHORT) {
1303 g(add_relocation(ctx, JMP_SHORT, 0, NULL));
1304 cgen_four(PARISC_BL | PARISC_N);
1305 } else if (length == JMP_LONG && PA_20) {
1306 g(add_relocation(ctx, JMP_LONG, 0, NULL));
1307 cgen_four(PARISC_BL_L | PARISC_N);
1309 g(add_relocation(ctx, JMP_EXTRA_LONG, 0, NULL));
1310 cgen_four(PARISC_LDIL | ((uint32_t)R_CG_SCRATCH << 21));
1311 g(cgen_2reg(ctx, PARISC_LDO, R_CG_SCRATCH, R_CG_SCRATCH));
1312 g(cgen_2reg(ctx, PARISC_BLR | PARISC_N, R_ZERO, R_CG_SCRATCH));
1313 g(cgen_2reg(ctx, PARISC_BLR | PARISC_N, R_ZERO, R_CG_SCRATCH));
1318 static bool attr_w cgen_jmp_reg(struct codegen_context *ctx, unsigned size, unsigned cond, unsigned length)
1320 unsigned reg = cget_one(ctx);
1321 return cgen_jmp_12regs(ctx, size, cond, length, reg, 0, 1);
1324 static bool attr_w cgen_jmp_reg_bit(struct codegen_context *ctx, unsigned size, unsigned bit, bool jnz, unsigned length)
1327 unsigned reg = cget_one(ctx);
1328 if (length > JMP_SHORTEST)
1330 if (size == OP_SIZE_4 || bit < 32) {
1332 mc = jnz ? PARISC_BB_1 : PARISC_BB_0;
1335 mc = jnz ? PARISC_BB_64_1 : PARISC_BB_64_0;
1337 g(cgen_cond_jmp(ctx, mc, length, reg, bit, 1));
1341 static bool attr_w cgen_jmp_2regs(struct codegen_context *ctx, unsigned size, unsigned cond, unsigned length)
1343 uint8_t *arg1 = ctx->code_position;
1344 uint8_t *arg2 = arg1 + arg_size(*arg1);
1345 ctx->code_position = arg2 + arg_size(*arg2);
1347 return cgen_jmp_12regs(ctx, size, cond, length, arg1[0], arg2[0], 2);
1348 if (arg2[0] == ARG_IMM) {
1349 int64_t imm = get_imm(&arg2[1]);
1352 cond = cmp_swap_arguments(cond);
1353 fc = cmp_sub_cond[cond];
1354 if (length > JMP_SHORTEST)
1355 fc ^= PARISC_COND_NEG;
1356 if (size == OP_SIZE_4) {
1357 mc = fc & PARISC_COND_NEG ? PARISC_CMPIB_NOT : PARISC_CMPIB;
1358 mc |= fc & ~PARISC_COND_NEG;
1360 mc = PARISC_CMPIB_64;
1362 case PARISC_COND_EQUAL: mc |= 1 << 13; break;
1363 case PARISC_COND_NOT_EQUAL: mc |= 5 << 13; break;
1364 case PARISC_COND_L_S: mc |= 2 << 13; break;
1365 case PARISC_COND_GE_S: mc |= 6 << 13; break;
1366 case PARISC_COND_LE_S: mc |= 3 << 13; break;
1367 case PARISC_COND_G_S: mc |= 7 << 13; break;
1368 case PARISC_COND_L_U: mc |= 0 << 13; break;
1369 case PARISC_COND_GE_U: mc |= 4 << 13; break;
1370 default: internal(file_line, "cgen_jmp_2regs: unsupported condition %x", fc);
1373 g(cgen_cond_jmp(ctx, mc, length, low_sign_ext_5(imm), arg1[0], 10));
1376 internal(file_line, "cgen_jmp_2regs: invalid arguments %02x, %02x", arg1[0], arg2[0]);
1380 static bool attr_w cgen_jmp_fp_test(struct codegen_context *ctx, unsigned length)
1382 cgen_four(PARISC_FTEST);
1383 if (length <= JMP_SHORT) {
1384 g(add_relocation(ctx, JMP_SHORT, 0, NULL));
1385 cgen_four(PARISC_BL | PARISC_N);
1386 } else if (length == JMP_LONG && PA_20) {
1387 g(add_relocation(ctx, JMP_LONG, 0, NULL));
1388 cgen_four(PARISC_BL_L | PARISC_N);
1390 cgen_four(PARISC_BL | PARISC_N | assemble_12(0));
1391 cgen_four(PARISC_BL | PARISC_N | assemble_12(3));
1392 g(add_relocation(ctx, JMP_EXTRA_LONG, 0, NULL));
1393 cgen_four(PARISC_LDIL | ((uint32_t)R_CG_SCRATCH << 21));
1394 g(cgen_2reg(ctx, PARISC_LDO, R_CG_SCRATCH, R_CG_SCRATCH));
1395 g(cgen_2reg(ctx, PARISC_BLR | PARISC_N, R_ZERO, R_CG_SCRATCH));
1396 g(cgen_2reg(ctx, PARISC_BLR | PARISC_N, R_ZERO, R_CG_SCRATCH));
1401 static bool attr_w cgen_jmp_indirect(struct codegen_context *ctx, unsigned reg)
1404 #ifdef ARCH_PARISC32
1407 mc = PARISC_BVE_RET;
1408 mc |= (uint32_t)reg << 21;
1410 mc = PARISC_STW_LONG;
1411 mc |= low_sign_ext_14(-0x18);
1412 g(cgen_2reg(ctx, mc, R_SP, R_RP));
1414 mc = PARISC_BVE_RET;
1415 mc |= (uint32_t)reg << 21;
1422 mc |= (uint32_t)reg << 21;
1426 mc |= (uint32_t)R_CG_SCRATCH << 16;
1429 mc |= (uint32_t)reg << 21;
1431 mc = PARISC_STW_LONG;
1432 mc |= low_sign_ext_14(-0x18);
1433 g(cgen_2reg(ctx, mc, R_SP, R_RP));
1436 mc |= (uint32_t)reg << 21;
1443 mc |= (uint32_t)reg << 21;
1447 if (unlikely(!ctx->fn))
1448 cgen_four(PARISC_NOP);
1452 static bool attr_w cgen_call_millicode(struct codegen_context attr_unused *ctx)
1454 #ifdef ARCH_PARISC32
1456 unsigned reg = R_SCRATCH_NA_1;
1461 mc |= assemble_12(1);
1463 g(cgen_2reg(ctx, mc, bit, reg));
1466 mc |= PARISC_LD_ST_W;
1467 mc |= PARISC_LD_ST_IMM;
1468 mc |= PARISC_LD_ST_LD;
1469 g(cgen_3reg(ctx, mc, low_sign_ext_5(2), reg, R_19));
1470 g(cgen_3reg(ctx, mc, low_sign_ext_5(-2), reg, reg));
1472 g(cgen_jmp_indirect(ctx, reg));
1477 static bool attr_w resolve_relocation(struct codegen_context *ctx, struct relocation *reloc)
1481 int64_t offs = (int64_t)(ctx->label_to_pos[reloc->label_id] >> 2) - (int64_t)(reloc->position >> 2) - 2;
1482 switch (reloc->length) {
1484 if (unlikely(offs < -0x800) || unlikely(offs >= 0x800))
1486 memcpy(&mc, ctx->mcode + reloc->position, 4);
1488 mc |= assemble_12(offs);
1489 memcpy(ctx->mcode + reloc->position, &mc, 4);
1492 if (unlikely(offs < -0x10000) || unlikely(offs >= 0x10000))
1494 memcpy(&mc, ctx->mcode + reloc->position, 4);
1496 mc |= assemble_17(offs);
1497 memcpy(ctx->mcode + reloc->position, &mc, 4);
1500 if (unlikely(offs < -0x200000) || unlikely(offs >= 0x200000))
1502 memcpy(&mc, ctx->mcode + reloc->position, 4);
1504 mc |= assemble_22(offs);
1505 memcpy(ctx->mcode + reloc->position, &mc, 4);
1507 case JMP_EXTRA_LONG:
1509 if (unlikely(offs != (int32_t)offs))
1511 memcpy(&mc4, ctx->mcode + reloc->position, 16);
1512 mc4[0] &= ~0x001fffffU;
1513 mc4[0] |= assemble_21((uint64_t)offs >> 1 >> 11);
1514 mc4[1] &= ~0x00003fffU;
1515 mc4[1] |= low_sign_ext_14(((uint64_t)offs >> 1) & 0x7ff);
1517 mc4[2] = PARISC_NOP;
1519 mc4[3] = PARISC_NOP;
1520 memcpy(ctx->mcode + reloc->position, &mc4, 16);
1523 internal(file_line, "resolve_relocation: invalid relocation length %u", reloc->length);
1528 static bool attr_w cgen_insn(struct codegen_context *ctx, uint32_t insn)
1530 /*debug("insn: %08x", insn);*/
1531 switch (insn_opcode(insn)) {
1542 g(cgen_call(ctx, insn_jump_size(insn)));
1544 case INSN_CALL_INDIRECT:
1545 g(cgen_call_indirect(ctx));
1548 g(cgen_mov(ctx, insn_op_size(insn)));
1551 g(cgen_movsx(ctx, insn_op_size(insn)));
1553 case INSN_CMP_DEST_REG:
1554 if (unlikely(insn_op_size(insn) < OP_SIZE_4))
1556 g(cgen_cmp_dest_reg(ctx, insn_op_size(insn), insn_aux(insn)));
1559 case INSN_ALU_FLAGS:
1560 if (unlikely(insn_op_size(insn) < OP_SIZE_4))
1562 g(cgen_alu(ctx, insn_op_size(insn), insn_writes_flags(insn), insn_aux(insn), false, insn_jump_size(insn)));
1565 case INSN_ALU_FLAGS_TRAP:
1566 if (unlikely(insn_op_size(insn) < OP_SIZE_4))
1568 g(cgen_alu(ctx, insn_op_size(insn), insn_writes_flags(insn), insn_aux(insn), true, insn_jump_size(insn)));
1571 if (unlikely(insn_op_size(insn) < OP_SIZE_4))
1573 g(cgen_alu1(ctx, insn_op_size(insn), insn_writes_flags(insn), insn_aux(insn), false, insn_jump_size(insn)));
1575 case INSN_ALU1_TRAP:
1576 if (unlikely(insn_op_size(insn) < OP_SIZE_4))
1578 g(cgen_alu1(ctx, insn_op_size(insn), insn_writes_flags(insn), insn_aux(insn), true, insn_jump_size(insn)));
1581 if (unlikely(insn_op_size(insn) < OP_SIZE_4) || unlikely(insn_writes_flags(insn) != 0))
1583 g(cgen_rot(ctx, insn_op_size(insn), insn_aux(insn)));
1586 if (unlikely(insn_op_size(insn) < OP_SIZE_4))
1588 g(cgen_btx(ctx, insn_op_size(insn), insn_aux(insn)));
1591 if (unlikely(insn_writes_flags(insn) != 0))
1593 g(cgen_cmp_cmov(ctx, insn_op_size(insn), insn_aux(insn)));
1596 if (unlikely(insn_op_size(insn) != OP_SIZE_NATIVE) || unlikely(insn_writes_flags(insn) != 0))
1598 g(cgen_movr(ctx, insn_aux(insn)));
1600 case INSN_FP_CMP_COND:
1601 g(cgen_fp_cmp_cond(ctx, insn_op_size(insn), insn_aux(insn)));
1603 case INSN_FP_TEST_REG:
1604 g(cgen_fp_test_reg(ctx));
1607 g(cgen_fp_alu(ctx, insn_op_size(insn), insn_aux(insn)));
1610 g(cgen_fp_alu1(ctx, insn_op_size(insn), insn_aux(insn)));
1612 case INSN_FP_TO_INT32:
1613 case INSN_FP_TO_INT64:
1614 g(cgen_fp_to_int(ctx, insn_opcode(insn) == INSN_FP_TO_INT32 ? OP_SIZE_4 : OP_SIZE_8, insn_op_size(insn)));
1616 case INSN_FP_FROM_INT32:
1617 case INSN_FP_FROM_INT64:
1618 g(cgen_fp_from_int(ctx, insn_opcode(insn) == INSN_FP_FROM_INT32 ? OP_SIZE_4 : OP_SIZE_8, insn_op_size(insn)));
1621 g(cgen_jmp(ctx, insn_jump_size(insn)));
1624 if (unlikely(insn_op_size(insn) < OP_SIZE_4))
1626 g(cgen_jmp_reg(ctx, insn_op_size(insn), insn_aux(insn), insn_jump_size(insn)));
1628 case INSN_JMP_REG_BIT:
1629 if (unlikely(insn_op_size(insn) < OP_SIZE_4))
1631 g(cgen_jmp_reg_bit(ctx, insn_op_size(insn), insn_aux(insn) & 63, insn_aux(insn) >> 6, insn_jump_size(insn)));
1633 case INSN_JMP_2REGS:
1634 if (unlikely(insn_op_size(insn) < OP_SIZE_4))
1636 g(cgen_jmp_2regs(ctx, insn_op_size(insn), insn_aux(insn), insn_jump_size(insn)));
1638 case INSN_JMP_FP_TEST:
1639 g(cgen_jmp_fp_test(ctx, insn_jump_size(insn)));
1641 case INSN_JMP_INDIRECT:
1642 g(cgen_jmp_indirect(ctx, cget_one(ctx)));
1644 case INSN_CALL_MILLICODE:
1645 g(cgen_call_millicode(ctx));
1649 internal(file_line, "cgen_insn: invalid insn %08x", insn);