implement integer operations with immediate operand
[ajla.git] / c2-hppa.inc
blobb93faadab14a0dac0826c9c440bd5bc28a8d1753
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  PARISC_N                       0x2U
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)
185         switch (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;
196         }
197         internal(file_line, "cmp_swap_arguments: can't swap the condition %u", cond);
198         return 0;
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;
205         cgen_four(mc);
206         return true;
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;
213         mc |= rt;
214         cgen_four(mc);
215         return true;
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)
271         uint32_t mc;
272         if (!PA_20 && PA_SPACES) {
273                 mc = PARISC_LDSID;
274                 mc |= (uint32_t)R_RP << 21;
275                 mc |= R_CG_SCRATCH;
276                 cgen_four(mc);
277                 mc = PARISC_MTSP;
278                 mc |= (uint32_t)R_CG_SCRATCH << 16;
279                 cgen_four(mc);
280                 mc = PARISC_BE;
281                 mc |= (uint32_t)R_RP << 21;
282                 mc |= PARISC_N;
283                 cgen_four(mc);
284         } else {
285                 mc = PA_20 ? PARISC_BVE_RET : PARISC_BV;
286                 mc |= (uint32_t)R_RP << 21;
287                 mc |= PARISC_N;
288                 cgen_four(mc);
289         }
290         cgen_four(PARISC_NOP);
291         return true;
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);
304         } else {
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);
311         }
312         return true;
315 static bool attr_w cgen_call_indirect(struct codegen_context *ctx)
317         uint32_t mc;
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));
328         return true;
331 static bool attr_w cgen_ld_st(struct codegen_context *ctx, bool ld, unsigned size, uint8_t reg, uint8_t *address)
333         int64_t imm = 0;
334         uint32_t mc;
336         if (address[0] == ARG_ADDRESS_1) {
337                 imm = get_imm(&address[2]);
338                 if (ld) {
339                         switch (size) {
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;
345                         }
346                 } else {
347                         switch (size) {
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;
353                         }
354                 }
355                 mc |= low_sign_ext_14(imm);
356                 g(cgen_2reg(ctx, mc, address[1], reg));
357                 return true;
358         }
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)
363                                 goto invalid;
364                         if (imm < 0 && address[0] == ARG_ADDRESS_1_POST_I)
365                                 goto invalid;
366                 }
367                 if (ld) {
368                         switch (size) {
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;
372                         }
373                 } else {
374                         switch (size) {
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;
378                         }
379                 }
380                 mc |= low_sign_ext_14(imm);
381                 g(cgen_2reg(ctx, mc, address[1], reg));
382                 return true;
383         }
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))
387                         goto invalid;
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;
394                 } else {
395                         goto invalid;
396                 }
397                 switch (size) {
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;
403                 }
404                 g(cgen_3reg(ctx, mc, address[2], address[1], reg));
405                 return true;
406         }
407 invalid:
408         internal(file_line, "cgen_ld_st: invalid address: %x, %02x, %02x, %"PRIxMAX"", size, reg, address[0], (uintmax_t)imm);
409         return false;
412 static bool attr_w cgen_fp_ld_st(struct codegen_context *ctx, bool ld, unsigned size, uint8_t reg, uint8_t *address)
414         int64_t imm = 0;
415         uint32_t mc;
417         if (unlikely(address[0] != ARG_ADDRESS_1))
418                 goto invalid;
420         imm = get_imm(&address[2]);
421         if (imm >= -0x10 && imm < 0x10) {
422                 if (ld)
423                         mc = size == OP_SIZE_4 ? PARISC_FLDW : PARISC_FLDD;
424                 else
425                         mc = size == OP_SIZE_4 ? PARISC_FSTW : PARISC_FSTD;
426                 mc |= low_sign_ext_5(imm) << 16;
427                 mc |= reg & 0x1f;
428                 mc |= (uint32_t)address[1] << 21;
429                 cgen_four(mc);
430                 return true;
431         } else {
432                 if (ld)
433                         mc = size == OP_SIZE_4 ? PARISC_FLDW_LONG : PARISC_FLDD_LONG;
434                 else
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));
438                 return true;
439         }
441 invalid:
442         internal(file_line, "cgen_fp_ld_st: invalid address: %x, %02x, %02x, %"PRIxMAX"", size, reg, address[0], (uintmax_t)imm);
443         return false;
446 static bool attr_w cgen_mov_args(struct codegen_context *ctx, unsigned size, uint8_t *arg1, uint8_t *arg2)
448         int64_t imm;
449         uint32_t mc;
451         if (arg1[0] < 0x20) {
452                 if (arg2[0] < 0x20) {
453                         if (size < OP_SIZE_NATIVE) {
454                                 if (OP_SIZE_NATIVE == OP_SIZE_4) {
455                                         switch (size) {
456                                                 case OP_SIZE_1:
457                                                         mc = PARISC_EXTRW_U ^ 0x3f8U;
458                                                         break;
459                                                 case OP_SIZE_2:
460                                                         mc = PARISC_EXTRW_U ^ 0x3f0U;
461                                                         break;
462                                                 default:
463                                                         internal(file_line, "cgen_mov_args: invalid op size %u", size);
464                                         }
465                                 } else {
466                                         switch (size) {
467                                                 case OP_SIZE_1:
468                                                         mc = PARISC_EXTRD_U ^ 0x13f8U;
469                                                         break;
470                                                 case OP_SIZE_2:
471                                                         mc = PARISC_EXTRD_U ^ 0x13f0U;
472                                                         break;
473                                                 case OP_SIZE_4:
474                                                         mc = PARISC_EXTRD_U ^ 0x13e0U;
475                                                         break;
476                                                 default:
477                                                         internal(file_line, "cgen_mov_args: invalid op size %u", size);
478                                         }
479                                 }
480                                 g(cgen_2reg(ctx, mc, arg2[0], arg1[0]));
481                                 return true;
482                         }
483                         g(cgen_3reg(ctx, PARISC_OR, arg2[0], 0, arg1[0]));
484                         return true;
485                 }
486                 if (arg2[0] == ARG_IMM) {
487                         imm = get_imm(&arg2[1]);
488                         if (imm >= -0x2000 && imm < 0x2000) {
489                                 mc = PARISC_LDO;
490                                 mc |= low_sign_ext_14(imm);
491                                 g(cgen_2reg(ctx, mc, R_ZERO, arg1[0]));
492                                 return true;
493                         }
494                         if (imm & 0x7ff)
495                                 goto invalid;
496                         mc = PARISC_LDIL;
497                         mc |= (uint32_t)arg1[0] << 21;
498                         mc |= assemble_21((uint64_t)imm >> 11);
499                         cgen_four(mc);
500                         return true;
501                 }
502                 return cgen_ld_st(ctx, true, size, arg1[0], arg2);
503         }
505         if (arg2[0] < 0x20) {
506                 return cgen_ld_st(ctx, false, size, arg2[0], arg1);
507         }
508         if (arg2[0] == ARG_IMM) {
509                 imm = get_imm(&arg2[1]);
510                 if (unlikely(imm != 0))
511                         goto invalid;
512                 return cgen_ld_st(ctx, false, size, R_ZERO, arg1);
513         }
515         if (reg_is_fp(arg1[0]) && arg2[0] == ARG_ADDRESS_1) {
516                 return cgen_fp_ld_st(ctx, true, size, arg1[0], arg2);
517         }
518         if (reg_is_fp(arg2[0]) && arg1[0] == ARG_ADDRESS_1) {
519                 return cgen_fp_ld_st(ctx, false, size, arg2[0], arg1);
520         }
521         if (reg_is_fp(arg1[0]) && reg_is_fp(arg2[0])) {
522                 mc = PARISC_FCPY;
523                 switch (size) {
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);
527                 }
528                 g(cgen_3reg(ctx, mc, 0, arg2[0] & 0x1f, arg1[0] & 0x1f));
529                 return true;
530         }
532 invalid:
533         internal(file_line, "cgen_mov_args: invalid arguments %02x, %02x", arg1[0], arg2[0]);
534         return false;
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)
547         uint32_t mc;
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) {
556                         switch (size) {
557                                 case OP_SIZE_1:
558                                         mc = PARISC_EXTRW_S ^ 0x3f8U;
559                                         break;
560                                 case OP_SIZE_2:
561                                         mc = PARISC_EXTRW_S ^ 0x3f0U;
562                                         break;
563                                 default:
564                                         internal(file_line, "cgen_movsx: invalid op size %u", size);
565                         }
566                 } else {
567                         switch (size) {
568                                 case OP_SIZE_1:
569                                         mc = PARISC_EXTRD_S ^ 0x13f8U;
570                                         break;
571                                 case OP_SIZE_2:
572                                         mc = PARISC_EXTRD_S ^ 0x13f0U;
573                                         break;
574                                 case OP_SIZE_4:
575                                         mc = PARISC_EXTRD_S ^ 0x13e0U;
576                                         break;
577                                 default:
578                                         internal(file_line, "cgen_movsx: invalid op size %u", size);
579                         }
580                 }
581                 g(cgen_2reg(ctx, mc, arg2[0], arg1[0]));
582                 return true;
583         } else {
584                 return cgen_mov_args(ctx, size, arg1, arg2);
585         }
588 static bool attr_w cgen_cmp_dest_reg(struct codegen_context *ctx, unsigned size, unsigned cond)
590         int32_t fc;
591         uint32_t mc;
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);
599         }
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]);
606                 mc = PARISC_CMPICLR;
607                 if (size == OP_SIZE_8)
608                         mc |= PARISC_CMPICLR_64;
609                 mc |= fc;
610                 mc |= low_sign_ext_11(imm);
611                 g(cgen_2reg(ctx, mc, arg2[0], arg1[0]));
612         } else {
613                 mc = PARISC_CMPCLR;
614                 if (size == OP_SIZE_8)
615                         mc |= PARISC_COND_64;
616                 mc |= fc;
617                 g(cgen_3reg(ctx, mc, arg2[0], arg3[0], arg1[0]));
618         }
620         mc = PARISC_LDO;
621         mc |= low_sign_ext_14(1);
622         g(cgen_2reg(ctx, mc, R_ZERO, arg1[0]));
624         return true;
627 static bool attr_w cgen_alu(struct codegen_context *ctx, unsigned size, unsigned writes_flags, unsigned alu, bool trap, unsigned length)
629         uint32_t mc;
630         uint8_t z = R_ZERO;
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))
637                 goto invalid;
639         if (arg3[0] == ARG_IMM && !trap) {
640                 int64_t imm = get_imm(&arg3[1]);
641                 if (!imm) {
642                         arg3 = &z;
643                         goto do_3reg;
644                 }
645                 if (unlikely(writes_flags))
646                         goto invalid;
647                 switch (alu) {
648                         case ALU_SUB:
649                                 imm = -(uint64_t)imm;
650                                 /*-fallthrough*/
651                         case ALU_ADD:
652                                 if (imm >= -0x2000 && imm < 0x2000) {
653                                         mc = PARISC_LDO;
654                                         mc |= low_sign_ext_14(imm);
655                                         g(cgen_2reg(ctx, mc, arg2[0], arg1[0]));
656                                         return true;
657                                 } else if (!(imm & 0x7ff) && arg1[0] == R_1) {
658                                         mc = PARISC_ADDIL;
659                                         mc |= (uint32_t)arg2[0] << 21;
660                                         mc |= assemble_21((uint64_t)imm >> 11);
661                                         cgen_four(mc);
662                                         return true;
663                                 } else {
664                                         goto invalid;
665                                 }
666                         default:
667                                 goto invalid;
668                 }
669         }
671 do_3reg:
672         switch (alu) {
673                 case ALU_ADD:   mc = writes_flags ? PARISC_ADD : PARISC_ADD_L;
674                                 break;
675                 case ALU_OR:    ajla_assert_lo(!writes_flags, (file_line, "cgen_alu: or must not write flags"));
676                                 mc = PARISC_OR;
677                                 break;
678                 case ALU_ADC:   ajla_assert_lo(writes_flags, (file_line, "cgen_alu: adc must write flags"));
679                                 mc = size == OP_SIZE_4 ? PARISC_ADD_C : PARISC_ADD_DC;
680                                 break;
681                 case ALU_SBB:   ajla_assert_lo(writes_flags, (file_line, "cgen_alu: sbb must write flags"));
682                                 mc = size == OP_SIZE_4 ? PARISC_SUB_B : PARISC_SUB_DB;
683                                 break;
684                 case ALU_AND:   ajla_assert_lo(!writes_flags, (file_line, "cgen_alu: and must not write flags"));
685                                 mc = PARISC_AND;
686                                 break;
687                 case ALU_SUB:   ajla_assert_lo(writes_flags, (file_line, "cgen_alu: sub must write flags"));
688                                 mc = PARISC_SUB;
689                                 break;
690                 case ALU_XOR:   ajla_assert_lo(!writes_flags, (file_line, "cgen_alu: xor must not write flags"));
691                                 mc = PARISC_XOR;
692                                 break;
693                 case ALU_ANDN:  ajla_assert_lo(!writes_flags, (file_line, "cgen_alu: andn must not write flags"));
694                                 mc = PARISC_ANDCM;
695                                 break;
696                 default:        goto invalid;
697         }
699         if (alu == ALU_ADD && arg3[0] == ARG_SHIFTED_REGISTER) {
700                 uint8_t *args;
701                 if (unlikely((arg3[1] >> 6) != 0))
702                         goto invalid;
703                 if (unlikely((arg3[1] & ARG_SHIFT_AMOUNT)) >= 4)
704                         goto invalid;
705                 mc |= (uint32_t)(arg3[1] & ARG_SHIFT_AMOUNT) << 6;
706                 arg3 += 2;
707                 args = arg3;
708                 arg3 = arg2;
709                 arg2 = args;
710         }
712         if (unlikely(arg3[0] >= 0x20))
713                 goto invalid;
715 #ifdef ARCH_PARISC32
716         if (alu == ALU_ADD && size == OP_SIZE_4 && arg1[0] == arg2[0] && trap && length == JMP_SHORTEST) {
717                 bool known;
718                 mc = PARISC_ADDB;
719                 mc |= PARISC_COND_SV;
720                 mc |= PARISC_N;
721                 g(add_relocation(ctx, JMP_SHORTEST, ctx->code_position - arg1, &known));
722                 g(cgen_2reg(ctx, mc, arg1[0], arg3[0]));
723                 if (unlikely(!known))
724                         cgen_four(PARISC_NOP);
725                 return true;
726         }
727 #endif
729         if (trap) {
730                 if (length == JMP_EXTRA_LONG || (length == JMP_LONG && !PA_20))
731                         mc |= PARISC_COND_SV;
732                 else
733                         mc |= PARISC_COND_NSV;
734                 if (size == OP_SIZE_8)
735                         mc |= PARISC_COND_64;
736         }
738         g(cgen_3reg(ctx, mc, arg2[0], arg3[0], arg1[0]));
740         if (trap) {
741                 if (length <= JMP_SHORT) {
742                         g(add_relocation(ctx, JMP_SHORT, ctx->code_position - arg1, NULL));
743                         cgen_four(PARISC_BL | PARISC_N);
744                 } else if (length == JMP_LONG && PA_20) {
745                         g(add_relocation(ctx, JMP_LONG, ctx->code_position - arg1, NULL));
746                         cgen_four(PARISC_BL_L | PARISC_N);
747                 } else {
748                         cgen_four(PARISC_BL | PARISC_N | assemble_12(3));
749                         g(add_relocation(ctx, JMP_EXTRA_LONG, ctx->code_position - arg1, NULL));
750                         cgen_four(PARISC_LDIL | ((uint32_t)R_CG_SCRATCH << 21));
751                         g(cgen_2reg(ctx, PARISC_LDO, R_CG_SCRATCH, R_CG_SCRATCH));
752                         g(cgen_2reg(ctx, PARISC_BLR | PARISC_N, R_ZERO, R_CG_SCRATCH));
753                         g(cgen_2reg(ctx, PARISC_BLR | PARISC_N, R_ZERO, R_CG_SCRATCH));
754                 }
755         }
757         return true;
759 invalid:
760         internal(file_line, "cgen_alu: invalid arguments %02x, %02x, %02x, %u, %u", arg1[0], arg2[0], arg3[0], alu, writes_flags);
761         return false;
764 static bool attr_w cgen_alu1(struct codegen_context *ctx, unsigned size, unsigned writes_flags, unsigned alu, bool trap, unsigned length)
766         uint32_t mc;
767         unsigned r1, r2, rt;
768         uint8_t *arg1 = ctx->code_position;
769         uint8_t *arg2 = arg1 + arg_size(*arg1);
770         ctx->code_position = arg2 + arg_size(*arg2);
772         if (unlikely(arg1[0] >= 0x20) || unlikely(arg2[0] >= 0x20))
773                 goto invalid;
775         switch (alu) {
776                 case ALU1_NOT:  ajla_assert_lo(!writes_flags, (file_line, "cgen_alu1: not must not write flags"));
777                                 mc = PARISC_UADDCM;
778                                 r1 = R_ZERO;
779                                 r2 = arg2[0];
780                                 rt = arg1[0];
781                                 break;
782                 case ALU1_NEG:  ajla_assert_lo(writes_flags, (file_line, "cgen_alu1: neg must write flags"));
783                                 mc = PARISC_SUB;
784                                 r1 = R_ZERO;
785                                 r2 = arg2[0];
786                                 rt = arg1[0];
787                                 break;
788                 case ALU1_INC:
789                 case ALU1_DEC:  ajla_assert_lo(writes_flags, (file_line, "cgen_alu1: inc/dec must write flags"));
790                                 if (trap && size == OP_SIZE_8) {
791                                         mc = PARISC_LDO;
792                                         mc |= low_sign_ext_14(alu == ALU1_INC ? 1 : -1);
793                                         g(cgen_2reg(ctx, mc, R_ZERO, R_CG_SCRATCH));
795                                         mc = PARISC_ADD;
796                                         r1 = arg2[0];
797                                         r2 = R_CG_SCRATCH;
798                                         rt = arg1[0];
799                                 } else {
800                                         mc = PARISC_ADDI;
801                                         mc |= low_sign_ext_11(alu == ALU1_INC ? 1 : -1);
802                                         r1 = arg1[0];
803                                         r2 = arg2[0];
804                                         rt = 0;
805                                 }
806                                 break;
807                 default:        goto invalid;
808         }
810         if (trap) {
811                 if (length == JMP_EXTRA_LONG || (length == JMP_LONG && !PA_20))
812                         mc |= PARISC_COND_SV;
813                 else
814                         mc |= PARISC_COND_NSV;
815                 if (size == OP_SIZE_8)
816                         mc |= PARISC_COND_64;
817         }
819         g(cgen_3reg(ctx, mc, r1, r2, rt));
821         if (trap) {
822                 if (length <= JMP_SHORT) {
823                         g(add_relocation(ctx, JMP_SHORT, ctx->code_position - arg1, NULL));
824                         cgen_four(PARISC_BL | PARISC_N);
825                 } else if (length == JMP_LONG && PA_20) {
826                         g(add_relocation(ctx, JMP_LONG, ctx->code_position - arg1, NULL));
827                         cgen_four(PARISC_BL_L | PARISC_N);
828                 } else {
829                         cgen_four(PARISC_BL | PARISC_N | assemble_12(3));
830                         g(add_relocation(ctx, JMP_EXTRA_LONG, ctx->code_position - arg1, NULL));
831                         cgen_four(PARISC_LDIL | ((uint32_t)R_CG_SCRATCH << 21));
832                         g(cgen_2reg(ctx, PARISC_LDO, R_CG_SCRATCH, R_CG_SCRATCH));
833                         g(cgen_2reg(ctx, PARISC_BLR | PARISC_N, R_ZERO, R_CG_SCRATCH));
834                         g(cgen_2reg(ctx, PARISC_BLR | PARISC_N, R_ZERO, R_CG_SCRATCH));
835                 }
836         }
838         return true;
840 invalid:
841         internal(file_line, "cgen_alu1: invalid arguments %02x, %02x, %u, %u", arg1[0], arg2[0], alu, writes_flags);
842         return false;
845 static bool attr_w cgen_sar(struct codegen_context *ctx, bool sar_complement, uint8_t reg)
847         uint32_t mc;
848         if (!sar_complement) {
849                 mc = PARISC_MTSAR;
850                 mc |= (uint32_t)reg << 16;
851                 cgen_four(mc);
852         } else if (PA_20) {
853                 mc = PARISC_MTSARCM;
854                 mc |= (uint32_t)reg << 16;
855                 cgen_four(mc);
856         } else {
857                 mc = PARISC_LDO;
858                 mc |= low_sign_ext_14(-1);
859                 g(cgen_2reg(ctx, mc, 0, R_CG_SCRATCH));
860                 mc = PARISC_XOR;
861                 g(cgen_3reg(ctx, mc, R_CG_SCRATCH, reg, R_CG_SCRATCH));
862                 mc = PARISC_MTSAR;
863                 mc |= (uint32_t)R_CG_SCRATCH << 16;
864                 cgen_four(mc);
865         }
866         return true;
869 static bool attr_w cgen_rot(struct codegen_context *ctx, unsigned size, unsigned rot)
871         uint32_t mc;
872         uint8_t *arg1 = ctx->code_position;
873         uint8_t *arg2 = arg1 + arg_size(*arg1);
874         uint8_t *arg3 = arg2 + arg_size(*arg2);
875         ctx->code_position = arg3 + arg_size(*arg3);
876         if (arg3[0] == ARG_IMM) {
877                 int64_t imm = get_imm(&arg3[1]);
878                 unsigned sh = imm & (size == OP_SIZE_4 ? 31 : 63);
879                 if (size == OP_SIZE_4) {
880                         switch (rot) {
881                                 case ROT_SHL:
882                                         mc = PARISC_DEPW_Z;
883                                         mc |= sh | (sh << 5);
884                                         break;
885                                 case ROT_SHR:
886                                         mc = PARISC_EXTRW_U;
887                                         mc |= sh | ((31 - sh) << 5);
888                                         break;
889                                 case ROT_SAR:
890                                         mc = PARISC_EXTRW_S;
891                                         mc |= sh | ((31 - sh) << 5);
892                                         break;
893                                 default:
894                                         goto invalid;
895                         }
896                 } else {
897                         switch (rot) {
898                                 case ROT_SHL:
899                                         mc = PARISC_DEPD_Z;
900                                         mc |= (sh & 31) | ((sh & 31) << 5);
901                                         if (sh & 32)
902                                                 mc ^= 0x1800;
903                                         break;
904                                 case ROT_SHR:
905                                         mc = PARISC_EXTRD_U;
906                                         mc |= (sh & 31) | ((31 - (sh & 31)) << 5);
907                                         if (sh & 32)
908                                                 mc ^= 0x1800;
909                                         break;
910                                 case ROT_SAR:
911                                         mc = PARISC_EXTRD_S;
912                                         mc |= (sh & 31) | ((31 - (sh & 31)) << 5);
913                                         if (sh & 32)
914                                                 mc ^= 0x1800;
915                                         break;
916                                 default:
917                                         goto invalid;
918                         }
919                 }
920                 if (rot == ROT_SHL)
921                         g(cgen_2reg(ctx, mc, arg1[0], arg2[0]));
922                 else
923                         g(cgen_2reg(ctx, mc, arg2[0], arg1[0]));
924                 return true;
925         } else if (arg3[0] < 0x20) {
926                 bool sar_complement = rot != ROT_SHR;
927                 g(cgen_sar(ctx, sar_complement, arg3[0]));
928                 if (size == OP_SIZE_4) {
929                         switch (rot) {
930                                 case ROT_SHL:
931                                         mc = PARISC_DEPW_Z_SAR;
932                                         break;
933                                 case ROT_SHR:
934                                         mc = PARISC_SHRPW_SAR;
935                                         break;
936                                 case ROT_SAR:
937                                         mc = PARISC_EXTRW_S_SAR;
938                                         break;
939                                 default:
940                                         goto invalid;
941                         }
942                 } else {
943                         switch (rot) {
944                                 case ROT_SHL:
945                                         mc = PARISC_DEPD_Z_SAR | 0x100;
946                                         break;
947                                 case ROT_SHR:
948                                         mc = PARISC_SHRPD_SAR;
949                                         break;
950                                 case ROT_SAR:
951                                         mc = PARISC_EXTRD_S_SAR | 0x100;
952                                         break;
953                                 default:
954                                         goto invalid;
955                         }
956                 }
957                 if (rot == ROT_SHL) {
958                         g(cgen_2reg(ctx, mc, arg1[0], arg2[0]));
959                 } else if (rot == ROT_SHR) {
960                         g(cgen_3reg(ctx, mc, R_ZERO, arg2[0], arg1[0]));
961                 } else {
962                         g(cgen_2reg(ctx, mc, arg2[0], arg1[0]));
963                 }
964                 return true;
965         }
967 invalid:
968         internal(file_line, "cgen_rot: invalid arguments %02x, %02x, %02x, %u, %u", arg1[0], arg2[0], arg3[0], size, rot);
969         return false;
972 static bool attr_w cgen_btx(struct codegen_context *ctx, unsigned size, unsigned aux)
974         uint32_t mc;
975         uint8_t *arg1 = ctx->code_position;
976         uint8_t *arg2 = arg1 + arg_size(*arg1);
977         uint8_t *arg3 = arg2 + arg_size(*arg2);
978         ctx->code_position = arg3 + arg_size(*arg3);
980         if (aux == BTX_BTS || aux == BTX_BTR) {
981                 if (arg1[0] != arg2[0]) {
982                         g(cgen_3reg(ctx, PARISC_OR, arg2[0], 0, arg1[0]));
983                 }
984         }
986         if (arg3[0] == ARG_IMM) {
987                 int64_t imm = get_imm(&arg3[1]);
988                 unsigned bit = imm & (size == OP_SIZE_4 ? 31 : 63);
989                 if (size == OP_SIZE_4) {
990                         switch (aux) {
991                                 case BTX_BTS:
992                                 case BTX_BTR:
993                                         mc = PARISC_DEPWI;
994                                         mc |= 31U | (bit << 5);
995                                         g(cgen_2reg(ctx, mc, arg1[0], low_sign_ext_5(aux == BTX_BTS)));
996                                         return true;
997                                 case BTX_BTEXT:
998                                         mc = PARISC_EXTRW_U;
999                                         mc |= 31U | ((31 - bit) << 5);
1000                                         g(cgen_2reg(ctx, mc, arg2[0], arg1[0]));
1001                                         return true;
1002                                 default:
1003                                         goto invalid;
1004                         }
1005                 } else {
1006                         switch (aux) {
1007                                 case BTX_BTS:
1008                                 case BTX_BTR:
1009                                         mc = PARISC_DEPDI;
1010                                         mc |= 31U | ((bit & 0x1f) << 5) | ((bit & 0x20) << 6);
1011                                         g(cgen_2reg(ctx, mc, arg1[0], low_sign_ext_5(aux == BTX_BTS)));
1012                                         return true;
1013                                 case BTX_BTEXT:
1014                                         mc = PARISC_EXTRD_U;
1015                                         mc |= 31U | (((63 - bit) & 0x1f) << 5) | (((63 - bit) & 0x20) << 6);
1016                                         g(cgen_2reg(ctx, mc, arg2[0], arg1[0]));
1017                                         return true;
1018                                 default:
1019                                         goto invalid;
1020                         }
1021                 }
1022         } else {
1023                 g(cgen_sar(ctx, true, arg3[0]));
1024                 if (size == OP_SIZE_4) {
1025                         switch (aux) {
1026                                 case BTX_BTS:
1027                                 case BTX_BTR:
1028                                         mc = PARISC_DEPWI_SAR;
1029                                         mc |= 31U;
1030                                         g(cgen_2reg(ctx, mc, arg1[0], low_sign_ext_5(aux == BTX_BTS)));
1031                                         return true;
1032                                 case BTX_BTEXT:
1033                                         mc = PARISC_EXTRW_U_SAR;
1034                                         mc |= 31U;
1035                                         g(cgen_2reg(ctx, mc, arg2[0], arg1[0]));
1036                                         return true;
1037                                 default:
1038                                         goto invalid;
1039                         }
1040                 } else {
1041                         switch (aux) {
1042                                 case BTX_BTS:
1043                                 case BTX_BTR:
1044                                         mc = PARISC_DEPDI_SAR;
1045                                         mc |= 31U;
1046                                         g(cgen_2reg(ctx, mc, arg1[0], low_sign_ext_5(aux == BTX_BTS)));
1047                                         return true;
1048                                 case BTX_BTEXT:
1049                                         mc = PARISC_EXTRD_U_SAR;
1050                                         mc |= 31U;
1051                                         g(cgen_2reg(ctx, mc, arg2[0], arg1[0]));
1052                                         return true;
1053                                 default:
1054                                         goto invalid;
1055                         }
1056                 }
1057         }
1059 invalid:
1060         internal(file_line, "cgen_btx: invalid arguments %02x, %02x, %02x, %u, %u", arg1[0], arg2[0], arg3[0], size, aux);
1061         return false;
1064 static bool attr_w cgen_cmp_cmov(struct codegen_context *ctx, unsigned op_size, unsigned cond)
1066         int32_t fc;
1067         uint32_t mc;
1068         int64_t imm;
1069         uint8_t *arg1 = ctx->code_position;
1070         uint8_t *arg2 = arg1 + arg_size(*arg1);
1071         uint8_t *arg3 = arg2 + arg_size(*arg2);
1072         uint8_t *arg4 = arg3 + arg_size(*arg3);
1073         uint8_t *arg5 = arg4 + arg_size(*arg4);
1074         ctx->code_position = arg5 + arg_size(*arg5);
1076         if (arg3[0] == ARG_IMM) {
1077                 cond = cmp_swap_arguments(cond);
1078         }
1080         fc = cmp_sub_cond[cond];
1081         fc ^= PARISC_COND_NEG;
1083         if (arg5[0] == ARG_IMM) {
1084                 imm = get_imm(&arg5[1]);
1085                 mc = PARISC_CMPICLR;
1086                 mc |= fc;
1087                 mc |= low_sign_ext_11(imm);
1088                 g(cgen_2reg(ctx, mc, arg4[0], R_ZERO));
1089         } else {
1090                 mc = PARISC_CMPCLR;
1091                 mc |= fc;
1092                 g(cgen_3reg(ctx, mc, arg4[0], arg5[0], R_ZERO));
1093         }
1095         g(cgen_mov_args(ctx, op_size, arg1, arg3));
1097         return true;
1100 static bool attr_w cgen_movr(struct codegen_context *ctx, unsigned cond)
1102         int32_t fc;
1103         uint32_t mc;
1104         uint8_t *arg1 = ctx->code_position;
1105         uint8_t *arg2 = arg1 + arg_size(*arg1);
1106         uint8_t *arg3 = arg2 + arg_size(*arg2);
1107         uint8_t *arg4 = arg3 + arg_size(*arg3);
1108         ctx->code_position = arg4 + arg_size(*arg4);
1110         fc = cmp_sub_cond[cond];
1111         fc ^= PARISC_COND_NEG;
1113         if (unlikely(arg1[0] != arg2[0]) || unlikely(arg1[0] >= 0x20))
1114                 internal(file_line, "cgen_movr: invalid arguments");
1116         mc = PARISC_ADD_L;
1117         mc |= fc;
1118         if (OP_SIZE_NATIVE == OP_SIZE_8)
1119                 mc |= PARISC_COND_64;
1121         g(cgen_3reg(ctx, mc, R_ZERO, arg3[0], R_ZERO));
1123         g(cgen_mov_args(ctx, OP_SIZE_NATIVE, arg1, arg4));
1125         return true;
1128 static bool attr_w cgen_fp_cmp_cond(struct codegen_context *ctx, unsigned op_size, unsigned aux)
1130         uint32_t mc;
1131         unsigned cond;
1132         uint8_t *arg1 = ctx->code_position;
1133         uint8_t *arg2 = arg1 + arg_size(*arg1);
1134         ctx->code_position = arg2 + arg_size(*arg2);
1135         switch (aux) {
1136                 case FP_COND_P:         cond = PARISC_FCMP_P; break;
1137                 case FP_COND_NP:        cond = PARISC_FCMP_NP; break;
1138                 case FP_COND_E:         cond = PARISC_FCMP_E; break;
1139                 case FP_COND_NE:        cond = PARISC_FCMP_NE; break;
1140                 case FP_COND_A:         cond = PARISC_FCMP_A; break;
1141                 case FP_COND_BE:        cond = PARISC_FCMP_BE; break;
1142                 case FP_COND_B:         cond = PARISC_FCMP_B; break;
1143                 case FP_COND_AE:        cond = PARISC_FCMP_AE; break;
1144                 default:                internal(file_line, "cgen_fp_cmp_cond: invalid condition %u", aux);
1145                                         return false;
1146         }
1147         mc = PARISC_FCMP;
1148         if (op_size == OP_SIZE_8)
1149                 mc |= PARISC_FP_DOUBLE;
1150         g(cgen_3reg(ctx, mc, arg1[0] & 0x1f, arg2[0] & 0x1f, cond));
1151         return true;
1154 static bool attr_w cgen_fp_test_reg(struct codegen_context *ctx)
1156         unsigned reg = cget_one(ctx);
1157         uint32_t mc;
1159         mc = PARISC_LDO;
1160         mc |= low_sign_ext_14(1);
1161         g(cgen_2reg(ctx, mc, R_ZERO, reg));
1163         cgen_four(PARISC_FTEST);
1165         mc = PARISC_LDO;
1166         mc |= low_sign_ext_14(0);
1167         g(cgen_2reg(ctx, mc, R_ZERO, reg));
1169         return true;
1172 static bool attr_w cgen_fp_alu(struct codegen_context *ctx, unsigned op_size, unsigned aux)
1174         uint32_t mc;
1175         uint8_t *arg1 = ctx->code_position;
1176         uint8_t *arg2 = arg1 + arg_size(*arg1);
1177         uint8_t *arg3 = arg2 + arg_size(*arg2);
1178         ctx->code_position = arg3 + arg_size(*arg3);
1179         switch (aux) {
1180                 case FP_ALU_ADD:        mc = PARISC_FADD; break;
1181                 case FP_ALU_SUB:        mc = PARISC_FSUB; break;
1182                 case FP_ALU_MUL:        mc = PARISC_FMPY; break;
1183                 case FP_ALU_DIV:        mc = PARISC_FDIV; break;
1184                 default:                internal(file_line, "cgen_fp_alu: invalid alu %u", aux);
1185         }
1186         switch (op_size) {
1187                 case OP_SIZE_4:         break;
1188                 case OP_SIZE_8:         mc |= PARISC_FP_DOUBLE; break;
1189                 default:                internal(file_line, "cgen_fp_alu: invalid size %u", op_size);
1190         }
1191         g(cgen_3reg(ctx, mc, arg3[0] & 0x1f, arg2[0] & 0x1f, arg1[0] & 0x1f));
1192         return true;
1195 static bool attr_w cgen_fp_alu1(struct codegen_context *ctx, unsigned op_size, unsigned aux)
1197         uint32_t mc;
1198         uint8_t *arg1 = ctx->code_position;
1199         uint8_t *arg2 = arg1 + arg_size(*arg1);
1200         ctx->code_position = arg2 + arg_size(*arg2);
1201         switch (aux) {
1202                 case FP_ALU1_NEG:       mc = PARISC_FNEG; break;
1203                 case FP_ALU1_SQRT:      mc = PARISC_FSQRT; break;
1204                 default:                internal(file_line, "cgen_fp_alu1: invalid alu %u", aux);
1205         }
1206         switch (op_size) {
1207                 case OP_SIZE_4:         break;
1208                 case OP_SIZE_8:         mc |= PARISC_FP_DOUBLE; break;
1209                 default:                internal(file_line, "cgen_fp_alu: invalid size %u", op_size);
1210         }
1211         g(cgen_3reg(ctx, mc, 0, arg2[0] & 0x1f, arg1[0] & 0x1f));
1212         return true;
1215 static bool attr_w cgen_fp_to_int(struct codegen_context *ctx, unsigned int_op_size, unsigned fp_op_size)
1217         uint32_t mc;
1218         uint8_t *arg1 = ctx->code_position;
1219         uint8_t *arg2 = arg1 + arg_size(*arg1);
1220         ctx->code_position = arg2 + arg_size(*arg2);
1222         mc = PARISC_FCNVFXT;
1223         if (int_op_size == OP_SIZE_8)
1224                 mc |= PARISC_FCNVFXT_TO_LONG;
1225         if (fp_op_size == OP_SIZE_8)
1226                 mc |= PARISC_FCNVFXT_FROM_DOUBLE;
1227         mc |= arg1[0] & 0x1f;
1228         mc |= (uint32_t)(arg2[0] & 0x1f) << 21;
1229         cgen_four(mc);
1230         return true;
1233 static bool attr_w cgen_fp_from_int(struct codegen_context *ctx, unsigned int_op_size, unsigned fp_op_size)
1235         uint32_t mc;
1236         uint8_t *arg1 = ctx->code_position;
1237         uint8_t *arg2 = arg1 + arg_size(*arg1);
1238         ctx->code_position = arg2 + arg_size(*arg2);
1240         mc = PARISC_FCNVXF;
1241         if (int_op_size == OP_SIZE_8)
1242                 mc |= PARISC_FCNVXF_FROM_LONG;
1243         if (fp_op_size == OP_SIZE_8)
1244                 mc |= PARISC_FCNVXF_TO_DOUBLE;
1245         mc |= arg1[0] & 0x1f;
1246         mc |= (uint32_t)(arg2[0] & 0x1f) << 21;
1247         cgen_four(mc);
1248         return true;
1251 static bool attr_w cgen_cond_jmp(struct codegen_context *ctx, uint32_t mc, unsigned length, unsigned reg1, unsigned reg2, int reloc_offset)
1253         if (length == JMP_SHORTEST) {
1254                 bool known;
1255                 mc |= PARISC_N;
1256                 g(add_relocation(ctx, JMP_SHORTEST, reloc_offset, &known));
1257                 g(cgen_2reg(ctx, mc, reg2, reg1));
1258                 if (unlikely(!known))
1259                         cgen_four(PARISC_NOP);
1260         } else if (length == JMP_SHORT) {
1261                 mc |= PARISC_N;
1262                 g(cgen_2reg(ctx, mc, reg2, reg1));
1263                 g(add_relocation(ctx, JMP_SHORT, reloc_offset, NULL));
1264                 cgen_four(PARISC_BL | PARISC_N);
1265         } else if (length == JMP_LONG && PA_20) {
1266                 mc |= PARISC_N;
1267                 g(cgen_2reg(ctx, mc, reg2, reg1));
1268                 g(add_relocation(ctx, JMP_LONG, reloc_offset, NULL));
1269                 cgen_four(PARISC_BL_L | PARISC_N);
1270         } else {
1271                 mc |= PARISC_N;
1272                 mc |= assemble_12(3);
1273                 g(cgen_2reg(ctx, mc, reg2, reg1));
1274                 g(add_relocation(ctx, JMP_EXTRA_LONG, reloc_offset, NULL));
1275                 cgen_four(PARISC_LDIL | ((uint32_t)R_CG_SCRATCH << 21));
1276                 g(cgen_2reg(ctx, PARISC_LDO, R_CG_SCRATCH, R_CG_SCRATCH));
1277                 g(cgen_2reg(ctx, PARISC_BLR | PARISC_N, R_ZERO, R_CG_SCRATCH));
1278                 g(cgen_2reg(ctx, PARISC_BLR | PARISC_N, R_ZERO, R_CG_SCRATCH));
1279         }
1280         return true;
1283 static bool attr_w cgen_jmp_12regs(struct codegen_context *ctx, unsigned size, unsigned cond, unsigned length, unsigned reg1, unsigned reg2, int reloc_offset)
1285         int32_t fc;
1286         uint32_t mc;
1287         fc = cmp_sub_cond[cond];
1288         if (length > JMP_SHORTEST)
1289                 fc ^= PARISC_COND_NEG;
1290         if (size == OP_SIZE_4)
1291                 mc = !(fc & PARISC_COND_NEG) ? PARISC_CMPB : PARISC_CMPB_NOT;
1292         else
1293                 mc = !(fc & PARISC_COND_NEG) ? PARISC_CMPB_64 : PARISC_CMPB_64_NOT;
1294         mc |= ((uint32_t)fc & ~PARISC_COND_NEG);
1295         g(cgen_cond_jmp(ctx, mc, length, reg1, reg2, reloc_offset));
1296         return true;
1299 static bool attr_w cgen_jmp(struct codegen_context *ctx, unsigned length)
1301         if (length <= JMP_SHORT) {
1302                 g(add_relocation(ctx, JMP_SHORT, 0, NULL));
1303                 cgen_four(PARISC_BL | PARISC_N);
1304         } else if (length == JMP_LONG && PA_20) {
1305                 g(add_relocation(ctx, JMP_LONG, 0, NULL));
1306                 cgen_four(PARISC_BL_L | PARISC_N);
1307         } else {
1308                 g(add_relocation(ctx, JMP_EXTRA_LONG, 0, NULL));
1309                 cgen_four(PARISC_LDIL | ((uint32_t)R_CG_SCRATCH << 21));
1310                 g(cgen_2reg(ctx, PARISC_LDO, R_CG_SCRATCH, R_CG_SCRATCH));
1311                 g(cgen_2reg(ctx, PARISC_BLR | PARISC_N, R_ZERO, R_CG_SCRATCH));
1312                 g(cgen_2reg(ctx, PARISC_BLR | PARISC_N, R_ZERO, R_CG_SCRATCH));
1313         }
1314         return true;
1317 static bool attr_w cgen_jmp_reg(struct codegen_context *ctx, unsigned size, unsigned cond, unsigned length)
1319         unsigned reg = cget_one(ctx);
1320         return cgen_jmp_12regs(ctx, size, cond, length, reg, 0, 1);
1323 static bool attr_w cgen_jmp_reg_bit(struct codegen_context *ctx, unsigned size, unsigned bit, bool jnz, unsigned length)
1325         uint32_t mc;
1326         unsigned reg = cget_one(ctx);
1327         if (length > JMP_SHORTEST)
1328                 jnz = !jnz;
1329         if (size == OP_SIZE_4 || bit < 32) {
1330                 bit = 31 - bit;
1331                 mc = jnz ? PARISC_BB_1 : PARISC_BB_0;
1332         } else {
1333                 bit = 63 - bit;
1334                 mc = jnz ? PARISC_BB_64_1 : PARISC_BB_64_0;
1335         }
1336         g(cgen_cond_jmp(ctx, mc, length, reg, bit, 1));
1337         return true;
1340 static bool attr_w cgen_jmp_2regs(struct codegen_context *ctx, unsigned size, unsigned cond, unsigned length)
1342         uint8_t *arg1 = ctx->code_position;
1343         uint8_t *arg2 = arg1 + arg_size(*arg1);
1344         ctx->code_position = arg2 + arg_size(*arg2);
1345         if (arg2[0] < 0x20)
1346                 return cgen_jmp_12regs(ctx, size, cond, length, arg1[0], arg2[0], 2);
1347         if (arg2[0] == ARG_IMM) {
1348                 int64_t imm = get_imm(&arg2[1]);
1349                 uint32_t mc;
1350                 int32_t fc;
1351                 cond = cmp_swap_arguments(cond);
1352                 fc = cmp_sub_cond[cond];
1353                 if (length > JMP_SHORTEST)
1354                         fc ^= PARISC_COND_NEG;
1355                 if (size == OP_SIZE_4) {
1356                         mc = fc & PARISC_COND_NEG ? PARISC_CMPIB_NOT : PARISC_CMPIB;
1357                         mc |= fc & ~PARISC_COND_NEG;
1358                 } else {
1359                         mc = PARISC_CMPIB_64;
1360                         switch (fc) {
1361                                 case PARISC_COND_EQUAL:         mc |= 1 << 13; break;
1362                                 case PARISC_COND_NOT_EQUAL:     mc |= 5 << 13; break;
1363                                 case PARISC_COND_L_S:           mc |= 2 << 13; break;
1364                                 case PARISC_COND_GE_S:          mc |= 6 << 13; break;
1365                                 case PARISC_COND_LE_S:          mc |= 3 << 13; break;
1366                                 case PARISC_COND_G_S:           mc |= 7 << 13; break;
1367                                 case PARISC_COND_L_U:           mc |= 0 << 13; break;
1368                                 case PARISC_COND_GE_U:          mc |= 4 << 13; break;
1369                                 default:        internal(file_line, "cgen_jmp_2regs: unsupported condition %x", fc);
1370                         }
1371                 }
1372                 g(cgen_cond_jmp(ctx, mc, length, low_sign_ext_5(imm), arg1[0], 10));
1373                 return true;
1374         }
1375         internal(file_line, "cgen_jmp_2regs: invalid arguments %02x, %02x", arg1[0], arg2[0]);
1376         return false;
1379 static bool attr_w cgen_jmp_fp_test(struct codegen_context *ctx, unsigned length)
1381         cgen_four(PARISC_FTEST);
1382         if (length <= JMP_SHORT) {
1383                 g(add_relocation(ctx, JMP_SHORT, 0, NULL));
1384                 cgen_four(PARISC_BL | PARISC_N);
1385         } else if (length == JMP_LONG && PA_20) {
1386                 g(add_relocation(ctx, JMP_LONG, 0, NULL));
1387                 cgen_four(PARISC_BL_L | PARISC_N);
1388         } else {
1389                 cgen_four(PARISC_BL | PARISC_N | assemble_12(0));
1390                 cgen_four(PARISC_BL | PARISC_N | assemble_12(3));
1391                 g(add_relocation(ctx, JMP_EXTRA_LONG, 0, NULL));
1392                 cgen_four(PARISC_LDIL | ((uint32_t)R_CG_SCRATCH << 21));
1393                 g(cgen_2reg(ctx, PARISC_LDO, R_CG_SCRATCH, R_CG_SCRATCH));
1394                 g(cgen_2reg(ctx, PARISC_BLR | PARISC_N, R_ZERO, R_CG_SCRATCH));
1395                 g(cgen_2reg(ctx, PARISC_BLR | PARISC_N, R_ZERO, R_CG_SCRATCH));
1396         }
1397         return true;
1400 static bool attr_w cgen_jmp_indirect(struct codegen_context *ctx, unsigned reg)
1402         uint32_t mc;
1403 #ifdef ARCH_PARISC32
1404         if (PA_20) {
1405                 if (PA_SPACES) {
1406                         mc = PARISC_BVE_RET;
1407                         mc |= (uint32_t)reg << 21;
1408                         cgen_four(mc);
1409                         mc = PARISC_STW_LONG;
1410                         mc |= low_sign_ext_14(-0x18);
1411                         g(cgen_2reg(ctx, mc, R_SP, R_RP));
1412                 } else {
1413                         mc = PARISC_BVE_RET;
1414                         mc |= (uint32_t)reg << 21;
1415                         mc |= PARISC_N;
1416                         cgen_four(mc);
1417                 }
1418         } else {
1419                 if (PA_SPACES) {
1420                         mc = PARISC_LDSID;
1421                         mc |= (uint32_t)reg << 21;
1422                         mc |= R_CG_SCRATCH;
1423                         cgen_four(mc);
1424                         mc = PARISC_MTSP;
1425                         mc |= (uint32_t)R_CG_SCRATCH << 16;
1426                         cgen_four(mc);
1427                         mc = PARISC_BE;
1428                         mc |= (uint32_t)reg << 21;
1429                         cgen_four(mc);
1430                         mc = PARISC_STW_LONG;
1431                         mc |= low_sign_ext_14(-0x18);
1432                         g(cgen_2reg(ctx, mc, R_SP, R_RP));
1433                 } else {
1434                         mc = PARISC_BV;
1435                         mc |= (uint32_t)reg << 21;
1436                         mc |= PARISC_N;
1437                         cgen_four(mc);
1438                 }
1439         }
1440 #else
1441         mc = PARISC_BV;
1442         mc |= (uint32_t)reg << 21;
1443         mc |= PARISC_N;
1444         cgen_four(mc);
1445 #endif
1446         if (unlikely(!ctx->fn))
1447                 cgen_four(PARISC_NOP);
1448         return true;
1451 static bool attr_w cgen_call_millicode(struct codegen_context attr_unused *ctx)
1453 #ifdef ARCH_PARISC32
1454         uint32_t mc;
1455         unsigned reg = R_SCRATCH_NA_1;
1456         unsigned bit;
1458         mc = PARISC_BB_0;
1459         bit = 31 - 1;
1460         mc |= assemble_12(1);
1461         mc |= PARISC_N;
1462         g(cgen_2reg(ctx, mc, bit, reg));
1464         mc = PARISC_LD_ST;
1465         mc |= PARISC_LD_ST_W;
1466         mc |= PARISC_LD_ST_IMM;
1467         mc |= PARISC_LD_ST_LD;
1468         g(cgen_3reg(ctx, mc, low_sign_ext_5(2), reg, R_19));
1469         g(cgen_3reg(ctx, mc, low_sign_ext_5(-2), reg, reg));
1471         g(cgen_jmp_indirect(ctx, reg));
1472 #endif
1473         return true;
1476 static bool attr_w resolve_relocation(struct codegen_context *ctx, struct relocation *reloc)
1478         uint32_t mc;
1479         uint32_t mc4[4];
1480         int64_t offs = (int64_t)(ctx->label_to_pos[reloc->label_id] >> 2) - (int64_t)(reloc->position >> 2) - 2;
1481         switch (reloc->length) {
1482                 case JMP_SHORTEST:
1483                         if (unlikely(offs < -0x800) || unlikely(offs >= 0x800))
1484                                 return false;
1485                         memcpy(&mc, ctx->mcode + reloc->position, 4);
1486                         mc &= ~0x00001ffdU;
1487                         mc |= assemble_12(offs);
1488                         memcpy(ctx->mcode + reloc->position, &mc, 4);
1489                         return true;
1490                 case JMP_SHORT:
1491                         if (unlikely(offs < -0x10000) || unlikely(offs >= 0x10000))
1492                                 return false;
1493                         memcpy(&mc, ctx->mcode + reloc->position, 4);
1494                         mc &= ~0x001f1ffdU;
1495                         mc |= assemble_17(offs);
1496                         memcpy(ctx->mcode + reloc->position, &mc, 4);
1497                         return true;
1498                 case JMP_LONG:
1499                         if (unlikely(offs < -0x200000) || unlikely(offs >= 0x200000))
1500                                 return false;
1501                         memcpy(&mc, ctx->mcode + reloc->position, 4);
1502                         mc &= ~0x03ff1ffdU;
1503                         mc |= assemble_22(offs);
1504                         memcpy(ctx->mcode + reloc->position, &mc, 4);
1505                         return true;
1506                 case JMP_EXTRA_LONG:
1507                         offs -= 2;
1508                         if (unlikely(offs != (int32_t)offs))
1509                                 return false;
1510                         memcpy(&mc4, ctx->mcode + reloc->position, 16);
1511                         mc4[0] &= ~0x001fffffU;
1512                         mc4[0] |= assemble_21((uint64_t)offs >> 1 >> 11);
1513                         mc4[1] &= ~0x00003fffU;
1514                         mc4[1] |= low_sign_ext_14(((uint64_t)offs >> 1) & 0x7ff);
1515                         if (offs & 1)
1516                                 mc4[2] = PARISC_NOP;
1517                         else
1518                                 mc4[3] = PARISC_NOP;
1519                         memcpy(ctx->mcode + reloc->position, &mc4, 16);
1520                         return true;
1521                 default:
1522                         internal(file_line, "resolve_relocation: invalid relocation length %u", reloc->length);
1523         }
1524         return false;
1527 static bool attr_w cgen_insn(struct codegen_context *ctx, uint32_t insn)
1529         /*debug("insn: %08x", insn);*/
1530         switch (insn_opcode(insn)) {
1531                 case INSN_ENTRY:
1532                         g(cgen_entry(ctx));
1533                         return true;
1534                 case INSN_LABEL:
1535                         g(cgen_label(ctx));
1536                         return true;
1537                 case INSN_RET:
1538                         g(cgen_ret(ctx));
1539                         return true;
1540                 case INSN_CALL:
1541                         g(cgen_call(ctx, insn_jump_size(insn)));
1542                         return true;
1543                 case INSN_CALL_INDIRECT:
1544                         g(cgen_call_indirect(ctx));
1545                         return true;
1546                 case INSN_MOV:
1547                         g(cgen_mov(ctx, insn_op_size(insn)));
1548                         return true;
1549                 case INSN_MOVSX:
1550                         g(cgen_movsx(ctx, insn_op_size(insn)));
1551                         return true;
1552                 case INSN_CMP_DEST_REG:
1553                         if (unlikely(insn_op_size(insn) < OP_SIZE_4))
1554                                 goto invalid_insn;
1555                         g(cgen_cmp_dest_reg(ctx, insn_op_size(insn), insn_aux(insn)));
1556                         return true;
1557                 case INSN_ALU:
1558                 case INSN_ALU_FLAGS:
1559                         if (unlikely(insn_op_size(insn) < OP_SIZE_4))
1560                                 goto invalid_insn;
1561                         g(cgen_alu(ctx, insn_op_size(insn), insn_writes_flags(insn), insn_aux(insn), false, insn_jump_size(insn)));
1562                         return true;
1563                 case INSN_ALU_TRAP:
1564                 case INSN_ALU_FLAGS_TRAP:
1565                         if (unlikely(insn_op_size(insn) < OP_SIZE_4))
1566                                 goto invalid_insn;
1567                         g(cgen_alu(ctx, insn_op_size(insn), insn_writes_flags(insn), insn_aux(insn), true, insn_jump_size(insn)));
1568                         return true;
1569                 case INSN_ALU1:
1570                         if (unlikely(insn_op_size(insn) < OP_SIZE_4))
1571                                 goto invalid_insn;
1572                         g(cgen_alu1(ctx, insn_op_size(insn), insn_writes_flags(insn), insn_aux(insn), false, insn_jump_size(insn)));
1573                         return true;
1574                 case INSN_ALU1_TRAP:
1575                         if (unlikely(insn_op_size(insn) < OP_SIZE_4))
1576                                 goto invalid_insn;
1577                         g(cgen_alu1(ctx, insn_op_size(insn), insn_writes_flags(insn), insn_aux(insn), true, insn_jump_size(insn)));
1578                         return true;
1579                 case INSN_ROT:
1580                         if (unlikely(insn_op_size(insn) < OP_SIZE_4) || unlikely(insn_writes_flags(insn) != 0))
1581                                 goto invalid_insn;
1582                         g(cgen_rot(ctx, insn_op_size(insn), insn_aux(insn)));
1583                         return true;
1584                 case INSN_BTX:
1585                         if (unlikely(insn_op_size(insn) < OP_SIZE_4))
1586                                 goto invalid_insn;
1587                         g(cgen_btx(ctx, insn_op_size(insn), insn_aux(insn)));
1588                         return true;
1589                 case INSN_CMP_CMOV:
1590                         if (unlikely(insn_writes_flags(insn) != 0))
1591                                 goto invalid_insn;
1592                         g(cgen_cmp_cmov(ctx, insn_op_size(insn), insn_aux(insn)));
1593                         return true;
1594                 case INSN_MOVR:
1595                         if (unlikely(insn_op_size(insn) != OP_SIZE_NATIVE) || unlikely(insn_writes_flags(insn) != 0))
1596                                 goto invalid_insn;
1597                         g(cgen_movr(ctx, insn_aux(insn)));
1598                         return true;
1599                 case INSN_FP_CMP_COND:
1600                         g(cgen_fp_cmp_cond(ctx, insn_op_size(insn), insn_aux(insn)));
1601                         return true;
1602                 case INSN_FP_TEST_REG:
1603                         g(cgen_fp_test_reg(ctx));
1604                         return true;
1605                 case INSN_FP_ALU:
1606                         g(cgen_fp_alu(ctx, insn_op_size(insn), insn_aux(insn)));
1607                         return true;
1608                 case INSN_FP_ALU1:
1609                         g(cgen_fp_alu1(ctx, insn_op_size(insn), insn_aux(insn)));
1610                         return true;
1611                 case INSN_FP_TO_INT32:
1612                 case INSN_FP_TO_INT64:
1613                         g(cgen_fp_to_int(ctx, insn_opcode(insn) == INSN_FP_TO_INT32 ? OP_SIZE_4 : OP_SIZE_8, insn_op_size(insn)));
1614                         return true;
1615                 case INSN_FP_FROM_INT32:
1616                 case INSN_FP_FROM_INT64:
1617                         g(cgen_fp_from_int(ctx, insn_opcode(insn) == INSN_FP_FROM_INT32 ? OP_SIZE_4 : OP_SIZE_8, insn_op_size(insn)));
1618                         return true;
1619                 case INSN_JMP:
1620                         g(cgen_jmp(ctx, insn_jump_size(insn)));
1621                         return true;
1622                 case INSN_JMP_REG:
1623                         if (unlikely(insn_op_size(insn) < OP_SIZE_4))
1624                                 goto invalid_insn;
1625                         g(cgen_jmp_reg(ctx, insn_op_size(insn), insn_aux(insn), insn_jump_size(insn)));
1626                         return true;
1627                 case INSN_JMP_REG_BIT:
1628                         if (unlikely(insn_op_size(insn) < OP_SIZE_4))
1629                                 goto invalid_insn;
1630                         g(cgen_jmp_reg_bit(ctx, insn_op_size(insn), insn_aux(insn) & 63, insn_aux(insn) >> 6, insn_jump_size(insn)));
1631                         return true;
1632                 case INSN_JMP_2REGS:
1633                         if (unlikely(insn_op_size(insn) < OP_SIZE_4))
1634                                 goto invalid_insn;
1635                         g(cgen_jmp_2regs(ctx, insn_op_size(insn), insn_aux(insn), insn_jump_size(insn)));
1636                         return true;
1637                 case INSN_JMP_FP_TEST:
1638                         g(cgen_jmp_fp_test(ctx, insn_jump_size(insn)));
1639                         return true;
1640                 case INSN_JMP_INDIRECT:
1641                         g(cgen_jmp_indirect(ctx, cget_one(ctx)));
1642                         return true;
1643                 case INSN_CALL_MILLICODE:
1644                         g(cgen_call_millicode(ctx));
1645                         return true;
1646                 default:
1647                 invalid_insn:
1648                         internal(file_line, "cgen_insn: invalid insn %08x", insn);
1649                         return false;
1650         }